3
$\begingroup$

I have a sine plot and image, and I want to combine the two on 2D basis and 3D basis. I tried below:-

plotF = DensityPlot[Sin[8*x]*Sin[8*y], {x, 0, 1}, {y, 0, 1}];
image = ExampleData[{"TestImage", "Moon"}];
plotF3D = Plot3D[0, {x, 0, 1}, {y, 0, 1}, PlotRange -> {{0, 1}, {0, 1}, {0, 1}}, PlotStyle -> Texture[plotF], BoxRatios -> {1, 1, 1}];
plotImage3D = Plot3D[0, {x, 0, 1}, {y, 0, 1}, PlotRange -> {{0, 1}, {0, 1}, {0, 1}}, PlotStyle -> Texture[image], BoxRatios -> {1, 1, 1}];
combine2D = Show[plotF, image];
combine3D = Show[plotF3D, plotImage3D];
{{plotF, image, combine2D}, {plotF3D, plotImage3D, combine3D}} // Column

enter image description here

As you can see, both 2D and 3D basis failed. For 2D, the combined plot become a pure grey square. For 3D, the combined plot doesn't reflect the sine plot.

How can I get it done? Many thanks!


For the 2D case, I've tried this (which is similar to the output that I want):-

ImageAdd[ImageMultiply[plotF, 0.5], ImageMultiply[image, 0.5]]

and then I got this:-

enter image description here

But as you can see, the sizes of the plots are still unmatched...

$\endgroup$
  • $\begingroup$ What is the expected result? There are different ways of combining images. $\endgroup$ – C. E. Jun 17 '18 at 22:37
  • $\begingroup$ Hi, the expected result for the 2D case is to seeing both the "sine plot" and the image within the same plot range, mixing each other. For the 3D case, the output is similar, just at the botton of the plot box. $\endgroup$ – H42 Jun 17 '18 at 23:51
  • $\begingroup$ So, how should they mix with each other? Think of the sine plot as a matrix of pixel values, and the image as a matrix of pixel values. How do you take two corresponding pixels and produce a new pixel value? $\endgroup$ – C. E. Jun 18 '18 at 0:19
  • $\begingroup$ For the 2D case, is it possible to do something like ImageAdd[ImageMultiply[plotF, 0.5], ImageMultiply[image, 0.5]]? But I am unable to modify the size of the plots to make their sizes match... $\endgroup$ – H42 Jun 18 '18 at 1:22
  • $\begingroup$ I wrote up something, feel free to tell me if I'm missing the point. Maybe it is necessary to combine a plot and an image in your application. $\endgroup$ – C. E. Jun 18 '18 at 1:43
2
$\begingroup$

you mean something like this?

image = ExampleData[{"TestImage", "Moon"}];

plotF = DensityPlot[Sin[8*x]*Sin[8*y], {x, 0, 1}, {y, 0, 1}, 
   ImageSize -> ImageDimensions[image], Axes -> False, Frame -> None];
plotF3D = 
 Plot3D[0, {x, 0, 1}, {y, 0, 1}, 
  PlotRange -> {{0, 1}, {0, 1}, {0, 1}}, 
  PlotStyle -> 
   Texture[ImageAdd[ImageMultiply[plotF, 0.5], 
     ImageMultiply[image, 0.5]]], BoxRatios -> {1, 1, 1}, 
  Mesh -> None]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for reply. But there's still one problem. There's a margin and thus they are still not perfectly matched. I tried several approaches to resize any of them but still can't remove the margin. Thus there will be errors in value in the overlapped plot especially for some function much more precise than Sine. Is that possible to get rid of the margin as well? $\endgroup$ – H42 Jun 18 '18 at 17:08
3
$\begingroup$

Thinking about this as a plotting problem or an image processing problem, as in your comment, may not be the best approach for this situation. You might instead consider the image as a matrix of pixel values. You could do something like this:

{xdim, ydim} = ImageDimensions[image];
f[{x_, y_}] := Sin[8 (x - 1)/(xdim - 1)] Sin[8 (y - 1)/(ydim - 1)]
MapIndexed[0.5 # + 0.5 f[#2] &, ImageData[image], {2}] // Image

Mathematica graphics

If you want colors as well, then adapt 0.5 # + 0.5 f[#2] & to return a list with the values, one for each color channel. It can be done by passing the value to a color function given by ColorData.

$\endgroup$
  • $\begingroup$ also {xdim, ydim} = ImageDimensions[image]; f[a_, {x_, y_}] := 0.5 a + 0.5*Sin[8 (x - 1)/(xdim - 1)] Sin[8 (y - 1)/(ydim - 1)] ImageApplyIndexed[f[#1, #2] &, image] $\endgroup$ – Alucard Jun 18 '18 at 2:21

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.