How to find blob nearest to specified coordinate?

2 views (last 30 days)
Given: I have following image:
Want: I want to find blob nearest to desired coordinate [160,138], and remove all other blobs, as show bellow:
Currently done:
I=imread('1_im.jpg');
BW= im2bw(I);
CC = bwconncomp(BW, 8);%Determine the connected components
stat_I = regionprops(CC,'Area','Centroid'); %Compute the area and centroid of CC
L = labelmatrix(CC); % Create label matrix
aprox_coor=[160,138]; % Coordinates of intrest
x_coor = aprox_coor(1);y_coor = aprox_coor(2);
for u = 1:length([stat_I.Area]) % Convert struct.Centoid into double
x_cent(u)=stat_I(u).Centroid(:,1);
y_cent(u)=stat_I(u).Centroid(:,2);
xy_cent=[x_cent' y_cent'];
end
if length([stat_I.Area])<1 %chek in check for empty cells within stat_I list
ind_xy = [];
else
%find index of wanted x coordinate +/- 10%
ind_x = find(x_cent >= x_coor*0.9 & x_cent <= x_coor*1.1);
ind_y = find(y_cent >= y_coor*0.9 & y_cent <= y_coor*1.1); % the same for y
ind_xy = intersect(ind_x,ind_y); % check if indexs are the same
stat_I(ind_xy).Area %Remove all other objects
BW2 = ismember(L, find([stat_I.Area] == stat_I(ind_xy).Area));
figure(ci); imshow(BW2);
end
Needed: I'm wondering how to simplify and make the code more robust.
[ACKNOWLEDGMENTS]
Thank you for any help.
I will vote for all your answers.
[MATLAB version]
R2014a

Answers (0)

Community Treasure Hunt

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

Start Hunting!