I would like to use a set of two images with the same color scheme (img1 and img2) to produce a new image (img3) in which each pixel corresponds to the "higher pixel color value" of img1 and img2.

Imagine objects (e.g. disks) in img1 have the color col1:

col1= ColorData["TemperatureMap"][0.5]

In img2 the disks are at different positions (partly overlapping with img1), they should have the color col2:

col2= ColorData["TemperatureMap"][0.2]

For the resulting image img3 we should check if objects in img1 and img2 overlap there. If so, then the pixel gets the color col1 (because 0.5 > 0.2).

If at a pixel only one of the images has an object, then color remains the same. A scheme with only three colors might look like this:

  • blue AND red --> red
  • blue AND background --> blue
  • background AND red --> red
  • $\begingroup$ Higher color picture? On what color space? $\endgroup$ Commented Dec 11, 2014 at 22:53
  • $\begingroup$ I was nor precise, see above .. $\endgroup$
    – mrz
    Commented Dec 11, 2014 at 22:59
  • $\begingroup$ Let me ask more clearly: If img1 is RGB[.5,.1,.3] and img2 is RGB[.4,.2,.4]... what do you want as result? $\endgroup$ Commented Dec 11, 2014 at 23:05
  • 1
    $\begingroup$ @MilenkoRubin-Zuzic What you want is in general not possible, because the mapping value -> {r,g,b} which you use to create img1 and img2 has (in general) no inverse. What you can do is to calculate the max before you turned your gray values into colors, but this is what bill has already shown you in his answer. $\endgroup$
    – halirutan
    Commented Dec 12, 2014 at 4:32
  • $\begingroup$ @halirutan Have you seen my answer? I think it still has some problem, but looks good overall $\endgroup$ Commented Dec 12, 2014 at 18:46

2 Answers 2


Nice Problem.

Perhaps something like:

f[x_] := List @@ (ColorData["TemperatureMap"][x]);
s = Table[(f@x) -> x, {x, 0, 1, .001}];
k = Nearest@s;


c = DensityPlot[#, {x, 0, 1}, {y, 0, 1}, ColorFunction -> "TemperatureMap", Frame -> None, 
               PlotRangePadding -> 0, PlotPoints -> 100] & /@ {x, y};
m = Map[k, (List @@@ ImageData@#), {2}] & /@ c;
GraphicsRow[Join[c, {Image@Map[f, MapThread[Max, m, 2], {2}]}]]

Mathematica graphics

Although I think there is still a small problem in the interpolation in the middle of the max zones.


Yes, you can use ImageApply. For example, if img1 and img2 are two images of the same size, then

ImageApply[Max, {img1, img2}]

takes the max of each channel.

  • $\begingroup$ see changes above ... $\endgroup$
    – mrz
    Commented Dec 11, 2014 at 23:29
  • 2
    $\begingroup$ Define the map/function you want to use and place it in ImageApply instead of Max. $\endgroup$
    – bill s
    Commented Dec 11, 2014 at 23:56
  • $\begingroup$ I'm not sure. Is it so easy? $\endgroup$ Commented Dec 16, 2014 at 13:01

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.