3
$\begingroup$

There are two images in this question. I want to overlay the img1 over img2 to see where they match and I want to do it manually:

img1 = Import["1.png"];
img2 = Import["2.png"];
Manipulate[
ImageCompose[img2, SetAlphaChannel[ColorNegate@img1, 0.5], {x, y}],
{{x, 0}, 0, 1000, 1}, {{y, 0}, 0, 1000, 1}]

This works for me, but I would like to align the two images by clicking and dragging one of the images over the other. How can I achieve this? I tried using a construct such as:

DynamicModule[{pt = {0,0}},
ClickPane[ImageCompose[img1,img2,pt], (pt=#)&
]
]

But it does not work, and even if it works, I have to click rather than drag one image over the other.

$\endgroup$
0

2 Answers 2

4
$\begingroup$

The interactive drawing tools of the frontend permits to do drag one image over an other.

img1 = Import["https://i.sstatic.net/biYj0.png"];
img2 = Import["https://i.sstatic.net/fLHMI.png"];

Overlay[{
  ImageCrop[img2, 2 ImageDimensions[img2], {1, -1}, Padding -> White]
  , SetAlphaChannel[ColorNegate@img1, 0.5]}
 , {1, 2}, 2]  

enter image description here

This demo uses only the left click of the mouse (on Windows at least).

EDIT

Until now, I didn't know any way to retrieve the position of the moved image programmaticaly (though some efforts).

I just have found a solution : it suffices to add Show @ before each image of the Overlay and, after moving the image, ask the options of each image :

img1 = Import["https://i.sstatic.net/biYj0.png"];
img2 = Import["https://i.sstatic.net/fLHMI.png"];

Overlay[{
  Show @ ImageCrop[img2, 2 ImageDimensions[img2], {1, -1}, 
    Padding -> White]
  , Show @ SetAlphaChannel[ColorNegate@img1, 0.5]}, {1, 2}, 2]

enter image description here

{ {PlotRange -> {{0, 160.}, {0, 550.}}}, {ImageMargins -> {{56.,0.}, {0., 192.}}, ImageSize -> {48.6727, Automatic},
PlotRange -> {{0, 80.}, {0, 275.}}} }

The relevant informations are ImageMargins and eventually ImageSize

$\endgroup$
1
$\begingroup$
img1 = Import["https://i.sstatic.net/biYj0.png"];
img2 = Import["https://i.sstatic.net/fLHMI.png"];

You can use LocatorPane with padded reference image as background and overlay image as locator:

background = Framed @ ImagePad[img2, 100, White];

appearance = Framed[SetAlphaChannel[ColorNegate @ img1, 0.5],
   FrameMargins -> 0, FrameStyle -> Red];

LocatorPane[ImageDimensions[img2]/2, background, Appearance -> appearance]

enter image description here

A few additional methods:

DynamicModule[{pt = ImageDimensions[img2] / 2}, 
 LocatorPane[Dynamic @ pt, Dynamic @ Framed @ 
    ImageCompose[ImagePad[img2, 100, White], 
        SetAlphaChannel[ImageMultiply[ColorNegate@img1, Yellow], .5], pt], 
    Appearance -> None]]

enter image description here

DynamicModule[{pt = ImageDimensions[img2]/2}, 
 Panel @ Show[ImagePad[img2, 100, White], 
   Epilog -> Dynamic @ {Locator[pt, 
       SetAlphaChannel[ImageMultiply[ColorNegate@img1, Yellow], .5], 
       ImageSize -> ImageDimensions[img1]]}]]

enter image description here

Manipulate[Panel @ ImageCompose[ImagePad[img2, 100, White], 
      SetAlphaChannel[ImageMultiply[ColorNegate @ img1, Yellow], .5], pt], 
 {{pt, ImageDimensions[img2] / 2}, Locator, Appearance -> None}, 
 AppearanceElements -> {}, Paneled -> False]

enter image description here

$\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.