I would like to know how to convert a binary image to a pseudo color image based on closed regions

 Accepted Answer

Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'image.png';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
% Get a mask:
binaryImage = grayImage < 128;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
impixelinfo;
axis('on', 'image');
title('Mask/Binary/Logical Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
% Display the image.
subplot(2, 2, 3);
imshow(labeledImage, []);
impixelinfo;
axis('on', 'image');
title('Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'jet', [0,0,.5]);
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
impixelinfo;
axis('on', 'image');
title('Colorized Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
[EDIT] Just a note to say that while this may look pretty, it's not very useful because you cannot tell if nearby blobs are connected because their colors are so similar. You'd be better off with shuffling the colors like this:
coloredLabelsImage = label2rgb (labeledImage, 'jet', 'k', 'shuffle');

More Answers (3)

sir,please check the follow code to get some information
clc; clear all; close all;
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/777933/image.png');
J = im2bw(I);
J = ~J;
[L,num] = bwlabel(J);
figure; imagesc(L);axis equal; axis off;colormap jet;

2 Comments

Hi yanqui liu
Thank you for the help. I think the imagesc command is the reson why the circumference of each cell in the pusedo color image is approximated.
Sure, I will go through the book.
Thank you

Sign in to comment.

Thank you image analyist for the your answer and the code. I will try to grasp your code and run it, and will let you know my comments.
Neverthelss, thank you for the immediate response.
Hey thanks a lot again. I really appreciate your help. I just have some questions regarding your code.
1st one
from your code "binaryImage = grayImage < 128;"
how does this work.

10 Comments

You gave us a gray scale image with values in the range 0-255 so I had to binarize it to turn it into a binary image with values of true or false (1 or 0). I did this by taking all values above 128 and setting them to 1.
When you do this
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
the number of blobs is counted and returned in numberOfBlobs
Hey Image Analyist, I have one more question for you.
Is it possible to count the number of each color bit?
Please do not mind my above comment.
Is it possible to extract the area of each closed surface in the pusedo colour image?
Not sure what you mean by "area of each closed surface" but you can get the total number of white pixels in your binary image like this
numWhitePixels = nnz(binaryImage);
If you want the area of each individually you can use regionprops
props = regionprops(binaryImage);
allAreas = [props.Area]
Ok. I tried regionprops. It gives me 278 areas. Does this mean, I have 278 irregular circles (white regions) in the binary image?
Also, I tried uploading the binary logical image into the image region analyizer app. I get something like this
Does this mean, i have 312 white regions and matlab just evaluated it's areas, equivalent diameters etc?
You can get the number of blobs like this:
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
props = regionprops(labeledImage, 'Area', 'EquivDiameter', 'Perimeter'); % Whatever you want.
numberOfBlobs should be the same as numel(props) which is the number of blobs in the image.
You might want to call
binaryImage = imclearborder(binaryImage);
before calling bwlabel() to get rid of partial blobs being chopped off at the boundary. The areas are not accurate for those blobs because we do not have the entire blob available to measure.
Alright, I understand. I will let you know if i have anymore questions.
Thank you and have a nice day

Sign in to comment.

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!