1

I am trying to implement the 2D correlation algorithm to detect the position of an object in the image, i don't want to use any built in function estimates 2d correlation.

Here is my code:

I=imread('image.tif');      % image is a black image contains white letters.
h=imread('template.tif');   %template is a small image taken from the original image, it contains one white letter.
I=double(I);
h=double(h);
[nrows ncolumns]=size(I);
[nrows2 ncolumns2]=size(h);
C=zeros(nrows,ncolumns);

for u=1:(nrows-nrows2+1)
   for v=1:(ncolumns-ncolumns2+1)
       for x=1:nrows2
           for y=1:ncolumns2
               C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
           end
       end
  end
end

[maxC,ind] = max(C(:));
[m,n] = ind2sub(size(C),ind)   % the index represents the position of the letter.

output_image=(3.55/4).*C./100000;
imshow(uint8(output_image));

I think it is working! but it is very slow.

How can i replace the following code by a better code to speed up the algorithm?

   for x=1:nrows2
       for y=1:ncolumns2
           C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
       end
   end

I am thinking that in every time i have the following two matrices

h(1:nrows2,1:ncolumns2) and I(u:u+nrows2-1,v:v+ncolumns2-1)

another question, are there any improvements?

thanks.

HforHisham
  • 1,914
  • 1
  • 22
  • 34
  • You said, an object. can you try it with different objects in same image and then different quality/type of images? – bonCodigo Nov 24 '12 at 16:24
  • You said, an object. can you try it with different objects in same image and then different quality/type of images? I am not front of a machine now. But following link could give you some optimizing ideas. [link](http://stackoverflow.com/questions/8591515/how-do-i-detect-an-instance-of-an-object-in-an-image) PS: it is not great posting from mobile. I really want delete the first comment. – bonCodigo Nov 24 '12 at 16:32
  • the image is a simple black image contains white letters, and the template is a black small image contains one white letter, in the output image i found the max value at the letter in the template(i tried multiple templates) – HforHisham Nov 24 '12 at 16:34

3 Answers3

2

Whenever you can, try to use matrix ops. So try something like:

rowInds = (1:nrows2)-1;
colInds = (1:ncolumns2)-1;

temp = h.*I(u+rowInds,v+colInds);
C(u,v) = sum(temp(:));

Instead of:

for x=1:nrows2
    for y=1:ncolumns2
        C(u,v)=C(u,v)+(h(x,y)*I(u+x-1,v+y-1));
    end
end
Pete
  • 2,336
  • 2
  • 16
  • 23
  • My answer assumes you want to use nothing more than matrix ops and for-loops. If you can use FFT's, or convolution functions, use those, as others have said below. – Pete Nov 24 '12 at 17:53
0

Yes there are many improvements. You don't need a for loop at all. Since you do not want to use matlab's xcorr2 function, you can use conv2. See the answer I gave here.

Community
  • 1
  • 1
carandraug
  • 12,938
  • 1
  • 26
  • 38
0

How about determining the cross correlation in the Fourier domain, following the cross-correlation theorem? That should guarantee a dramatic speed increase.

Maurits
  • 2,082
  • 3
  • 28
  • 32
  • thank you i already applied it in the frequency domain(which is more fast), but i wanted to make it in the spatial domain. – HforHisham Nov 24 '12 at 20:19