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

조회 수: 2 (최근 30일)
Erfaneh
Erfaneh 2014년 8월 15일
댓글: Amir 2014년 8월 16일
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?

채택된 답변

Amir
Amir 2014년 8월 15일
편집: Amir 2014년 8월 15일
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
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개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by