Grouping object with minimum in-between distance.

2 次查看(过去 30 天)
Hi there I have a binary image with different objects. I want to fill the objects which are placed in a group with minimum in between distances (object 1,2,3,4 and 5 are in group). How can I avoid filling the other objects which are not in the group. Thank You
  2 个评论
Matthew Eicholtz
Matthew Eicholtz 2016-3-21
What do you mean by "fill the objects which are placed in a group"? Do you mean that you want to fill the holes within individual members in a group? Or that you want to connect all members of a group by filling in the space between the objects?
rupam baruah
rupam baruah 2016-3-21
I want to fill the holes within individual members in a group.

请先登录,再进行评论。

采纳的回答

Matthew Eicholtz
Matthew Eicholtz 2016-3-21
编辑:Matthew Eicholtz 2016-3-21
Use imfill with the form BW2 = IMFILL(BW,LOCATIONS) where LOCATIONS specifies the points at which to start the flood-fill operation.
Example
Create a sample image containing several shapes with holes:
I = zeros(200,200);
I = insertShape(I,'circle',[30 40 10],'LineWidth',10,'Color','w');
I = insertShape(I,'circle',[50 60 5],'LineWidth',2,'Color','w');
I = insertShape(I,'rectangle',[20 70 5 20],'LineWidth',2,'Color','w');
I = insertShape(I,'circle',[170 60 20],'LineWidth',5,'Color','w');
I = insertShape(I,'rectangle',[140 20 50 10],'LineWidth',5,'Color','w');
I = insertShape(I,'rectangle',[110 50 20 30],'LineWidth',2,'Color','w');
I = insertShape(I,'circle',[100 150 10],'LineWidth',8,'Color','w');
I = insertShape(I,'circle',[120 180 10],'LineWidth',6,'Color','w');
I = insertShape(I,'circle',[80 175 10],'LineWidth',4,'Color','w');
I = insertShape(I,'rectangle',[55 125 20 20],'LineWidth',10,'Color','w');
I = im2bw(I); %convert to binary image
figure; imshow(I);
Next, locate each object using bwconncomp and compute its centroid using regionprops.
cc = bwconncomp(I);
s = regionprops(cc,'Centroid');
xy = round(reshape([s(:).Centroid],2,[])'); %an n-by-2 array of object centroids
ind = sub2ind(size(I),xy(:,2),xy(:,1)); %linear indices of object centroids
Now I assume you have some method already for grouping objects together (e.g. clustering), so I won't describe how to do that. But, let's say that your algorithm determined that objects 4, 5, 6, and 7 should be grouped,
grp = [4 5 6 7];
Then, to fill in the objects in that group but not others, use imfill:
J = imfill(I,ind(grp));
figure; imshow(J);
NOTE: If one or more of the objects is non-convex or has multiple holes, you may need to seed the flood-fill algorithm with something other than the object centroid.
Hope this helps.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by