MATLAB: Cropping 2 images together

Image Processing Toolboximcropnormxcorr2

My goal is to cut out similar regions from 2 images.
I’m using the example method shown in ‘normxcorr2()’ to find similar regions from two images, and am using ‘imcrop()’ to cut those areas.
What I want to ask is if there is a way to cut both images when the overlapping area doesn’t completely fit inside the larger image.
For example, I have 2 images ‘A’ and ‘B’ sized 1865*1200 and 904*670 respectively. And from the normxcorr2 function I get [xpeak=799, ypeak=846] and from that [xoffset=129, yoffset=-58]. This makes area [130 -57 670 904] to be the highest similarity, which is not inside ‘A’.
Is there a way to cut out the overlapping area from both ‘A’ and ‘B’ without the process of manually calculating the coordinates where they overlap?
Thank you in advance for your help.

Best Answer

Hello,
I had a similar issue and wrote the attached function Align2Images, which seems to work well in my case.
You can find attached as examples two slightly different images of same size image1 and image2, as well as a cropped version of image1.
The code is not particularly well written, but it does the job. Let me know if you have any questions.