24
$\begingroup$

I have talk about how to catenate two images whose have same part totally in my this post.

But in this case two images have similar information, not totally same.

img1 = Import @ "https://i.stack.imgur.com/oc1L8.jpg"

img2 = Import @ "https://i.stack.imgur.com/GGTBv.jpg"

This is my expected result

If I use ImageAlign, I just can see the second image. How to catenate the first image?

img3 = ImageAlign[img1, img2];
ImageCompose[img3, {img1, .5}]

I can use ImageCorrespondingPoints to see the corresponding points

matches = ImageCorrespondingPoints[##] & @@ images;
MapThread[
  Show[#1, Graphics[{Red, MapIndexed[Inset[#2[[1]], #1] & , #2]}], 
    ImageSize -> 400] &, {images, matches}];
dim = ImageDimensions[First[images]];
pos = {First[matches], {First[dim], 0} + # & /@ 
    RescalingTransform[
      Transpose[{{0, 0}, ImageDimensions[Last[images]]}], 
      Transpose[{{0, 0}, ImageDimensions[First[images]]}]][
     Last[matches]]};
Show[ImageAssemble[{First[images], ImageResize[Last[images], dim]}], 
 Epilog -> {Thick, 
   Riffle[MapThread[Line@*List, pos], 
    Unevaluated[RandomColor[]], {1, -1, 2}]}, ImageSize -> 500]

But I don't know how to connect them.

$\endgroup$
  • 3
    $\begingroup$ This VERY well-studied problem is called "image stitching" or "image compositing." en.wikipedia.org/wiki/Image_stitching $\endgroup$ – David G. Stork Sep 6 '17 at 0:34
  • 1
    $\begingroup$ Possible duplicate of Can Mathematica recompose an image? $\endgroup$ – mattiav27 Sep 6 '17 at 8:02
  • 2
    $\begingroup$ @mattiav27 NoNoNo,please cancel the duplicate tag,because those image have same part in that post,but in my this post,this two images just have similar part. $\endgroup$ – yode Sep 6 '17 at 9:02
  • $\begingroup$ @Kuba Need your privilege here,if you agree with my comment upstair $\endgroup$ – yode Sep 6 '17 at 12:34
  • $\begingroup$ @yode in order to do what? $\endgroup$ – Kuba Sep 6 '17 at 17:00
26
$\begingroup$

Is this what you need?

padded = ImagePad[img1, {{#, #}, {#2, #2}} & @@ ImageDimensions@img2];
aligned = ImageAlign[padded, img2];


ImageCrop @ ImageCompose[padded, aligned]

enter image description here

Padding may be expensive for ImageAlign so if you know where it should fit you can pad from one or two sides not around.

$\endgroup$
  • $\begingroup$ Fun..it is a magic.I cannot imagine it can be solve by such simple method.. $\endgroup$ – yode Sep 5 '17 at 12:21
  • 1
    $\begingroup$ And there I was messing about with ImageCorrespondingPoints. $\endgroup$ – aardvark2012 Sep 5 '17 at 12:21
  • $\begingroup$ @aardvark2012 Don't blame yourself, I don't understand why this is not more straightforward. I mean, this is an obvious thing to want for two images, why would anyone want to crop the second one? $\endgroup$ – Kuba Sep 5 '17 at 12:23
29
$\begingroup$

Can supplement Kuba's nice answer with a geometric transform from the documentation.

 {w, h} = ImageDimensions[img2];
{e, tr} = FindGeometricTransform[img1, img2];
tmp = ImagePerspectiveTransformation[img2, tr, DataRange -> Full, 
   PlotRange -> {{0, First@tr[{w, 0}]}, {0, h}}];
ImageCompose[tmp, {img1, 1}, Round@({w, h}/2)]

yielding:

enter image description here

$\endgroup$
  • $\begingroup$ Neat and faster :) +1 $\endgroup$ – Kuba Sep 6 '17 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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