In a previous question Karsten 7. proposed an elegant image-manipulation solution on how to overlay two images.

However in the following case the method does not properly work and needs refinement. Let me be more specific:

An image of real galaxy

enter image description here

And here is an image of a numerical simulation

enter image description here

As you can see, there is a problem with the first image. More precisely, the entire galaxy is slightly titled to the right.

Is there a way to fit the numerical simulation image on top of the real galaxy? The green and red arms should be rotated, stretched, moved and whatever is necessary, so as to fit exactly to the arms of the real galaxy.

The output should be something similar like this

enter image description here

Any suggestions?

  • $\begingroup$ @Kuba The problem is not mathematical! I simply want to fit the second image on top of the first. $\endgroup$ – Vaggelis_Z Aug 17 '16 at 16:19
  • $\begingroup$ @Kuba You are right! The entire galaxy is titled. Anyway, the simulation image should somehow be manipulated, so as the green and the red arms to fit exactly the real ones. $\endgroup$ – Vaggelis_Z Aug 17 '16 at 16:29
  • 8
    $\begingroup$ @Vaggelis_Z: "The problem is not mathematical! I simply want to fit the ..." Fitting is inherently a mathematical process, so I would consider this question far too vague as is. What would qualify as a good fit? What kind of transformation will you allow to which image in order to make the fit work? Can I just rearrange all the pixels int he second image to make the fit, or do you want it limited to stretching/rotation/scaling? $\endgroup$ – nben Aug 17 '16 at 17:16
  • 3
    $\begingroup$ @Vaggelis_Z Your edit doesn't answers any of my questions; it just plops different numerical data on a better initial galaxy image. This code will combine your two images if you have them loaded as img1 and img2: ImageAdd[img1, ImageApply[If[Min[#] > 0.5, {0, 0, 0, 0}, Append[#, 1]] &, img2]], but that particular fit looks awful to me. Is it good enough for you? If not, why not, and how should I evaluate whether a particular alignment of the images is 'good enough'? If you just want to overlay them by eye, I'd suggest using photoshop or the gimp. $\endgroup$ – nben Aug 17 '16 at 17:27
  • 4
    $\begingroup$ @Vaggelis_Z Unless you can formally define what you mean by "so as to fit the real arms" then Mathematica is probably the wrong tool for this. It sounds like you want to drag/stretch/rotate the simulation image to obtain a subjective alignment with the galaxy image (ie, you'll know it when you see it, but you can't describe the criteria formally); photoshop is great for this. If what you want is to find the affine transform of the second image that minimizes the sum of distances from all green/red pixels to the nearest white pixel, or something like that, Mathematica is a better tool. $\endgroup$ – nben Aug 17 '16 at 17:48

I think it might work to just add rotation to Karsten 7.'s previous answer that was cited in the question.

simulation = 
  With[{sim = Import["http://i.stack.imgur.com/3xNli.jpg"]}, 
    ColorReplace[sim, First @ DominantColors@sim -> Transparent]]

galaxy = Import["http://i.stack.imgur.com/qDYpd.jpg"]


DynamicModule[{galaxyW, galaxyH},
   {galaxyW, galaxyH} = ImageDimensions[galaxy];
       ImageRotate[ImageResize[simulation, {s1, s2}], θ °], Scaled[{p1, p2}]],
     {{p1, 0.5}, 0.2, 1., Appearance -> "Labeled"},
     {{p2, 0.5}, 0.2, 1., Appearance -> "Labeled"},
     {{s1, galaxyW/2}, 100, 50 + galaxyW, Appearance -> "Labeled"},
     {{s2, galaxyH/2}, 100, 20 + galaxyH, Appearance -> "Labeled"},
     {{θ, 0.}, -90., 90., 2. , Appearance -> "Labeled"}]]


The adjusted image and its parameters can be saved by choosing Paste Snapshot from the Manipulate's Autorun/Bookmark popup menu.

DynamicModule[{p1 = 0.5`, p2 = 0.51`, s1 = 535.`, s2 = 192.4`, θ = -20.`}, 
    ImageRotate[ImageResize[simulation, {s1, s2}], θ °], Scaled[{p1, p2}]]]


  • $\begingroup$ The output is simply great! Could you provide the exact values of p1, p2, s1, s2 and $\theta$ which you used for obtaining this result? $\endgroup$ – Vaggelis_Z Aug 18 '16 at 5:37
  • $\begingroup$ @Vaggelis_Z. I have made an update that shows how to save the adjusted image and its parameters. $\endgroup$ – m_goldberg Aug 18 '16 at 14:35
  • $\begingroup$ Yes, everything is great now. Many thanks! $\endgroup$ – Vaggelis_Z Aug 18 '16 at 14:44

Assuming this is just about making them visually match in an interactive way:

i1 = Import["http://i.stack.imgur.com/qDYpd.jpg"];
i2 = Import["http://i.stack.imgur.com/3xNli.jpg"];
i2 = ColorReplace[i2, First@DominantColors@i2 -> Transparent]
i3 = Image3D[{i2}];

 Show[{i3}, Prolog -> Inset[i1, shift], Background -> Black],
 {{shift, {0.5, 0.5}}, {0.3, 0.3}, {0.7, 0.7}}]


The Manipulate is only used to shift the galaxy a little bit. The simulation image can be rotated and resized with the mouse directly.

  • $\begingroup$ Is there a way to remove the white bounding box? $\endgroup$ – Vaggelis_Z Aug 18 '16 at 10:07
  • $\begingroup$ @Vaggelis_Z Yes, just add the option Boxed -> False to Show. $\endgroup$ – Karsten 7. Aug 18 '16 at 13:08

Assuming that all you really want/need is a visually good fit, you can apply the functions Translate, Rotate, and Scale to any Mathematica graphics of your simulated result, and then superimpose the two graphics using Show. If you wrap this into a manipulate environment, you can even adapt your simulation result interactively for a best visual fit. Looking up the documentation of the above three functions should be sufficient to get you started.

  • 2
    $\begingroup$ This is not an answer, it's just a comment. Answers provide the full solution to the problem. $\endgroup$ – Vaggelis_Z Aug 17 '16 at 18:46
  • 3
    $\begingroup$ @Vaggelis_Z I don't think the answer is particularly helpful either, but your question is so vague and unspecified that I don't think you can expect anything better than rough guidance like this. I'm flagging your question because I think that demanding that others post exact solutions to very-particular and not especially Mathematica-relevant problems for you, then down-voting them when they instead post help/guidance about the general case, borders on abuse of the forum; this is a learning and dialog site, not a do-your-homework-for-you site. $\endgroup$ – nben Aug 17 '16 at 19:09
  • $\begingroup$ @user21382: I agree, which is why I posted what I posted. $\endgroup$ – Pirx Aug 17 '16 at 19:35
  • 1
    $\begingroup$ @Vaggelis_Z: Thanks for your appreciation; I will keep this in mind for the future. $\endgroup$ – Pirx Aug 17 '16 at 19:36

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.