I would like to stitch together a pair of images. For example:


enter image description here

ImageAlign successfully registers these, but of course it only returns the part of img2 that overlaps with img1.

img2a = ImageAlign[img1, img2];
ImageCompose[img1, {Colorize[img2a], 0.25}]

enter image description here

To stitch the images together, I need to keep the entire area of img2. I can derive a transformation to align the two images, then apply it to img2:


enter image description here

But using PlotRange->All to show the entire image area appears to effectively un-transform the image so they no longer align.


enter image description here


enter image description here

How can I retain the entire non-overlapping area of both images when registering them?


1 Answer 1


You can use the result of FindGeometricTransform to adjust the PlotRange.
Starting with the same images:

imgOriginal = ImageTake[ExampleData[{"TestImage", "Aerial"}], {1, -2}, {2, -1}];
img1 = ImageTake[imgOriginal, {0, 160}];
img2 = ImageTake[imgOriginal, {-160, -1}];

Finding the transformation with

tr = FindGeometricTransform[img1, img2, TransformationClass -> "Translation"];

using it to tramsform img2

img2it = ImagePerspectiveTransformation[img2, tr[[2]], DataRange -> Full, 
 PlotRange -> Transpose[{tr[[2]][{0, 0}], ImageDimensions[img2]}], 
 Masking -> All, Background -> White]


and to position the overlay

ImageCompose[img2it, {Colorize[img1], 0.3}, 
  Round[(ImageDimensions[img2it] - tr[[2]][{1, 1}])/2]]


Comparing the result with the original image:

GraphicsRow[{%, imgOriginal}, ImageSize -> Large]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.