cropping 2 images together

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.

7 Comments

as i understood you correctly
Red square is similar area you want to crop
part of the red square would be outside image 2 also but yes you are correct.
to be clearer the area would look like this, and I want to crop both 'A' and 'B'.
Can you please attach the images and code?
These are processed images A ,B and the code I'm using
I know these images are not that similar, but it shows a somewhat extream example of what I want to do.
I want to crop both A and B at the location of the rectangle, which would be the down right corner of A and the up left corner of B. And am hoping there would be a way to do so without manually calculating where to cut A and B in the process.
Thank you for your help.
You have white white background on each image. Did you try to get rid of it?
that would be because I saved it from matlab. original images do not have white backgrounds

Sign in to comment.

 Accepted Answer

Loïc Toraille
Loïc Toraille on 12 Mar 2020
Edited: Loïc Toraille on 12 Mar 2020
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.

1 Comment

Hello,
I just want to add: if you're looking to align two slightly displaced images of the same size rather than find the location of a small image inside a bigger one, you're reaching the limits of normxcorr2.
Thankfully, you can in this case use normxcorr2_general which was written by another user:
Hope this helps.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 26 Feb 2020
You can probably determine where the peak of the cross correlation is. Then place a cropping box around that but you'll need to clip the coordinates of the box if the sides of the box go outside the image. Then use imcrop() or indexing with that (potentially cropped) box.

3 Comments

Hoyong Yie
Hoyong Yie on 28 Feb 2020
Edited: Hoyong Yie on 28 Feb 2020
Maybe I wasn't clear in the question, I am already doing that
My question was is there a way to
1.'clip the coordinates'
2.crop both images where they overlap (where the box would be indicating)
without manual calculation for their coordinates.
It's no problem doing this for a few cases but it gets time consuming to do it over and over.
I said to "clip the coordinates" and "then use imcrop or indexing" and you said you're already doing that. OK, I didn't have a chance to look at your code yet - maybe this weekend. However you then said your question was about how to "clip the coordinates" and "crop", but you said you were already doing that. If you're already doing that, then why are they your questions??? So someone's confused (probably me).
From what I know, when you use normxcorr2, matlab slides the template through A to find where the correlation is the highest.
What the example form the matlab normxcorr2 page shows (which seems to be same as your demo) is how to locate the point of highest correlation, and input the templet size to show a rectangle where the templet 'would be' in A.
But when you use two different images to see whether they have similarities, sometimes the rectangle appears with a part of it outside A.
About the 'clipping the coordinates', I was being stupid and misstyping a dot for a comma (several times) and thinking that the imcrop not working must be because its cutting outside A, so I had to calculate how much is outside A to do the cropping properly.
But I think 'cropping both images' part is still a problem for me. What I wanted to do is to also crop the template to fit the rectangle inside A. The coordinate I get from normxcorr2 is based on A, so my guess is it doesn't apply to the template.
So my question is 'Is there a way to get the coordinates to crop the template from the rectangle, or do I have to use normxcorr2 again?'

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!