Matlab 如何根据每个对象区域、主坐标和最小坐标的某些指定阈值遮罩标记的对象?
我目前正在使用Matlab 如何根据每个对象区域、主坐标和最小坐标的某些指定阈值遮罩标记的对象?,matlab,image-processing,image-segmentation,Matlab,Image Processing,Image Segmentation,我目前正在使用bwconomp标记每个连接的对象,并使用regionprops分别查找每个标记对象的面积、大小轴。我还显示了每个标记的对象的面积、大小。现在我想为area、majoraxis和minoraxis设置一些阈值,如果area、majoraxis和minoraxis的值高于指定的阈值,则必须屏蔽该对象。如何做到这一点 这是我的密码 clc clear all close all Index = 1; scrsz = get(0,'ScreenSize'); %read an image
bwconomp
标记每个连接的对象,并使用regionprops
分别查找每个标记对象的面积、大小轴。我还显示了每个标记的对象的面积、大小。现在我想为area、majoraxis和minoraxis设置一些阈值,如果area、majoraxis和minoraxis的值高于指定的阈值,则必须屏蔽该对象。如何做到这一点
这是我的密码
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
的对象的索引。在本例中,我将它们涂成红色,但您可以对它们执行任何操作,也可以将它们从数据
结构中删除
您还可以使用逻辑运算符组合多个条件,例如使用MinorAxis
和MajorAxis
属性。请注意,我使用了allrea
作为一个新变量来存储连接的区域,以使事情更清楚,但您可以将它们保留为vertcat(data.Area)
全部代码:
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')
和样本输出:
那么,你是说你只想在对象超过面积、长轴和短轴长度阈值时才显示它吗?@rayryeng否我只想显示低于某个指定阈值的对象。我尝试用这种方式在索引=BW2(vertcat(data.area)>13000和vertcat(data.MajorAxisLength)>300和vertcat上进行掩蔽(data.MinorAxisLength)>90);C=num2单元(高于指数)BW2(vertcat((C{cellfun(@numel,C)}))=0;但它不工作。。。
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')