In [1]:
Copied!
%matplotlib inline
from IPython.display import HTML,Image,SVG,YouTubeVideo
%matplotlib inline
from IPython.display import HTML,Image,SVG,YouTubeVideo
Border based segmentation¶
We have seen that berder detection can be achieved by various filters, e.g. by convolution (linear filters) or by non linear filters such as morphological gradient.
The results of these filters is an image where pixels located near abrupt gray level changes have a high intensity whereas pixels located in more contiuous regions share a low value.
Here below, the result of a Sobel filter on the an image whith clearly identifiable objects:
In [2]:
Copied!
def norm_hist(ima):
hist,bins = np.histogram(ima.flatten(),range(256)) # histogram is computed on a 1D distribution --> flatten()
return 1.*hist/np.sum(hist) # normalized histogram
def display_hist(ima):
plt.figure(figsize=[10,5])
if ima.ndim == 2:
nh = norm_hist(ima)
else:
nh_r = norm_hist(ima[:,:,0])
nh_g = norm_hist(ima[:,:,1])
nh_b = norm_hist(ima[:,:,2])
# display the results
plt.subplot(1,2,1)
plt.imshow(ima,cmap=plt.cm.gray)
plt.subplot(1,2,2)
if ima.ndim == 2:
plt.plot(nh,label='hist.')
else:
plt.plot(nh_r,color='r',label='r')
plt.plot(nh_g,color='g',label='g')
plt.plot(nh_b,color='b',label='b')
plt.legend()
plt.xlabel('gray level');
def norm_hist(ima):
hist,bins = np.histogram(ima.flatten(),range(256)) # histogram is computed on a 1D distribution --> flatten()
return 1.*hist/np.sum(hist) # normalized histogram
def display_hist(ima):
plt.figure(figsize=[10,5])
if ima.ndim == 2:
nh = norm_hist(ima)
else:
nh_r = norm_hist(ima[:,:,0])
nh_g = norm_hist(ima[:,:,1])
nh_b = norm_hist(ima[:,:,2])
# display the results
plt.subplot(1,2,1)
plt.imshow(ima,cmap=plt.cm.gray)
plt.subplot(1,2,2)
if ima.ndim == 2:
plt.plot(nh,label='hist.')
else:
plt.plot(nh_r,color='r',label='r')
plt.plot(nh_g,color='g',label='g')
plt.plot(nh_b,color='b',label='b')
plt.legend()
plt.xlabel('gray level');
In [3]:
Copied!
from skimage.data import camera,coins
from skimage.filters import sobel
import matplotlib.pyplot as plt
import numpy as np
im = coins().astype(np.float)
plt.imshow(im,cmap=plt.cm.gray);
from skimage.data import camera,coins
from skimage.filters import sobel
import matplotlib.pyplot as plt
import numpy as np
im = coins().astype(np.float)
plt.imshow(im,cmap=plt.cm.gray);
In [4]:
Copied!
fsobel = sobel(im)
norm = 255*fsobel/np.max(fsobel)
display_hist(norm)
fsobel = sobel(im)
norm = 255*fsobel/np.max(fsobel)
display_hist(norm)
In order to separate object one can consider only very sharp borders, e.g. with $sobel>50$.
In [5]:
Copied!
borders = 255*(norm>50)
plt.imshow(borders,cmap=plt.cm.gray);
borders = 255*(norm>50)
plt.imshow(borders,cmap=plt.cm.gray);
In [6]:
Copied!
import skimage.filters.rank as skr
from skimage.morphology import disk
fim = skr.median(im.astype(np.uint8),disk(7))
fsobel = sobel(fim)
norm = 255*fsobel/np.max(fsobel)
plt.imshow(norm>30,cmap=plt.cm.gray);
import skimage.filters.rank as skr
from skimage.morphology import disk
fim = skr.median(im.astype(np.uint8),disk(7))
fsobel = sobel(fim)
norm = 255*fsobel/np.max(fsobel)
plt.imshow(norm>30,cmap=plt.cm.gray);
looking carefully to the obtained borders, we see that, depending on the threshold level choosen, some borders may be disconnected.
Questions:
- do you know a better edge detection that maybe help to solve this problem ?
In [ ]:
Copied!