How to locate the the position of a 3D subimage in larger image in Matlab?
8 views (last 30 days)
Show older comments
I have a 3D subimage saved as X.mat , which is a subimage of the 3D image Y.mat. How can I find the position of this subimage in the image Y?
Can anyone help me?
Thanks,
0 Comments
Answers (2)
David Young
on 29 Jul 2014
Assuming you have used load to read the mat-files and the images are stored as X and Y, you can look for an exact match by checking each subimage of Y.
found = false;
for r = 1:size(Y,1)-size(X,1)+1
for c = 1:size(Y,2)-size(X,2)+1
for s = 1:size(Y,3)-size(X,3)+1
if isequal(Y(r:r+size(X,1)-1, c:c+size(X,2)-1, s:s+size(X,3)-1), X)
found = true;
break
end
end
if found
break
end
end
if found
break
end
end
if found
fprintf('Match found at: %d %d %d\n', r, c, s);
else
fprintf('No match found\n');
end
This reports the coordinates of the low-index corner of the match.
If the subimage is not an exact subimage, you need to use some other technique, such as normalised cross-correlation.
3 Comments
David Young
on 29 Jul 2014
I'm not sure you've understood my answer. What does it not do that you want to do?
Image Analyst
on 2 Aug 2014
It should find only one coordinate if it occurs just one time in the larger volume. (I didn't look over the code in detail but assumes it finds the upper left corner or something like that.) If it occurs twice it will find two . If you wanted a listing of all coordinates in the rectangular block at the one "found" location then you can to that by adding the width, length, and height to the one found coordinate (upper left corner). That will get you a whole "block" of coordinates.
Ahmet Cecen
on 29 Jul 2014
Depending on the size and type of data, it might be preferable to use a convolution or an erosion. I can't give a specific code snippet without further information.
1 Comment
Alex Taylor
on 4 Aug 2014
I just wanted to chain off of Ahmet that this is a 3-D template matching problem. David Young's solution is very helpful, and will work fine if you are truly looking for one and only one EXACT match in the larger volume. More typically when doing template matching, either the template or the larger image are not exact matches of one another (there are noise, illumination differences, etc.). In these cases, people usually turn to algorithms like normalized cross correlation and determine the position in the correlation matrix with the highest peak correlation. This method will work even when there isn't an exact match.
You might want to look at
normxcorr2
in IPT as an introduction to the 2-D template matching workflow. You could try to implement your own 3-D extension to normxcorr2 using
convn
or
fftn
(depending on your template image size either the spatial domain or the frequency domain will be faster).
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!