MATLAB Answers


Colorizing grayscale images with tones of specified colors...

Asked by Abarairenji on 18 Jul 2012
Latest activity Commented on by Image Analyst
on 26 Jan 2017
Hello; I need your very helps about my project in University. I need to colorize X-ray images. I have writed a simple code about it. But i have several problems. Since we do not have a good x-gun, every one of the images are different from one another,I need to colorize these images using 4 colors i mean, there are 4 different color regions. Red and its tones for for example low valued pixels; yellow and its tones for another pixel value region and so on.. But i do not know how can i distribute tones of a color to different pixel values :( I can just colorize specified pixel value regions with one,two,three ( i mean countable) tones. And i need to optimize each picture i mean same materials must be in same color region but since we do not have a good environment, for even same material, pixel values can be different from each other in two taken images.
I can send you the code i wrote but i think its useless.Since that gui can only colorize specified 3 pixel regions i mean for example a color for values between 0-100 and another one for 100-155 and another one for 155-255 ( just for example )
Here is the link for the code i wrote:
I need your advices on how can i solve these 2 problems. Please help me ...
Sincerely Abarairenji


Show 1 older comment
Also, the X-ray images that you provided us with are 8-bit grayscale JPEGs, thus there is only 255 distinct pixel values that can be achieved. However, I wouldn't be surprised if the original X-ray images were collected at a higher bit depth (e.g. 12-bit or 16-bit). If you can acquire those images, you might be able to get better resolution in your colorations. For example, DICOM images can be read using Matlab's dicomread() function.
Hello Matt, Thank you for your help. I can say that i may collect images at 12 or 16 bit i guess. I dont know what are dicom images.About the colorization; For example in a x image, i want to colorize most dark regions (for example between 0-50) with the continious tones of blue,dark regions with the tones of red (for example 50-75), middle intensity regions with continious tones of brown (for ex. 75-155),less dense regions with green and lessers with yellows and its continous tones...
Did you scroll down far enough to see my answer below? Where I recommended ind2rgb() or colormap()?

Sign in to comment.

2 Answers

Answer by Image Analyst
on 18 Jul 2012
Edited by Image Analyst
on 19 Jul 2012
 Accepted Answer

You can use a colormap to change how the image looks, or you can create a colormap and apply it to the image with ind2rgb() to get an RGB image out.
That will let you somewhat arbitrarily colorize your images. But if you want the color to correspond to the actual physical real-world mass of your objects, then that's a different story. Say you want red to be where your sample density is 20 grams per square centimeter, but that can happen at 50 gray levels for some images and 170 gray levels in other images because you don't have control over your exposure (dosage). In that case, your only hope is if you have similar images (say, all chest x-rays) and you can assume that the intensity values (the histograms) should all be the same. In that case, you can use my Histogram shaping code in my File Exchange ( to match all your images histograms to one master image. If your x-ray source varies in its dosage, and your samples also vary (say, you have mice in one image and truck engine crankcases in another) then you're out of luck, unless you have a standard embedded in the image such as a aluminum step wedge.


If you need to show data on top of the image, you can show the image and call "hold on" then plot. For example:
figure(1),imshow(CoolColorizedImage), hold on
plot(x,y), hold off
% Now Figure 1 has your data on top of the cool, colorized grayscale image.
I don't understand why you can't simply use imshow() to display your image and then use colormap() to apply some colormap. It does work. Maybe though you need different colormaps on different axes. In that case, read this:;jsessionid=ffe53d91f96d08d777ac29499393
Thank you a lot (: It worked. If i can do anything for you, please remind me Mr.

Sign in to comment.

Answer by obaldia on 26 Jan 2017

this has already been answered, but there is no solution for changing the tone of the image to an arbitrary color and not temper with the colormaps.
To change a grayscale image to a tone-scale one could use the HSV color space.
[im ~] = imread('cameraman.tif'); %im is a one dimensional grayscale image
[imRGB] = cat(3, im, im, im); %convert grayscale image to RGB
[imHSV] = rgb2hsv(imRGB);
Now we just have to change the "hue" of the HSV color space to a specific color. For example, petrol. Petrol has a chroma (hue) of 163°
imHSV(:,:,1) = 163./360; %Change the "hue" of the image, norm to 360 degrees
imHSV(:,:,2) = 1 - imHSV(:,:,2) %invert the saturation axis
The result is a petrolscale image!
res = hsv2rgb(imHSV);

  1 Comment

I'm attaching a related program, in case anyone is interested. It increases the saturation (color vividness/purity) for a particular color range in the image (e.g. blue is "enhanced") and other colors remain the same.

Sign in to comment.