TransWikia.com

How to drag an overlay image over the reference image?

Mathematica Asked on July 7, 2021

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.

2 Answers

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

img1 = Import["https://i.stack.imgur.com/biYj0.png"];
img2 = Import["https://i.stack.imgur.com/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.stack.imgur.com/biYj0.png"];
img2 = Import["https://i.stack.imgur.com/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

Answered by andre314 on July 7, 2021

img1 = Import["https://i.stack.imgur.com/biYj0.png"];
img2 = Import["https://i.stack.imgur.com/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

Answered by kglr on July 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP