How to find coordinates of the centroid of objects(found with bwlabel function) with area more than 3 pixels in the attached image?

3 views (last 30 days)
Hello all, I have the attached image and using bwlabel MATLAB built-in function, I got the number of objects. Then I want to find the coordinates of the centroid of objects with more than three pixels;In the code I wrote bwlabel gives me 1279 objects and the with this line(allAreas= allAreas(allAreas>3);),the number of objects will be 665 in the workspace, but when I wrote the for loop,it started with the first object in the stats structure which its area is 1 pixel. Thanks
a = imread('PrimaryOrange.jpg');
bg=rgb2gray(a);
threshold = 20;
bw=bg> threshold;
out=bw;
L = bwlabel(out);
[labeledImage, numberOfObject] = bwlabel(out);
props = regionprops(labeledImage, 'Area');
allAreas = [props.Area];
stats = regionprops('table',labeledImage,'Area','Centroid',...
'MajorAxisLength','MinorAxisLength','Perimeter')
allAreas= allAreas(allAreas>3);
for i= 1 : numberOfObjects
x_object= stats.Centroid(i,1);
y_object= stats.Centroid(i,2);
new_x= ceil(x_object )+ 30
new_y=ceil(y_object)+30
new_x_y= [new_x new_y]
if BWdapi(new_x_y) == 1
antibody = [antibody ; new_x_y]
end
end

Accepted Answer

Matt J
Matt J on 23 Oct 2018
Edited: Matt J on 23 Oct 2018
You could just filter out all the objects that are too small before doing any of your other processing.
bw=bwareafilt( bg> threshold, [4,inf]);
Or, do
stats=stats(allAreas>3);
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!