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

4 views (last 30 days)
this to this

Accepted Answer

Image Analyst
Image Analyst on 25 Oct 2021
Edited: Image Analyst on 25 Oct 2021
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;
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);
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
% Get a mask:
binaryImage = grayImage < 128;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis('on', 'image');
title('Mask/Binary/Logical Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% 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, []);
axis('on', 'image');
title('Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% 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);
axis('on', 'image');
title('Colorized Labeled Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
[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)

yanqi liu
yanqi liu on 26 Oct 2021
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;

Akshay Kumar Pakala
Akshay Kumar Pakala on 25 Oct 2021
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.

Akshay Kumar Pakala
Akshay Kumar Pakala on 25 Oct 2021
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.

Sign in to comment.


Find more on Image Processing Toolbox in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!