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);


16 comments:

  1. Replies
    1. You're welcome! I'll be very happy to receive your comments and suggestions for further improvement of this blog.

      Delete
  2. hi..i am getting an error in these lines Cxt = conv2(x,rot90(conj(temp)),'same'); % Cross correlation
    imshow(Cxt/max(max(Cxt))); pause(1)
    please help me

    ReplyDelete
    Replies
    1. try to run all the lines of code at once. Also can you please quote the error?

      Delete
    2. tried that but still returned the same error:
      Undefined function 'conv2' for input arguments of type 'double' and attributes
      'full 3d real'.

      Delete
    3. 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!

      Delete
    4. Warning: PNG library warning: Incorrect sBIT chunk length.
      Undefined 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

      Delete
  3. Hi.. what is IDE or programming language for running this code? thanks

    ReplyDelete
    Replies
    1. The 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...

      Delete
  4. Hi, how can I find the size of the object (if my template is not of the same size)

    ReplyDelete
  5. This 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.

    ReplyDelete
  6. Why you used conv2 command for correlation ? and Please explain how this command perform correlation?

    ReplyDelete
    Replies
    1. If 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))'.

      Delete
  7. very thanks, maybe you forgot that
    x = double(image)/255; -> this must be rgbtogray otherwise it's not running. so it must be like this x = double(rgb2gray(image))/255;

    ReplyDelete
    Replies
    1. 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.

      Delete
  8. How can we detect if template size is different from its size in image?

    ReplyDelete