Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
small matrix to large matrix matching (correlation)

Subject: small matrix to large matrix matching (correlation)

From: tlawren

Date: 16 Dec, 2010 21:47:20

Message: 1 of 3

Greetings all. I'm new to the problem I am working on, so I'm sorry if my question is not clear.

I am trying to find the best match-up/alignment (correlation) of a small matrix within a larger matrix. (I've included an example below to better explain what I'm trying to do.) So far, I have been using xcorr2(A,B), but the matches are always thrown off. I looked at how xcorr2() calculates correlation coefficients and it appears that large values (products of large values) dominate the match. Can I use xcorr2() to solve this problem or do I need to use something else?

Example: Given A and B below, I want to find the location of A within B. Clearly the 2's should align, but xcorr2() gives the best correlation as being the 2 in A on top of the 5 in B.

A =
0 0 0
0 2 0
0 0 0

B =
0 0 0 0 0 0
0 5 0 0 2 0
0 0 0 0 0 0

Subject: small matrix to large matrix matching (correlation)

From: Sean de

Date: 16 Dec, 2010 22:04:20

Message: 2 of 3

"tlawren " <tlawren300@gmail.com> wrote in message <iee1d8$kp4$1@fred.mathworks.com>...
> Greetings all. I'm new to the problem I am working on, so I'm sorry if my question is not clear.
>
> I am trying to find the best match-up/alignment (correlation) of a small matrix within a larger matrix. (I've included an example below to better explain what I'm trying to do.) So far, I have been using xcorr2(A,B), but the matches are always thrown off. I looked at how xcorr2() calculates correlation coefficients and it appears that large values (products of large values) dominate the match. Can I use xcorr2() to solve this problem or do I need to use something else?
>
> Example: Given A and B below, I want to find the location of A within B. Clearly the 2's should align, but xcorr2() gives the best correlation as being the 2 in A on top of the 5 in B.
>
> A =
> 0 0 0
> 0 2 0
> 0 0 0
>
> B =
> 0 0 0 0 0 0
> 0 5 0 0 2 0
> 0 0 0 0 0 0

One way would be to use minimum absolute difference instead of the normalized x-correlation and a floating window. Here's an example for your 1-dimensional case above (it's 1 dimensional since we're only going to move A along B).

%Data
 A = [
 0 0 0
 0 2 0
 0 0 0];
 
 B =[
 0 0 0 0 0 0
 0 5 0 0 2 0
 0 0 0 0 0 0];

%Engine
idx_dim2 = 1;
ad_current = inf;
for ii = 1:size(B,2)-size(A,2)+1
    ad_new = sum(reshape(abs(B(:,ii:ii+size(A,2)-1)-A),[],1));
    if ad_new < ad_current
        ad_current = ad_new;
        idx_dim2 = ii;
    end
end

Bbest = B(:,idx_dim2:idx_dim2+size(A,2)-1)

Subject: small matrix to large matrix matching (correlation)

From: David Young

Date: 16 Dec, 2010 23:48:20

Message: 3 of 3

There are various possible measures you could experiment with, and which is best depends on your application. Minimum absolute difference is certainly a good one to try, but you should also look at the sum of the squared differences, which is a very common match quality measure.

Also, if you have the Image Processing Toolbox, look at using normxcorr2 rather than xcorr2. This will normalise by the local standard deviation; with your data you'll get two equal peaks, one on the 5 and one on the 2, which may or may not be what you want.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us