You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matlab中准确计算图像中各区域的面积?

问题描述

我想要计算图像中由白色边界包围的各个区域的面积,尝试了以下Matlab代码,但结果不准确:

a = rgb2gray(imread("flox2 (1).tif"));
figure,imshow(a)

thresholdValue = 100;
binaryImage = a > thresholdValue;
props = regionprops(binaryImage, 'all');
numberOfBlobs = numel(props);
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
    thisBoundary = boundaries{k}; % Get boundary for this specific blob.
    x = thisBoundary(:,2); % Column 2 is the columns, which is x.
    y = thisBoundary(:,1); % Column 1 is the rows, which is x.
    plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
for k = 1 : numberOfBlobs           % Loop through all blobs.
    % Find the individual measurements of each blob.  They are field of each structure in the props strucutre array.
    % You could use the bracket trick (like with blobECD above) OR you can get the value from the field of this particular structure.
    % I'm showing you both ways and you can use the way you like best.
    %meanGL = props(k).MeanIntensity;       % Get average intensity.
    blobArea = props(k).Area;               % Get area.
    %blobPerimeter = props(k).Perimeter;        % Get perimeter.
    %blobCentroid = props(k).Centroid;      % Get centroid one at a time
    % Now do the printing of this blob's measurements to the command window.
    fprintf(1,'#%2d %17.1f %11.1f %8.1f %8.1f %8.1f % 8.1f\n', k,  blobArea);
end

执行代码后的结果出现了一些不应存在的被红色边界包围的小区域,恳请提供改进建议。

改进方案

1. 优化二值化阈值

固定阈值100易受图像噪声和灰度分布影响,改用自动阈值方法更可靠:

  • Otsu自动阈值:根据图像灰度分布自动计算最优阈值
level = graythresh(a);
binaryImage = imbinarize(a, level);
  • 自适应阈值:适合光照不均匀的图像
thresh = adaptthresh(a, 0.4); % 0.4为灵敏度参数,可按需调整
binaryImage = imbinarize(a, thresh);

2. 过滤小面积噪声区域

用bwareaopen函数移除面积过小的连通区域,只保留目标区域:

% 过滤掉面积小于50像素的区域,阈值可根据实际图像调整
binaryImage = bwareaopen(binaryImage, 50);

3. 图像预处理降噪

二值化前对图像做平滑处理,减少噪声干扰:

% 高斯滤波,sigma=1.5可根据噪声程度调整
a = imgaussfilt(a, 1.5);

4. 仅识别外部边界

bwboundaries默认会识别内部孔洞的边界,若只需要白色边界包围的区域,添加'noholes'参数:

boundaries = bwboundaries(binaryImage, 'noholes');

完整修改后代码

% 读取图像并转灰度
a = rgb2gray(imread("flox2 (1).tif"));
figure, imshow(a)

% 预处理:高斯平滑降噪
a = imgaussfilt(a, 1.5);

% Otsu自动阈值二值化
level = graythresh(a);
binaryImage = imbinarize(a, level);

% 去除小面积噪声区域
binaryImage = bwareaopen(binaryImage, 50);

% 提取区域属性与外部边界
props = regionprops(binaryImage, 'Area');
numberOfBlobs = numel(props);
boundaries = bwboundaries(binaryImage, 'noholes');
numberOfBoundaries = size(boundaries, 1);

% 绘制红色边界
hold on;
for k = 1 : numberOfBoundaries
    thisBoundary = boundaries{k};
    x = thisBoundary(:,2);
    y = thisBoundary(:,1);
    plot(x, y, 'r-', 'LineWidth', 2);
end
hold off;

% 输出各区域面积
for k = 1 : numberOfBlobs
    blobArea = props(k).Area;
    fprintf(1,'#%2d 面积:%17.1f 像素\n', k, blobArea);
end

内容的提问来源于stack exchange,提问作者farabee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 21:00:31