MATLAB 中的面积计算

Area calculation in MATLAB

我正在写一篇论文,我需要从我附加的图像中找出黑色区域的面积。

Original image

我用阈值和赞美图像做了一些处理。Processed image 现在我在寻找黑色区域的面积时遇到了问题。有人可以帮忙吗?我是 MATLAB.

的新手

Here is my code :

img1=imread('C:/Users/Allan/Desktop/unnamed1.jpg');
imshow(img1)

img1=rgb2gray(img1);
imshow(img1)

img2=im2bw(img1,graythresh(img1));
imshow(img2)

img2=~img2;
imshow(img2)

B = bwboundaries(img2);
imshow(img2)
hold on

for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
end

使用regionpropsbwarea:

% take the NOT image
bw = ~img2;
% find individual regions
cc = bwconncomp(bw);
% find area for each black region
props = regionprops(cc,{'Area','Centroid'});
regionArea = [props(:).Area];
% find area of total black region
totalArea = bwarea(bw);
% plotting
for ii = find(regionArea > 100)
    c = props(ii).Centroid;
    text(c(1),c(2),num2str(regionArea(ii)),'Color','b',...
        'HorizontalAlignment','center','VerticalAlignment','middle',...
        'FontWeight','bold');
end