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');
imshow(Cxt/max(max(Cxt))); pause(1)
Finding Normalised Cross correlation map
h = ones(size(temp));
Cgg = conv2(x,h,'same');
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);
Thanx .............
ReplyDeleteYou're welcome! I'll be very happy to receive your comments and suggestions for further improvement of this blog.
Deletehi..i am getting an error in these lines Cxt = conv2(x,rot90(conj(temp)),'same'); % Cross correlation
ReplyDeleteimshow(Cxt/max(max(Cxt))); pause(1)
please help me
try to run all the lines of code at once. Also can you please quote the error?
Deletetried that but still returned the same error:
DeleteUndefined function 'conv2' for input arguments of type 'double' and attributes
'full 3d real'.
This error is occurred because one of your script files is named as 'conv2.m', rename it and your problem will be solved. To locate the file, run "which conv2" after you get the error. you'll see the address of the file as a response!
DeleteWarning: PNG library warning: Incorrect sBIT chunk length.
DeleteUndefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d real'.
Error in p1 (line 12)
Cxt = conv2(x,rot90(conj(temp)),'same'); % Cross correlation
Hi.. what is IDE or programming language for running this code? thanks
ReplyDeleteThe code presented in this post are primarily developed in MATLAB R2010a but can also be run in Octave or Scilab by installing proper toolbox like Image Processing Toolbox in Octave...
DeleteHi, how can I find the size of the object (if my template is not of the same size)
ReplyDeleteThis code is written with the assumption that the object size is same as that of the template. You may look into some machine learning algorithms like deep neural network for object detection.
ReplyDeleteWhy you used conv2 command for correlation ? and Please explain how this command perform correlation?
ReplyDeleteIf you look at the formula of correlation and convolution are almost same with one difference: Convolution needs second matrix to be flipped along rows and columns. the advantages of 'conv2' function is that we can pass the argument 'same' which will keep the resultant output size same as that of the input image. If you see the code, i have used 'rot90' on the second matrix i.e., template patch to compensate for the flipping done inside the conv2 function. 'rot90' performs the same task as that of 'fliplr(flipud(x))'.
Deletevery thanks, maybe you forgot that
ReplyDeletex = double(image)/255; -> this must be rgbtogray otherwise it's not running. so it must be like this x = double(rgb2gray(image))/255;
Thanks for your comment. Indeed it is required for color images. The image I have taken for the experiment is already in gray level format.
DeleteHow can we detect if template size is different from its size in image?
ReplyDelete