Change pixel color and intensity independently in an image

11 views (last 30 days)
I have a matrix (ratioImage) of class double that I am plotting as a color image. So I get an image with different colors indicating the different values of pixels in my matrix using imshow and colormap('jet'). Now, I would like to vary the pixel intensity (brightness) according to the values of the pixels contained in another matrix (Nphotons). I want to keep the same colors indicating the values in ratioImage, but I want to adjust the brightness of each pixel according to the values in Nphotons. Is it possible to do that with Matlab? Any clue on how to start this?
Thank you!

Answers (2)

Johannes Fischer
Johannes Fischer on 25 Jun 2020
Here is how i would try to do it:
% First, you need to get the image that you see as an RGB-matrix. If your
% ratioImage is an NxM matrix, you want a NxMx3 matrix where the last
% diemsion stores the three color channels red, green and blue.
rgbMat = ind2rgb(ratioImage, jet(256));
% in order to change the brightness, you need to convert these values into
% another color-space, L*a*b. The L*a*b color-space does not have 3 color
% channels (like RGB) but one channel for Lightness and two for color.
labMat = rgb2lab(rgbMat);
% Now you can change the values in the first channel of labMat to adjust
% the brightness.
labMat(:, :, 1) =
% after you are done, convert back to RGB and display
rgbMat = lab2rgb(labMat);
imshow(rgbMat)
On a different note:
Many people object to the use of the colormap jet. It produces very colorful images, but at the cost accurate interpretation of your data, because the contrast is not uniform. Hence, a couple of years ago matlab introduced a new (default) colormap 'parula'. Steve Eddins wrote about their rationale in these blog posts:
But since you additionally change the brighntess depending on values in another matrix, these considerations are maybe not necessary your case.
  1 Comment
Lucile M
Lucile M on 14 Jul 2020
Thank you for your answer. I understand the point but it does not really do what I need. The values in the matrix are completely modified when converting back to RBG, and when plotting the pattern does not tell anything anymore. It is still helpful information though! I'll try a bit further and come back here if I succeed at some point.

Sign in to comment.


Image Analyst
Image Analyst on 25 Jun 2020
If you use caxis() you can basically "pin" the ends of your jet colormap to whatever values you want. So if your original image range was 0-2 and you had 0 blue and 2 be yellow because you used jet(256), and then you wanted to raise the values so that now your range is 1-3 but you still wanted 2 to be yellow, you could set caxis([0,3]) after each time you called imshow() or image(). If you do that, 0 would be blue, 2 would be yellow, and 3 would be red regardless of the values present in the image. If you don't then the colormap will adjust automatically so that the min intensity value (regardless of its absolute value) will be blue and the max intensity value (regardless of its absolute value) will be red and the images would look the same even though you raised the values of the image pixels.
  2 Comments
Lucile M
Lucile M on 14 Jul 2020
Thank you for this tip! I don't want to play on colors though, but on intensities (which I'm not sure is possible in matlab)
Image Analyst
Image Analyst on 14 Jul 2020
Instead of jet(), you can use a colormap called gray() that has no colors, just grayscale intensities.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!