#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') import sys sys.path.insert(0,'..') from IPython.display import HTML,Image,SVG,YouTubeVideo from helpers import header HTML(header()) # In[2]: from skimage import data import numpy as np from skimage.morphology import disk import skimage.filter.rank as skr from skimage.measure import label from skimage.morphology import watershed from skimage.io import imread from scipy import ndimage as ndi import matplotlib.pyplot as plt from skimage.segmentation import mark_boundaries # In[3]: # segment the coins im = data.coins() plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[4]: # detect the eyes / nose im = data.chelsea() plt.imshow(im); # In[5]: # counting the galaxies im = data.hubble_deep_field() plt.imshow(im); # In[6]: im = data.page() bg = skr.median(im, disk(10)) res = (1.*im/bg) < .8 plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); plt.figure() plt.imshow(bg,cmap=plt.cm.gray); plt.colorbar() plt.figure() plt.imshow(res,cmap=plt.cm.gray); plt.colorbar(); # In[7]: Image('http://www.biotechniques.com/multimedia/archive/00003/BTN_A_000112488_O_F00_3051a.jpg') # In[8]: # segment the cells im = imread('http://homepages.ulb.ac.be/~odebeir/data/dh_phase.png') th = im>150 th1 = im>100 plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); plt.figure() plt.imshow(1.*th+th1,cmap=plt.cm.gray) # In[9]: from skimage.filter import canny ca = canny(im) plt.figure(figsize=[10,10]) plt.imshow(ca,cmap=plt.cm.gray) # In[10]: from skimage.morphology import watershed from skimage.segmentation import mark_boundaries lab,n_lab = label(th,return_num=True) bg = th1==0 lab[bg] = n_lab+1 #med = skr.median(im,disk(5)) #gr = skr.gradient(med,disk(3)) ws = watershed(255-im,lab) plt.imshow(mark_boundaries(im,ws)) # In[19]: im = imread('http://homepages.ulb.ac.be/~odebeir/data/exp0001.jpg') plt.figure(figsize=[20,20]) plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[13]: # count red and yellow flowers im = imread('http://homepages.ulb.ac.be/~odebeir/data/flowers.jpg') plt.imshow(im) plt.colorbar(); # In[14]: # find the fiber orientation im = imread('http://homepages.ulb.ac.be/~odebeir/data/image4.png') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[15]: from IPython.display import YouTubeVideo YouTubeVideo('PUcz11MLxUk', start=0, autoplay=1, theme="light", color="blue",) # In[16]: # detect stroma im = imread('http://homepages.ulb.ac.be/~odebeir/data/Rp042826d.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[17]: # segment the flowers im = imread('http://homepages.ulb.ac.be/~odebeir/data/KaneFlowers.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[18]: from skimage.morphology import watershed from skimage.segmentation import mark_boundaries gr = skr.gradient(im,disk(3)) local_min = im <= skr.minimum(im,disk(5)) lab = label(local_min) #med = skr.median(im,disk(5)) ws = watershed(gr,lab) plt.figure(figsize=[10,10]) plt.imshow(mark_boundaries(im,ws)) #plt.imshow(local_min) # In[22]: rgb = imread('http://homepages.ulb.ac.be/~odebeir/data/4colors.JPG') plt.figure(figsize=[20,20]) plt.imshow(rgb) plt.colorbar(); # In[27]: r = skr.median(rgb[:,:,0],disk(1)) plt.imshow(r,cmap=plt.cm.gray) # In[55]: s = rgb.sum(axis=2) th = s > 100 #post-processing pth = skr.minimum(th.astype(np.uint8),disk(1)) plt.figure(figsize=[20,20]) plt.imshow(pth,cmap=plt.cm.gray) plt.colorbar() # In[58]: lab = label(pth) lut = np.arange(0,np.max(lab)+1) plt.imshow(lab) plt.colorbar() mask = lab == 20 plt.imshow(mask) plt.colorbar() # In[49]: from random import shuffle shuffle(lut) # In[54]: shuffle(lut) plt.imshow(lut[lab]) plt.colorbar() # In[ ]: # In[59]: # segment the cell im = imread('http://homepages.ulb.ac.be/~odebeir/data/exp0001crop.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[68]: m = skr.median(im,disk(5)) plt.imshow(m,cmap=plt.cm.gray) plt.colorbar() # In[73]: th1 = m < 90 th2 = np.bitwise_and(110 > m,m < 130) plt.imshow(th2) # In[85]: markers = label(th2) plt.imshow(markers) plt.colorbar() # In[86]: markers[markers==3] = 2 ws = watershed(im,markers) # In[87]: plt.imshow(ws) plt.imshow(mark_boundaries(im,ws)) # In[91]: # segment the cell im = imread('http://homepages.ulb.ac.be/~odebeir/data/brain.jpg')[:,:,0] plt.figure(figsize=(10,10)) plt.imshow(im,cmap=plt.cm.gray) plt.colorbar(); # In[94]: plt.hist(im.flatten(),255); # In[95]: otsu # In[97]: from skimage.filters import threshold_otsu t_otsu = threshold_otsu(im) t_otsu # In[99]: th = im > t_otsu plt.figure(figsize=(10,10)) plt.imshow(th) # In[103]: lab = label(th,connectivity=1) plt.imshow(lab) # In[104]: from skimage.measure import regionprops # In[114]: props = regionprops(lab) brain = lab==7 pp = skr.maximum(brain,disk(3)) pp = skr.minimum(pp,disk(3)) plt.imshow(pp) # In[112]: for p in props: print p.area, p.label # In[ ]: