27

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.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
yode
  • 26,686
  • 4
  • 62
  • 167

2 Answers2

33

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

30

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Fun..it is a magic.I cannot imagine it can be solve by such simple method.. – yode Sep 05 '17 at 12:21
  • 1
    And there I was messing about with ImageCorrespondingPoints. – aardvark2012 Sep 05 '17 at 12:21
  • @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? – Kuba Sep 05 '17 at 12:23