Wednesday, April 18, 2012

Morphological Operations

Morphological Operations: Part I

Morphological Operations

This program will demonstrate the various morphological operations used in image processing. For further information please read: Gonzalez, Rafael C, Digital image processing - 3rd ed. - New Delhi Pearson Education 2009

Contents

Start

clear all;
close all;
clc;

Read the image

im = imread('coins.png');
figure; imshow(im);title('original Image');

Thresholding

this step will convert the given gray cycle image into binary image
x = im>128;
figure;imshow(x); title('Binary Image');


Defining Structuring Element

In mathematical morphology, a structuring element (s.e.) is a shape, used to probe or interact with a given image, with the purpose of drawing conclusions on how this shape fits or misses the shapes in the image. It is typically used in morphological operations, such as dilation, erosion, opening, and closing, as well as the hit-or-miss transform. (Source: Wikipedia)
Smat1 = [0 1 0; 1 1 1; 0 1 0];
Smat2 = [1 1 1; 1 0 1; 1 1 1];
SE = strel(Smat1);
figure;
subplot 121;  imshow(Smat1); title('Element1');
subplot 122;  imshow(Smat2); title('Element2');


Image Dilation

The dilation operation usually uses a structuring element for probing and expanding the shapes contained in the input image.
xdilate = imdilate(x,SE);
figure;imshow(xdilate); title('Dilated Image');


Image Erosion

Visit here: http://homepages.inf.ed.ac.uk/rbf/HIPR2/erode.htm
xerode = imerode(x,SE);
figure;imshow(xerode); title('Eroded Image');


Closing Operation

Opening operation is same as close(A,B) = erode(dilate(A,B),B) It removes noise and irregularities inside the object
xclose = imerode(imdilate(x,SE),SE);
figure;imshow(xclose); title('Closed Image');


Opening Operation

Opening operation is same as open(A,B) = dilate(erode(A,B),B) It removes background noise as well as small objects from the scene
xopen = imdilate(imerode(x,SE),SE);
figure;imshow(xopen); title('Opened Image');


Region Filling

Visit below link for more information http://www.cis.rit.edu/class/simg782/lectures/lecture_04/lec782_05_04.pdf
B=[0,1,0;1,1,1;0,1,0];
maxit = 1000;
x = xclose;
sa=size(x);
X=zeros(sa(1),sa(2));
[m start] = min(min(x==1));
X(start)=1;
Y=zeros(sa(1),sa(2));
xc= ~x;
count = 1;
while ((min(X ~= Y) == 0) & (count < maxit))
    count=count+1;
    Y=X;
    X=imdilate(Y,B) & xc;
end
figure;imshow(X); title('Region Filling');

Boundary Detection

we can detect boundaries of the objects using this technique boundary = A - (A eroded by SE);
xhat = imerode(X,SE);
boundary = ~(X-xhat);
figure; imshow(boundary); title('Boundary Detection');


(Part II: Coming soon...)

Saturday, April 14, 2012

Template matching using Normalized Cross Correlation

Template matching using Normalised Cross Correlation

Template matching using Normalized Cross Correlation

This program demonstrate the implementation of conventional cross correlation and normalized cross correlation metric to find the similarity score between template and the image portion.This program can be used for image registration to align the given images according to correlated pixels. Also this image demonstrate the use of correlation techniques to match the object. This method is very crude that it may fail most of the time if the object selected to find is a bright image.

Contents

Start

clear all;
close all;
clc;

Read the image

image = imread('dollar.tif');
x = double(image)/255;
imshow(x); pause(1);


Read the Template

template = imread('temp.tif');
temp = double(rgb2gray(template))/255;
imshow(temp); pause(1);


Finding correlation map

Cxt = conv2(x,rot90(conj(temp)),'same'); % Cross correlation
imshow(Cxt/max(max(Cxt))); pause(1)


Finding Normalised Cross correlation map

h = ones(size(temp));
Cgg = conv2(x,h,'same'); % finding the energy of signal
NCC = Cxt./Cgg;
imshow(NCC/max(max(NCC))); pause(0.5);


Display the object in image

Locate the maxima [m,n] from the figure and then display the box around the object
[a b] = max(max(NCC));
[c d] = max(NCC);
m = d(b);
n = b;
clear a b c d;
[a b] = size(x);
[c d] = size(temp);
c = round(c/2);
for i=1:a
    for j=1:b
        if (i==m-c && j>n-c && j<n+c)||(i==m+c && j>n-c && j<n+c)...
                || (j==n-c && i>m-c && i<m+c)||(j==n+c && i>m-c && i<m+c)
            x(i,j) = 0;
            x(i+1,j+1) = 0;
        end
    end
end
imshow(x);


Monday, April 2, 2012

Connected Component Labelling

Connected component Labelling

Connected component Labelling

This program demonstrate the segmentation process by classifying the connected pixels in the image. This program can be used for automatic coins counting machine, if images of coins taken with Black background and the coins are places at a safe distance. to try this, take an image of coins placing them on black surface and use this image as input for this program

Contents

Start

clear all;
close all;
clc;

Read the Image

I = rgb2gray(imread('coin.jpg')); % read graycycle image
title('Original Image');
imshow(I);


Convert it to binary image

ima = I>128; %Thresholding
ima = bwareaopen(ima,30); %Neglect the connected region <30 pixels
figure; imshow(ima);


Connected Component Labelling Algorithm

[a b] = size(ima);
label = 0;
D = [];
for i=1:a
    for j = 1:b
        if ima(i,j) == 0
            Im(i,j) = 0;
        else
            switch ima(i-1,j)+10*ima(i, j-1)
                case 0
                    label = label+1;
                    Im(i,j) = label;
                case 1
                    Im(i,j) = Im(i-1, j);
                case 10
                    Im(i,j) = Im(i ,j-1);
                case 11
                    if Im(i-1,j) == Im(i, j-1)
                        Im(i,j) = Im(i, j-1);
                    else
                        Im(i,j) = Im(i-1, j);
                        Im(Im == Im(i-1,j)) = Im(i, j-1);
                    end
            end
        end
    end
end

Label Conflicts Compensation

for i = 1:2:length(D)
    m = minmax(D(i:i+1));
    Im(Im==m(2)) = m(1);
    d((i+1)/2) = D(i);
end

Renaming the labels

label = 1;
for i = 1:max(max(Im))
    if sum(sum(Im==i))>10
        Im(Im==i) = label;
        label = label+1;
    end
end

Display of the results

figure;imshow(10*uint8(Im));
t = ['Number of connected regions = ' num2str(max(max(Im)))];
title(t);
disp(['Number of coins in the image are: ', num2str(max(max(Im)))]);
Number of coins in the image are: 9