Thursday, 23 January 2014

Example of Opening by reconstruction in scikit-image

This blog is a continuation of the last blog that I have written. The aim is to give you potential of the mathematical morphology (MM) using ski-image and their application.  MM is branch of image processing which has wide application in many diverse field. Erosion and dilation are basic operators of MM which can be combined in different way to form many different powerful MM operators. MM was devised for binary images but nowadays it has been extended to grayscale images as well.

Here I would show you application of erosion and opening by reconstruction to do some image processing. For definition of erosion and opening by reconstruction please follow any image processing books or look in the web there are many online tutorials.

So we have an image. Let us assume that out the different blobs that we have we want to have a blob which should have a minimum length of 200 pixels in horizontal direction. Such expert knowledge is handy in many images processing domain to improve the image analysis. E.g building cannot be less than certain width, road should be of certain width, bridge should be of certain width etc. in this case lets assume, the shown blobs are result of some building detection algorithm. Now we want to improve the result by incorporating expert knowledge using MM.


So first we want to erode the image with linear structural element of length 200 pixels and whatever is remaining after that, we want to recover original shape of the blobs which was distorted by using erosion. For that purpose we use opening by reconstruction. Here are the codes, enjoy and dont forget to play around with python.


# -*- coding: utf-8 -*-
Created on Mon Nov 18 15:42:21 2013
@author: shailesh
% call necessary libraries
import math
import matplotlib.pyplot as plt
import numpy as np
from skimage.draw import ellipse
from skimage.draw import polygon
from skimage.draw import circle
from skimage.morphology import label
from skimage.measure import regionprops
from skimage.transform import rotate
import matplotlib.patches as mpatches
import skimage.morphology as MM
% draw some arbitary shapes
image = np.zeros((1000, 1000),dtype=uint8)
% create an ellipse with centre (350,350) with minor axis = 100 and major = 220
rr, cc = ellipse(300, 750, 100, 220)
image[rr,cc] = 1
% create a polygon
x = np.array([1, 7, 4, 1])
x = np.array([1, 70, 40, 1])
y = np.array([1, 20, 80, 1])
rr, cc = polygon(y, x)
image[rr, cc] = 1
% create a polygon
rr, cc = circle(200, 200, 50)
image[rr, cc] = 1
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
rr, cc = ellipse(500, 800, 30, 40)
image[rr, cc] = 1
#lable connected regions
label_img = label(image)
#find properties of connected regions
regions = regionprops(label_img,['Centroid','Area','BoundingBox'])
%loop through connected regions and find
% Centroid', 'BoundingBox', 'Area' for each blobs
i= 0
NOS= len(np.unique(label_img))-1
X = np.zeros((1,NOS))
Y = np.zeros((1,NOS))
for props in regions:
y0, x0 = props['Centroid']
print x0, y0
%show figure
plt.axis((0, 1000, 1000, 0))
plt.title('original image')
%perform erosion with linear structuring element
SE= np.ones((1,200))
imageE = MM.binary_erosion(image, SE)
plt.title('Eroded image')
%perform opening by reconstruction to recover blob
imageRECON = MM.reconstruction(imageE, image)
plt.title('Reconstructed image')

Morphological reconstruction is a very powerful method. People have use it for many different applications such as watershed delieanation, filtering, change detection of building after earthquakes, building detection, bridge detection etc. For my work also, i have used the technique for unsupervised change detection using high resolution images and change detection of high rise buildings.

No comments:

Post a Comment