8
$\begingroup$

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

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

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:

ImageTransformation[img2,FindGeometricTransform[img2,img1,TransformationClass->"Rigid"][[2]],DataRange->Full]

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.

img2it=ImageTransformation[img2,FindGeometricTransform[img2,img1,TransformationClass->"Rigid"][[2]],DataRange->Full,PlotRange->All]

enter image description here

ImageCompose[img1,{Colorize[img2it],0.25}]

enter image description here

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

$\endgroup$

1 Answer 1

8
$\begingroup$

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]

img2it

and to position the overlay

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

composedImg

Comparing the result with the original image:

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

comparison

$\endgroup$

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.