How can I calculate areas of different segmented regions which are in the same image?

6 次查看(过去 30 天)
Hello all as I mentioned in the title I want to calculate the areas of the segmented regions which are in the same image. Thus far, I deleted the noises on the image and segmented three objects but I do not know how to calculate the areas of these regions, and this is the code i use :
close all
% Reads the image and names it as RGB (writing file location
% w/ file location and file extension)
% and shows it
figure
subplot(2,3,1)
RGB = imread('C:\Users\hp.user\Desktop\1\image #1.png');
imshow(RGB);
% Firstly, converts the image to gray, after that converts it
% to pure black and white
I = rgb2gray(RGB);
bw = imbinarize(I);
subplot(2,3,2)
imshow(bw);
% binarize function has global threshold level, to change
% this threshold level, we can implemet :
% level = graythresh(I)
% level = 0.5
% bw = imbinarize(I, level);
% global threshold level is 0.6863
% in our image global threshold level is erasing the some
% figures on the image so the threshold level is changed
% to 0.65
level = graythresh(I);
level = 0.65;
bw = imbinarize(I,level);
subplot(2,3,3)
imshow(bw)
% Removing the noise by using morphology functions,
% remove pixels which do not belong to the objects of
% interest
% bwareaopen removes all objects contatining fewer that 36 pixels but not
% removing the noise in the objects
bw = bwareaopen(bw,36);
subplot(2,3,4)
imshow(bw);
% to remove the noise in the objects we do the following code
% fill a gap in the pen's cap
% strel('disk',R) creates a disk-shaped structuring elemnt,
% where R speccifies the radius
se = strel('disk',2);
% bw1 = imclose(bw,se) performs morphological closing on the
% grayscale or binary image bw, returning the closed
% image, bw1
bw = imclose(bw,se);
% fill any holes, so that region props can be used to estimate
% the area enclosed by each of the boundaries
% imfill(bw,locations) performs a flood-fill operation on background pixels
% the input binary image starting from the points specified in
% locations
bw = imfill(bw,'holes');
subplot(2,3,5)
imshow(bw)
% find the boundaries
% calculate boundaries of regions in image and overlay the boundaries
% on the image
[B,L] = bwboundaries(bw,'noholes');
subplot(2,3,6)
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
*boundary = B{k};*
*plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)*
end
-------------------------------------
According to my lecturer, it is not available to use " regionprops" where image includes more than one segmented region to calculate area. Waiting for your helps. Thanks!

回答(3 个)

Brian Hannan
Brian Hannan 2017-7-20
Hi, Fevzi. You've already done the hard work here. The cell array B contains the boundaries of your polygons. One way to calculate the area of each boundary is to count the number of pixels it contains. Some functions you might want to check out are inpolygon, poly2mask, and polyarea.

Image Analyst
Image Analyst 2017-7-20
编辑:Image Analyst 2017-7-20
Your lecturer is completely wrong. Or perhaps you didn't understand exactly what he was trying to say. You certainly CAN measure multiple blobs with one call to regionprops().
  5 个评论
Wenqi Chen
Wenqi Chen 2024-2-9
How can I measure different cells in different areas like this? And how to deal with cells on the boundaries?Thanks!
Image Analyst
Image Analyst 2024-2-10
@Wenqi Chen you can turn your boundaries into masks with poly2mask. Then use regionprops to measure the area of the region.
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.
If you have any more questions, start your own discussion thread (rather than continue here).

请先登录,再进行评论。


Fevzi Ustundag
Fevzi Ustundag 2017-7-24
Thanks for the answers, I have just solved the problem after I uploaded the problem here. And you may be right about my lecturer. Maybe I did not understand him very well. Now, I am designing a GUI which contains different operations which includes also this area calculation. By the way, the areas of the objects are not seem exactly accurate. But it gives same results with MATLAB's Image Region Analyzer. Is it exact value or approximate value that acquired on Image Region Analyzer?
And this is the code I use to show areas of different objects in one figure:
figure
subplot(2,3,1)
RGB = imread('C:\Users\hp.user\Desktop\alan.png');
imshow(RGB);
I = rgb2gray(RGB);
bw = imbinarize(I);
subplot(2,3,2)
imshow(bw);
level = graythresh(I);
level = 0.65;
bw = imbinarize(I,level);
subplot(2,3,3)
imshow(bw)
bw = bwareaopen(bw,36);
subplot(2,3,4)
imshow(bw);
se = strel('disk',2);
bw = imclose(bw,se);
bw = imfill(bw,'holes');
subplot(2,3,5)
imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
subplot(2,3,6)
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,'Area','Centroid');
threshold = 0.94;
for k = 1:length(B)
boundary = B{k};
area = stats(k).Area;
area_string = sprintf('%2.2f',area);
if area > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
text(boundary(1,2)-35,boundary(1,1)+13,area_string,'Color','k',...
'FontSize',14,'FontWeight','bold');
end

Community Treasure Hunt

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

Start Hunting!

Translated by