How can detect or remove object that have area between 2 values?

I extract some object with threshold in some images. I earn their area with regionprops function. I want to detect the objects that their area is between two values(Max & Min values), or to remove all objects that their area is less than and greater than 2 values. Can some one help me?

1 件のコメント

Amir
Amir 2014 年 8 月 15 日
Can you please attach one example image.

サインインしてコメントする。

 採用された回答

Amir
Amir 2014 年 8 月 15 日
編集済み: Amir 2014 年 8 月 15 日

1 投票

Please try this code, I tried to write this code very clear (not efficient), please let me know if it is not clear
clc
clear all
close all
[filename, pathname] = uigetfile('*','File Selector');
I = imread(strcat(pathname,'\',filename)); % for example FileName='MyImage.jpg'
I=im2bw(I);
BW = edge(I,'canny',0.1);
[bw, loc2]= imfill(BW,'holes');
% http://www.mathworks.co.uk/help/images/ref/regionprops.html
rp = regionprops(bw,'All'); % you can specify the parameters which you need
ObjArea=zeros(size(rp,1),1);
CenterX=zeros(size(rp,1),1);
CenterY=zeros(size(rp,1),1);
for i=1:size(rp,1)
ObjArea(i)=rp(i).Area;
CenterX (i)= rp(i).Centroid(1);
CenterY (i)= rp(i).Centroid(2);
% you can add other properties (for example area, perimeter etc here)
end
Final=[ObjArea CenterX CenterY];
imshow(I);
hold on
for i=1:size(Final,1)
text(Final(i,2),Final(i,3),num2str(Final(i,1)),...
'HorizontalAlignment' , 'center',...
'VerticalAlignment' , 'middle');
end
title('Area of Particles');
prompt = {'Minimum size:','Maximum size:'};
dlg_title = 'Minimum and maximum sizes';
num_lines = 1;
def = {'5000','15000'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
MinMaxSize =str2num(char(answer));
MinSize=MinMaxSize(1,1); MaxSize= MinMaxSize(2,1);
Removed=[];
for i=1:size(rp,1)
if rp(i).Area<MinSize || rp(i).Area>MaxSize
Removed=[Removed i];
end
end
rp(Removed)=[];
NewImage= zeros(size(I));
for i=1:size(rp,1)
OneOject= rp(i).PixelList;
for j=1:length(rp(i).PixelList)
OneRow = OneOject(j,:);
NewImage (OneRow(2),OneRow(1))=1;
end
end
figure
imshow(NewImage)
title('Area of Particles - between Minimum and Maximum');
Step 1: Open your image
Step 2: See size of objects and choose the range
Step 3: Objects with the size between minimum and maximum will be shown

4 件のコメント

Erfaneh
Erfaneh 2014 年 8 月 15 日
Thanks for your reply. My problem has been solved.
Ben11
Ben11 2014 年 8 月 15 日
Thanks Amir that's a very nice answer!
Image Analyst
Image Analyst 2014 年 8 月 15 日
For an alternate size filtering method which was recommended to me by the Mathworks when I was first learning MATLAB, and which I still use, see my Image Processing Tutorial http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial---blobsdemo-- It uses ismember() and find() instead of for loops. The method is adaptable to any filtering of any property values (perimeter, MajorAxisLength, etc.).
Amir
Amir 2014 年 8 月 16 日
Thanks Image Analyst for this link and comment. That is very helpful.

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2014 年 8 月 15 日

コメント済み:

2014 年 8 月 16 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by