如何计算不同标签的面积并去除面积较小的标签
how to calculate the area of different labels and remove the labels with smaller area
我使用 CC = bwconncomp(BW) 标记了图像。现在我必须删除较小的标签 area.i 已使用以下代码查找区域。
[B,L] = bwboundaries(Bw,'noholes');
stats = regionprops(L,'Area','perimeter');
area = [stats.Area];
但现在如何删除面积较小的标签???
简单。使用 bwareaopen
。这需要一个二进制图像和一个你想要强制执行的最小区域。输出是相同的图像,但所有低于最小面积的区域都被删除了。
因此,假设thresh
是您要为图像中的对象保留的最小区域,请执行:
%// Remove those labels that are below an area of thresh
out = bwareaopen(L ~= 0, thresh);
%// Make a copy of the original label matrix and
%// only copy the labels that appeared in the above result over
Lfinal = zeros(size(L));
Lfinal(out) = L(out);
Lfinal
包含移除了较小区域的最终标签矩阵。在您的情况下,您可能希望从具有最大面积的对象中消除那些小于一定数量的对象......所以也许可以这样做:
thresh = round(0.1*max(area));
这会查看标签矩阵中最大的区域,并占其中的 10%。任何面积低于此最大面积 10% 的对象都将从您的标签矩阵中删除。
我使用 CC = bwconncomp(BW) 标记了图像。现在我必须删除较小的标签 area.i 已使用以下代码查找区域。
[B,L] = bwboundaries(Bw,'noholes');
stats = regionprops(L,'Area','perimeter');
area = [stats.Area];
但现在如何删除面积较小的标签???
简单。使用 bwareaopen
。这需要一个二进制图像和一个你想要强制执行的最小区域。输出是相同的图像,但所有低于最小面积的区域都被删除了。
因此,假设thresh
是您要为图像中的对象保留的最小区域,请执行:
%// Remove those labels that are below an area of thresh
out = bwareaopen(L ~= 0, thresh);
%// Make a copy of the original label matrix and
%// only copy the labels that appeared in the above result over
Lfinal = zeros(size(L));
Lfinal(out) = L(out);
Lfinal
包含移除了较小区域的最终标签矩阵。在您的情况下,您可能希望从具有最大面积的对象中消除那些小于一定数量的对象......所以也许可以这样做:
thresh = round(0.1*max(area));
这会查看标签矩阵中最大的区域,并占其中的 10%。任何面积低于此最大面积 10% 的对象都将从您的标签矩阵中删除。