In [1]:
%matplotlib inline
In [2]:
import numpy as np
import pylab
import mahotas as mh
In [3]:
img=mh.imread('Data/sample.tif')
In [4]:
pylab.imshow(img)
Out[4]:
In [5]:
print img.shape
In [6]:
img=img[:,:,0]
In [20]:
print img.shape
print img.max()
print img.min()
In [8]:
T = mh.thresholding.otsu(img)
pylab.imshow(img > T)
Out[8]:
In [58]:
imgf = mh.gaussian_filter(img,4)
print imgf.shape
print imgf.max()
print imgf.min()
In [56]:
T= mh.thresholding.otsu(imgf.astype(np.uint8))
pylab.imshow(imgf > T)
Out[56]:
In [59]:
rmax = mh.regmax(imgf)
se = np.ones((5,5))
rmax_big = mh.morph.dilate(rmax,se)
over = mh.overlay(imgf.astype(np.uint8),rmax)
over2= mh.overlay(imgf.astype(np.uint8),rmax_big)
In [60]:
pylab.imshow(over2)
Out[60]:
In [29]:
seeds,nr_nuclei = mh.label(rmax)
print nr_nuclei
In [27]:
dist = mh.distance(imgf > T)
dist = dist.max() - dist
dist -= dist.min()
dist = dist/float(dist.ptp()) * 255
dist = dist.astype(np.uint8)
pylab.imshow(dist)
Out[27]:
In [31]:
nuclei = mh.cwatershed(dist,seeds)
pylab.imshow(nuclei)
Out[31]:
In [33]:
whole = mh.segmentation.gvoronoi(nuclei)
pylab.imshow(whole)
Out[33]:
In [34]:
borders = np.zeros(nuclei.shape, np.bool)
borders[ 0,:] = 1
borders[-1,:] = 1
borders[:, 0] = 1
borders[:,-1] = 1
at_border = np.unique(nuclei[borders])
for obj in at_border:
whole[whole == obj] = 0
pylab.imshow(whole)
Out[34]:
In [38]:
print whole.shape
print whole.min()
print whole.max()
In [42]:
se = np.ones((5,5))
rmax_big = mh.morph.dilate(rmax,se)
In [43]:
over = mh.overlay(imgf.astype(np.uint8),rmax_big)
pylab.imshow(over)
Out[43]:
In [45]:
over2 = mh.overlay(img.astype(np.uint8),rmax_big)
mh.imsave('Data/copy.png',over2)
In [ ]: