3
$\begingroup$

I have a matrix of data. I use

Colorize[Image[Rescale[data]],ColorFunction->"Rainbow"]

to create an image from the data where the value of each pixel is used to pick a specific colour in the gradient colour scheme "Rainbow".

Now I want, to create an image where the value of each pixel is used as the opacity for a fixed gradient colour scheme from left to right. So, for instance, I want the left of the image to be blue and the right of the image to be red (in a continuous fashion). Then, when the a particular value of the matrix is high, the colour should be bright and when the value is low it should be dark.

Thanks!

$\endgroup$
0

3 Answers 3

5
$\begingroup$
data = RandomReal[1, {30, 30}];

ImageMultiply[LinearGradientImage[{Blue, Red}, {30, 30}], Image[data]]

enter image description here

$\endgroup$
4
$\begingroup$

It's a little hard to be sure this is exactly what you are looking for, but at least it's a start. First, make the gradient image:

rainbowImg=Image[Colorize[Image[Rescale[Table[j, {i, 1, 100}, {j, 1, 100}]]], ColorFunction -> "Rainbow"]]

enter image description here

Then take the data image and use it as an alpha channel to allow it to specify the brightness of the resulting image at each pixel. Since I don't have access to your data, just make it random.

dataImg=Image[Colorize[Image[Rescale[RandomReal[{0, 1}, {100, 100}]]], ColorFunction -> "Rainbow"]]

enter image description here

black = Image[ConstantArray[0, {100, 100}]];
Show[black, SetAlphaChannel[rainbowImg, dataImg]]

enter image description here

Since you asked that when "a particular value of the matrix is high, the colour should be bright and when the value is low it should be dark." I placed it against a black background (otherwise the transparent pixels would have appeared white on the screen).

$\endgroup$
4
$\begingroup$

You can also use ColorCombine:

When combining a color image with a grayscale image, ColorCombine creates an image of the same color space with alpha channel.

f1 = With[{dt = #, max = Last@Dimensions[#]}, ColorCombine[
       {Image[MapIndexed[List @@ ColorData[{"Rainbow", {1, max}}][Last@#2] &, dt,{2}]],
        Image[dt]}, "RGB"]] &;

data = RandomReal[1, {10, 10}];
im1 = Image[f1@data, ImageSize -> 300]

enter image description here

Alternatively, you can use Append to manually add the alpha channel:

f2 = With[{dt = #, max = Last@Dimensions[dt]}, 
         Image[MapIndexed[Append[List @@ ColorData[{"Rainbow", {1, max}}][Last@#2], #1] &, 
               dt, {2}], 
         ColorSpace -> "RGB"]] &;
im2 = Image[f2@data, ImageSize -> 300];
Row[{im1, im2}, Spacer[15]]

enter image description here

Blending Blue and Red instead of using ColorData["Rainbow"]:

f1b = With[{dt = #, max = Last[Dimensions[#]]}, 
    ColorCombine[{Image[MapIndexed[List @@ Blend[{Blue, Red}, Last[#2]/max] &, 
        dt, {2}]], Image[dt]}, "RGB"]] &;
f2b = With[{dt = #, max = Last[Dimensions[#]]}, 
    Image[MapIndexed[Append[List @@ Blend[{Blue, Red}, Last[#2]/max], #1] &, 
      dt, {2}], ColorSpace -> "RGB"]] &;

im1b = Image[f1b@data, ImageSize -> 300];
im2b = Image[f2b@data, ImageSize -> 300];
Row[{im1b, im2b}, Spacer[15]]

enter image description here

$\endgroup$

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.