# How to keep the non-overlapping parts when aligning images?

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}];


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}]


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]


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]


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


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

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]