What is the best approach to find a smaller matrix within a larger matrix (may not be identical, but need region if highest match)??
15 views (last 30 days)
Show older comments
I am looking to find where (and if) in a larger matrix does a smaller matrix lie? I am trying xcorr2, but it seems like sometimes it might return a lower correlation coefficient value in the correlation matrix for positions where there is an exact match, as opposed to some other locations. Eg, Larger matrix [0.1 0.2 0.3 0.4; 0.5 0.6 0.7 0.8; 0.9 1.0 1.1 1.2; 1.3 1.4 1.5 1.6] and smaller matrix [1.0 1.1; 1.4 1.5] returns 4,4 for the element with max value in the correlation matrix (consistent with its definition of the sum of the element-wise product), but I want the return to be 4,3 (location of exact match).
0 Comments
Answers (2)
Image Analyst
on 19 Jun 2021
You can't use corss correlation reliably to find a math. It's just a myth. See attached demo for proof. However, you can use normxcorr2() like DGM said, and I'm attaching another demo for that that works on a color image.
Alternatively you can scan the matrix getting a submatrix and using isequal() to compare the submatrix to the template matrix for perfect equality. That would probably be fine for microscopic-sized matrices like the examples you gave.
0 Comments
DGM
on 18 Jun 2021
This isn't really a good answer, since I can't offer an explanation of why it works differently, but it works if I do it like this:
a = [0.1 0.2 0.3 0.4; 0.5 0.6 0.7 0.8; 0.9 1.0 1.1 1.2; 1.3 1.4 1.5 1.6];
b = [1.0 1.1; 1.4 1.5];
c=normxcorr2(b,a)
[mx,idx] = max(c(:));
[idxr idxc] = ind2sub([5 5],idx)
Someone who has more experience with the task can probably offer more help.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!