How to convert specific GrayScale into coloured pixel
Show older comments
Hello; I need your very helps about my project in University. I need to colorize metalographic images.
The original Image

I need to quantify the iron phases. I have converted the RGB image to GrayScale because I think is easier to define the regions.
This is the GrayScale one, and I need to convert the light phase into yellow, the almost black into green and the black (circles) into blue.
The aim of this is to quantify the percentual of each phase.
I have tried by RGB image using this code
%%
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get the Perlite mask
yellowMask =redChannel >= 130 & greenChannel >= 130 & blueChannel >= 130;
% Get the Ferrite mask
GreenMask = redChannel > 60 & greenChannel > 60 & blueChannel > 60 & redChannel < 130 & greenChannel < 130 & blueChannel < 130;
% Get the Graphite mask
BlueMask = redChannel <= 60 & greenChannel <= 60 & blueChannel <= 60;
% Make Perlit mask Yellow
redChannel(yellowMask) = 255;
greenChannel(yellowMask) =255;
blueChannel(yellowMask) = 0;
% Make the white mask Green
redChannel(GreenMask) = 0;
greenChannel(GreenMask) = 255;
blueChannel(GreenMask) = 0;
% Make the Graphite mask Blue
redChannel(BlueMask) = 0;
greenChannel(BlueMask) = 0;
blueChannel(BlueMask) = 255;
% Recombine separate color channels into a single, true color RGB image.
rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);
figure
imshow(rgbImage2)
The final result was not as good I've expected

Thanks very much in advance!
Daniel
7 Comments
Sri Kondapalli
on 26 Mar 2021
Can you define "good" in the final image?
I see that you ahieved what you described. Are you looking for gradiant color profiles instead of sharp colors?
DGM
on 27 Mar 2021
If what you're hoping for is a less-speckled image, perhaps you could try running medfilt2() before thresholding. Of course, what makes a pleasing image may not be what makes an accurate account of phase percentages.
You might also consider looking at the histogram and see if your breakpoints can be optimized a bit more.
imhist(my2Dgrayimage)
Image Analyst
on 27 Mar 2021
If you want, you can remove "holes" or "islands" smaller than some specified number of pixels, inside a larger region, though clever use of morphological functions like imfill, and and() or xor(), etc.
Like the others asked, what in your mind defines a good or not good colorized image? For example not pure colors, but blended with the gray scale image underneath, or what?????
Daniel Fernandes
on 1 Jun 2021
FWIW, you could do the same without color. What you're doing could be done with what is essentially a label or index image -- a grayscale image with discrete levels for each object/region of interest. The only reason you might need color to represent classes of things in an image is if certain parts of the image represent multiple classes simultaneously.
Your case has no regions multiply represented, and it should have no regions which have no representation. It can be reduced to a set of non-intersecting masks whose union spans the entire image. You could represent the same information with a grayscale image with only 3 discrete gray levels.
I'd throw down an example, but frankly I don't know how best to deal with the illumination variability caused by the reflectivity of the surface. Your thresholds are already pretty good for fixed thresholds.
If you want color for some aesthetic reason, that's your decision to make. I'm just saying it's not adding any more information than the binary masks give, and it only complicates processing.
If all you want is the area fractions then that should be easy enough. If as I mentioned, the masks are non-intersecting and their union spans the entire image, then the fraction would be
perlitefraction = sum(perlitemask(:))/numel(perlitemask);
for example.
Daniel Fernandes
on 1 Jun 2021
Daniel Fernandes
on 1 Jun 2021
Accepted Answer
More Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




