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

1 view (last 30 days)
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?

Accepted Answer

Amir
Amir on 15 Aug 2014
Edited: Amir on 15 Aug 2014
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 Comments
Image Analyst
Image Analyst on 15 Aug 2014
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 on 16 Aug 2014
Thanks Image Analyst for this link and comment. That is very helpful.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!