I would like to know how to convert a binary image to a pseudo color image based on closed regions
Show older comments
Accepted Answer
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;
Akshay Kumar Pakala
on 25 Oct 2021
0 votes
Akshay Kumar Pakala
on 25 Oct 2021
0 votes
10 Comments
Akshay Kumar Pakala
on 25 Oct 2021
Akshay Kumar Pakala
on 25 Oct 2021
Image Analyst
on 26 Oct 2021
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
Akshay Kumar Pakala
on 26 Oct 2021
Akshay Kumar Pakala
on 26 Oct 2021
Akshay Kumar Pakala
on 26 Oct 2021
Image Analyst
on 26 Oct 2021
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]
Akshay Kumar Pakala
on 26 Oct 2021
Image Analyst
on 26 Oct 2021
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.
Akshay Kumar Pakala
on 26 Oct 2021
Categories
Find more on Blue 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!



