我如何根据每个对象区域、长轴和短轴的一些指定阈值来屏蔽标记对象?

how do i mask labeled object based on some specified threshold value for each objects area,majoraxis and minoraxis?

我目前正在使用 bwconnomp 标记每个连接的对象,并 regionprops 分别查找每个标记对象的面积、长轴、短轴。我还显示每个标记对象的面积、长轴和短轴。现在我想为面积、长轴和短轴设置一些阈值,如果面积、长轴和短轴的值高于指定阈值,那么该对象必须 masked.how 这可以完成吗??

这是我的代码

clc
clear all
close all
Index = 1;
scrsz = get(0,'ScreenSize');
%read an image
while Index ~= 0
    % Open a dialog and select an image file
    [FileName,FilePath,Index] = uigetfile('*.png', 'Open Imagefile ');
    if Index == 0
        disp('Procedure Done')
        break;
    end
inimage = imread([num2str(FilePath) FileName]);
D=inimage;
A=inimage;
subplot(2,3,1);
imshow(inimage);
title('original image');


%labeling algorithm
 B=im2bw(inimage);
 C=imfill(B,'holes');
 label=bwlabel(C);
 max(max(label))
 CC = bwconncomp(B);
data = regionprops(CC,'all');


for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth] ));
sy=min(col)-1;
sx=min(row)-1;

for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=A(row(i,1),col(i,1));
end
mytitle=strcat('Object Number:' ,num2str(j),'area:', num2str(data(j).Area),'MajorAxis: ',num2str(data(j).MajorAxisLength),'MinorAxis: ',num2str(data(j).MinorAxisLength));
figure,imshow(target);title(mytitle);
a=size(target);
ax=a(1);
ay=a(2);
pos=[1,1,ay,ax];
rectangle('Position',pos,'EdgeColo','r')
end

next = input('next image? press Enter: ');
if next == 0
    channelactivity = 0;
    break
else
    close all
    disp('==================================')
    pause(0.2)
    continue
end

end

这是一种方法。代码注释很容易理解;重要的一行如下:

AboveAreaIndices = find(vertcat(data.Area) > SomeValue)

其中存储面积大于SomeValue的对象的索引。在示例中,我将它们涂成红色,但您可以对它们做任何您想做的事情,或者将它们从 data 结构中完全删除。

您还可以使用逻辑运算符组合多个条件,例如使用 MinorAxisMajorAxis 属性。请注意,我使用 AllArea 作为一个新变量来存储连接区域以使事情更清楚,但您可以将它们保留为 vertcat(data.Area).

AboveIndices = find(vertcat(data.Area) > SomeValue & vertcat(data. MinorAxis) > SomeValue & Bla bla bla...);

完整代码:

clear
clc
close all

%// Read and clean up sample image
A = imread('rice.png');

A = im2bw(A,.5);

A = bwareaopen(A,50);
CC = bwconncomp(A);

%// Same as you.
data = regionprops(CC,'all');

%// Concatenate all the areas into an array. 
AllArea = vertcat(data.Area);

%//========================================
%//==== Apply threshold on area here \====

AboveAreaIndices = find(AllArea > 150);

%// If you wish to remove the entries from the data structure
% data(AllArea>150) = [];
%//========================================

%// Same for centroids...for display purposes
AllCentroids = vertcat(data.Centroid);


%// Display original and thresholded objects. Use the indices calculated
%// above to "mask" large areas if you want
imshow(A);

hold on

scatter(AllCentroids(:,1),AllCentroids(:,2),40,'b','filled')
scatter(AllCentroids(AboveAreaIndices,1),AllCentroids(AboveAreaIndices,2),40,'r','filled')

样本输出: