【问题标题】:how do i mask labeled object based on some specified threshold value for each objects area,majoraxis and minoraxis?如何根据每个对象区域、长轴和短轴的某些指定阈值屏蔽标记的对象?
【发布时间】:2015-07-14 04:55:42
【问题描述】:

我目前使用bwconnomp 标记每个连接的对象,regionprops 分别查找每个标记对象的面积、长轴、短轴。我还显示每个标记对象的面积、长轴和短轴。现在我想为面积、长轴和短轴设置一些阈值,如果面积、长轴和短轴的值高于指定阈值,则必须屏蔽该对象。如何做到这一点??

这是我的代码

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

【问题讨论】:

  • 那么你是说你只想显示超过面积、长轴和短轴长度阈值的对象?
  • @rayryeng 不,我只想显示低于某个指定阈值的对象。

标签: matlab image-processing image-segmentation


【解决方案1】:

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

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')

以及样本输出:

【讨论】:

  • 我尝试以这种方式屏蔽 AboveIndices = BW2(vertcat(data.Area) > 13000 & vertcat(data.MajorAxisLength) >300 & vertcat(data.MinorAxisLength) >90); C = num2cell(AboveIndices) BW2(vertcat((C{cellfun(@numel,C)})))=0;但它不工作......
猜你喜欢
  • 2015-07-05
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多