INFO-H-500 Image Acquisition & Processing
  • Home
  • 1.Introduction
    • Biological vision
    • Image sensors
    • Image representation
  • 2.Low-level image processing
    • Histogram
    • Linear filtering
    • Rank based filters
    • Image restoration
    • Edge detection
  • 3.Image segmentation
    • Typical image processing pipelines
    • Histogram based segmentation
    • Border based segmentation
    • Region based segmentation
    • Model based segmentation
      • Model based segmentation
      • Active contours
    • Examples
  • 4.Detection
    • Hough transform
    • Detectors
  • 5.Morphomathematics
    • Morphomathematical operators
    • Combined operations
    • The watershed transform
    • Gray level morphology
  • 6.Objects features
    • Statistical features
    • Contour features
    • Object moments
    • Texture features
  • LABS
  • References
  • About
  • Search
  • Previous
  • Next
  • GitHub
In [1]:
Copied!
%matplotlib inline
from IPython.display import HTML,Image,SVG,YouTubeVideo
%matplotlib inline from IPython.display import HTML,Image,SVG,YouTubeVideo
In [2]:
Copied!
from skimage import data
import numpy as np
from skimage.morphology import disk
import skimage.filters.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
from skimage import data import numpy as np from skimage.morphology import disk import skimage.filters.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]:
Copied!
# segment the coins
im = data.coins()
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# segment the coins im = data.coins() plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [4]:
Copied!
# detect the eyes / nose
im = data.chelsea()
plt.imshow(im);
# detect the eyes / nose im = data.chelsea() plt.imshow(im);
In [5]:
Copied!
# counting the galaxies
im = data.hubble_deep_field()
plt.imshow(im);
# counting the galaxies im = data.hubble_deep_field() plt.imshow(im);
In [6]:
Copied!
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.astype(np.uint8),cmap=plt.cm.gray);
plt.colorbar();
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.astype(np.uint8),cmap=plt.cm.gray); plt.colorbar();
In [7]:
Copied!
# segment the cells
im = imread('../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)
plt.colorbar();
# segment the cells im = imread('../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) plt.colorbar();
In [8]:
Copied!
from skimage.feature import canny


ca = canny(im)

plt.figure(figsize=[10,10])
plt.imshow(ca,cmap=plt.cm.gray);
from skimage.feature import canny ca = canny(im) plt.figure(figsize=[10,10]) plt.imshow(ca,cmap=plt.cm.gray);
In [9]:
Copied!
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))
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))
/home/olivier/.conda/envs/py3/lib/python3.7/site-packages/skimage/morphology/_deprecated.py:5: skimage_deprecation: Function ``watershed`` is deprecated and will be removed in version 0.19. Use ``skimage.segmentation.watershed`` instead.
  def watershed(image, markers=None, connectivity=1, offset=None, mask=None,
Out[9]:
<matplotlib.image.AxesImage at 0x7f31b3da2450>
In [10]:
Copied!
im = imread('../data/exp0001.jpg')
plt.figure(figsize=[20,20])
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
im = imread('../data/exp0001.jpg') plt.figure(figsize=[20,20]) plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [11]:
Copied!
# count red and yellow flowers
im = imread('../data/flowers.jpg')
plt.imshow(im)
plt.colorbar();
# count red and yellow flowers im = imread('../data/flowers.jpg') plt.imshow(im) plt.colorbar();
In [12]:
Copied!
# find the fiber orientation
im = imread('../data/image4.png')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# find the fiber orientation im = imread('../data/image4.png') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [13]:
Copied!
from IPython.display import YouTubeVideo
YouTubeVideo('PUcz11MLxUk', start=0, autoplay=1, theme="light", color="blue",)
from IPython.display import YouTubeVideo YouTubeVideo('PUcz11MLxUk', start=0, autoplay=1, theme="light", color="blue",)
Out[13]:
In [14]:
Copied!
# detect stroma
im = imread('../data/Rp042826d.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# detect stroma im = imread('../data/Rp042826d.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [15]:
Copied!
# segment the flowers
im = imread('../data/KaneFlowers.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# segment the flowers im = imread('../data/KaneFlowers.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [16]:
Copied!
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)
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)
/home/olivier/.conda/envs/py3/lib/python3.7/site-packages/skimage/morphology/_deprecated.py:5: skimage_deprecation: Function ``watershed`` is deprecated and will be removed in version 0.19. Use ``skimage.segmentation.watershed`` instead.
  def watershed(image, markers=None, connectivity=1, offset=None, mask=None,
Out[16]:
<matplotlib.image.AxesImage at 0x7f31b13565d0>
In [17]:
Copied!
rgb = imread('../data/4colors.JPG')

plt.figure(figsize=[20,20])
plt.imshow(rgb)
plt.colorbar();
rgb = imread('../data/4colors.JPG') plt.figure(figsize=[20,20]) plt.imshow(rgb) plt.colorbar();
In [18]:
Copied!
r = skr.median(rgb[:,:,0],disk(1))
plt.imshow(r,cmap=plt.cm.gray)
r = skr.median(rgb[:,:,0],disk(1)) plt.imshow(r,cmap=plt.cm.gray)
Out[18]:
<matplotlib.image.AxesImage at 0x7f31b1d95510>
In [19]:
Copied!
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()
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()
Out[19]:
<matplotlib.colorbar.Colorbar at 0x7f31b1fc8d10>
In [20]:
Copied!
lab = label(pth)

lut = np.arange(0,np.max(lab)+1)

plt.imshow(lab)
plt.colorbar()

mask = lab == 20
plt.imshow(mask)
lab = label(pth) lut = np.arange(0,np.max(lab)+1) plt.imshow(lab) plt.colorbar() mask = lab == 20 plt.imshow(mask)
Out[20]:
<matplotlib.image.AxesImage at 0x7f31b22cf310>
In [21]:
Copied!
from random import shuffle
shuffle(lut)
from random import shuffle shuffle(lut)
In [22]:
Copied!
shuffle(lut)
plt.imshow(lut[lab])
plt.colorbar()
shuffle(lut) plt.imshow(lut[lab]) plt.colorbar()
Out[22]:
<matplotlib.colorbar.Colorbar at 0x7f31b3e5af50>
In [ ]:
Copied!

In [23]:
Copied!
# segment the cell
im = imread('../data/exp0001crop.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# segment the cell im = imread('../data/exp0001crop.jpg') plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [24]:
Copied!
m = skr.median(im,disk(5))
plt.imshow(m,cmap=plt.cm.gray)
plt.colorbar()
m = skr.median(im,disk(5)) plt.imshow(m,cmap=plt.cm.gray) plt.colorbar()
Out[24]:
<matplotlib.colorbar.Colorbar at 0x7f31b1f866d0>
In [25]:
Copied!
th1 = m < 90

th2 = np.bitwise_and(110 > m,m < 130)

plt.imshow(th2)
th1 = m < 90 th2 = np.bitwise_and(110 > m,m < 130) plt.imshow(th2)
Out[25]:
<matplotlib.image.AxesImage at 0x7f31b21dda10>
In [26]:
Copied!
markers = label(th2)
plt.imshow(markers)
plt.colorbar()
markers = label(th2) plt.imshow(markers) plt.colorbar()
Out[26]:
<matplotlib.colorbar.Colorbar at 0x7f31b2498350>
In [27]:
Copied!
markers[markers==3] = 2
ws = watershed(im,markers)
markers[markers==3] = 2 ws = watershed(im,markers)
/home/olivier/.conda/envs/py3/lib/python3.7/site-packages/skimage/morphology/_deprecated.py:5: skimage_deprecation: Function ``watershed`` is deprecated and will be removed in version 0.19. Use ``skimage.segmentation.watershed`` instead.
  def watershed(image, markers=None, connectivity=1, offset=None, mask=None,
In [28]:
Copied!
plt.imshow(ws)
plt.imshow(mark_boundaries(im,ws))
plt.imshow(ws) plt.imshow(mark_boundaries(im,ws))
Out[28]:
<matplotlib.image.AxesImage at 0x7f31b238f290>
In [29]:
Copied!
# segment the cell
im = imread('../data/brain.jpg')[:,:,0]
plt.figure(figsize=(10,10))
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# segment the cell im = imread('../data/brain.jpg')[:,:,0] plt.figure(figsize=(10,10)) plt.imshow(im,cmap=plt.cm.gray) plt.colorbar();
In [30]:
Copied!
plt.hist(im.flatten(),255);
plt.hist(im.flatten(),255);
In [31]:
Copied!
from skimage.filters import threshold_otsu

t_otsu = threshold_otsu(im)
t_otsu
from skimage.filters import threshold_otsu t_otsu = threshold_otsu(im) t_otsu
Out[31]:
36
In [32]:
Copied!
th = im > t_otsu
plt.figure(figsize=(10,10))
plt.imshow(th)
th = im > t_otsu plt.figure(figsize=(10,10)) plt.imshow(th)
Out[32]:
<matplotlib.image.AxesImage at 0x7f31b1c7c790>
In [33]:
Copied!
lab = label(th,connectivity=1)
plt.imshow(lab)
lab = label(th,connectivity=1) plt.imshow(lab)
Out[33]:
<matplotlib.image.AxesImage at 0x7f31b1b29fd0>
In [34]:
Copied!
from skimage.measure import regionprops
from skimage.measure import regionprops
In [35]:
Copied!
props = regionprops(lab)

brain = (lab==7).astype(np.uint8)

pp = skr.maximum(brain,disk(3))
pp = skr.minimum(pp,disk(3))

plt.imshow(pp)
props = regionprops(lab) brain = (lab==7).astype(np.uint8) pp = skr.maximum(brain,disk(3)) pp = skr.minimum(pp,disk(3)) plt.imshow(pp)
Out[35]:
<matplotlib.image.AxesImage at 0x7f31b1aa9fd0>
In [36]:
Copied!
for p in props:
    print(p.area, p.label)
for p in props: print(p.area, p.label)
1459 1
5 2
1 3
3 4
1 5
16 6
6323 7
1 8
2 9
1 10
1 11
1 12
1 13
16 14
1 15
1 16
1 17
2 18
2 19
2 20
2 21
30 22
1 23
1 24
1 25
1 26
2 27
2 28
5 29
1 30
1 31
2 32
1 33
13 34
In [ ]:
Copied!

In [ ]:
Copied!

In [ ]:
Copied!


Creative Commons License maintained by Olivier Debeir and contributors.

Documentation built with MkDocs.

Search

From here you can search these documents. Enter your search terms below.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search