Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!s9g2000yqm.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: divide 1280-by-960 image into 9-by-9
Date: Thu, 26 May 2011 19:25:57 -0700 (PDT)
Organization: http://groups.google.com
Lines: 143
Message-ID: <5d9de233-1504-487b-9663-61aea7de2bf8@s9g2000yqm.googlegroups.com>
References: <irm4h1$jke$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: 184.57.134.178
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: posting.google.com 1306463157 19829 127.0.0.1 (27 May 2011 02:25:57 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 27 May 2011 02:25:57 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: s9g2000yqm.googlegroups.com; posting-host=184.57.134.178; posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: HUALESNKRC
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1,gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:728951

areeba khan:
Cross correlation is not a reliable way to do pattern matching.  You
think that correlating your template over your image will give a spike
where your template is located in your main image.  I know you think
that and I know you've been told that, and so was I, . . . and I even
thought that until I learned that it is not true.  Heck, even the
MATLAB help gives this incorrect statement:  "xcorr2 is the two-
dimensional version of xcorr. It has its maximum value when the two
matrices are aligned so that they are shaped as similarly as
possible."  That is not true in general, only for certain cases.  The
code below demonstrates a case where it's not true:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Script to demonstrate that the max value of a cross correlation
% is not always where the template is located.
clc;
clear all;
workspace;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
	cd(fileparts(which(mfilename)));
end

% Check that user has the Signal Processing Toolbox installed.
hasRequiredToolbox = license('test', 'signal_toolbox');
if ~hasRequiredToolbox
	% User does not have the toolbox installed.
	message = sprintf('Sorry, but you do not seem to have the Signal
Processing Toolbox.\nDo you want to try to continue anyway?');
	reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
	if strcmpi(reply, 'No')
		% User said No, so exit.
		return;
	end
end

% Define image 1
I1 = [1   2   3 100 100
     11   5   8 100 100
      4   6  13 100 100
     10  12  15 100 100
    100 100 100 100 100]

%Define image 2 = I1(2:4, 1:3)
I2 = [11  5  8
       4  6 13
      10 12 15]

% First do the regular cross correlation.
c = xcorr2(I1, I2)

% I2 is aligned with I1 at row 4, column 3
% of the cross correlation matrix.
% Remember the cross correlation matrix is bigger than either one.
% Let's see if the max is really at row 4, column 3,
% where you think it should be.
[maxRow maxCol] = find(c == max(c(:)))
% Nope, it's not.  Hmmmmm.....

% OK that didn't turn out as expected.
% So now let's try "normalized" cross correlation.
% Normalize each image.
i1n = (I1 - mean2(I1)) / std(I1(:))
i2n = (I2 - mean2(I2)) / std(I2(:))
% Now do the cross correlation with the normalized versions.
cn = xcorr2(i1n, i2n)
% See if it is max at the aligned spot.
[maxRow maxCol] = find(cn == max(cn(:)))
% Nope, it's not either.
% What have we learned here?

====================================
I1 =
     1     2     3   100   100
    11     5     8   100   100
     4     6    13   100   100
    10    12    15   100   100
   100   100   100   100   100


I2 =
    11     5     8
     4     6    13
    10    12    15


c =
          15          42          79        1556        2730
2200        1000
         178         239         345        2972        4692
3200        1400
         211         290         530        3921        6095
4800        2500
         290         497         900        4097        6190
4800        2500
        1662        2984        4185        6069        7103
4800        2500
        1380        2046        2590        3307        3765
2600        1500
         800        1300        2400        2400        2400
1600        1100

maxRow =
     5

maxCol =
     5

i1n =
   -1.1550   -1.1339   -1.1127    0.9393    0.9393
   -0.9435   -1.0704   -1.0070    0.9393    0.9393
   -1.0916   -1.0493   -0.9012    0.9393    0.9393
   -0.9646   -0.9223   -0.8589    0.9393    0.9393
    0.9393    0.9393    0.9393    0.9393    0.9393


i2n =
    0.4377   -1.1380   -0.3502
   -1.4006   -0.8754    0.9629
    0.1751    0.7003    1.4881


cn =
   -1.7189   -2.4962   -2.6522    0.4200    1.8607    0.8222    0.1644
   -2.5162   -2.3344   -0.8744    3.9718    3.5199   -1.3155   -1.1511
   -2.1285   -0.8192    0.1963    4.3092    1.5055   -1.9733   -0.7400
   -2.1562   -0.6544    0.6444    4.1462    1.4111   -1.9733   -0.7400
    0.8511    3.6214    4.5832    5.4054    1.7129   -1.9733   -0.7400
    1.2422    1.5029   -0.3052   -0.9885   -3.0070   -2.7955   -0.9044
   -0.3289   -1.3977   -0.9866   -0.9866   -0.9866   -0.6578    0.4111

maxRow =
     5

maxCol =
     4