# Find nearest crop of image within another image

ImageAlign[ref, i] works well when image i is fully contained within the reference image ref, but not so well when only a subregion of i appears in ref. For example:

r1 = ResourceFunction["RandomPhoto"][200];
r2 = ResourceFunction["RandomPhoto"][200];
i1 = ImageTake[r2, {120, 160}, {80, 180}];
i2 = ImageTake[r2, {90, 180}, {110, 140}];
ref = ImageCompose[r1, i1];
{ref, ImageAlign[ref, i2]}


How can I get ImageAlign to return the region at which they overlap:

ImageCompose[ref, {ImageApply[{.5, 1.5, .5}*# &, i2], .5}, {70, 70}]


I've tried using ImageCorrelate and ImageCorrespondingPoints but can't seem to get it quite right, but I'm sure there's an elegant solution here.

• I get a black image for some reason but I think you'll need something like this: crsp = ImageCorrespondingPoints[h, ref]; transform = Last@FindGeometricTransform[crsp[[1]], crsp[[2]], TransformationClass -> "Translation"]; ImageTransformation[ref, transform] – flinty Jun 2 '20 at 16:29
• @flinty Yes, but what I want is to find the rectangle where i2 and ref could intersect if moved and scaled. So you're right I'd want to look at ImageCorrespondingPoints[ref, i2], but unfortunately that is empty. – M.R. Jun 2 '20 at 19:38
• Random images are hard because they have so many keypoints, all of low quality. If your image really is a natural image surrounded by noise, I would suggest croping the image, either manually or by algorithm, allowing imageAlign to do its thing. – bill s Jun 2 '20 at 22:32
• Doesn’t need to be random, just replace RandomImage with ResourceFunction[“RandomPhoto”] – user5601 Jun 3 '20 at 0:02
• Correct @user5601 – M.R. Jun 3 '20 at 1:56