Grouping object with minimum in-between distance.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
rupam baruah
il 21 Mar 2016
Modificato: Matthew Eicholtz
il 21 Mar 2016
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158438/image.png)
2 Commenti
Matthew Eicholtz
il 21 Mar 2016
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?
Risposta accettata
Matthew Eicholtz
il 21 Mar 2016
Modificato: Matthew Eicholtz
il 21 Mar 2016
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173782/image.png)
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173783/image.png)
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 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!