Connecting the dots in a binary image
Show older comments
I have an image (attached) and I want to connect the dots into a circle (to later be filled in and the white coordinates will be collected). For context, the current coordinates shown are those making up the verticies of an annotation I did around an image I hope to eventually classify, and I annotated it at the pixel level. I annotated a complete circle, but I think the software I did it on had a different process of deciding which coordinates to save and resulting in my image not being fully connected. In order to fully fill in my image eventually, I want to make sure I have a fully connected border first, and to air on the side of caution, I want to close the border using pixels closest to the inside (or centroid of the shape) in cases where 2 edge lines could be connected by a point closer to the center or further (I always want the closer one). For example, if you look at the bottom of the image I have attached, there seems to be one black square disconnecting two long white boarder lines. I want to add a white square bridging those two lines, but I dont want it to fill in the line and make it straight, I want the white pixel I am adding to be up a line and inbetween the two line edges, touching each edgeline diagonally at the corners (or be on the y=2 line, not the y=1 line if you'd rather think of it in terms of the bottom left corner being (1,1)).
I have tried using imclose() and unfortunately it is too generous and estimates points outside the boundaries I have annotated to complete the shape. If anyone has any smart ways of going about this they can share, please let me know, I would really appreciate it! Like I said, I am working with this shape at the coordinate level, so if you know of a way to compute the points at that level, that would be totally find as well. I have attached a photo of just the image I am trying to change and rougly how I would ideally like it to be connected if this would help to see.
3 Comments
Image Analyst
on 7 Mar 2022
You could use edge linking (demo attached) though I think it's better to avoid the problem in the first place by doing a better segmentation that is not all broken up. Can you attach your original image and your segmentation code?
Mia Grahn
on 7 Mar 2022
Mia Grahn
on 7 Mar 2022
Accepted Answer
More Answers (1)
img = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/916929/Screen%20Shot%202022-03-06%20at%208.51.29%20PM.png');
img = imcrop(img,[78 74 202 224]);
bw = im2bw(img);
bw2 = ~bw;
bw2 = imerode(bw2, strel('disk', 10));
bw2 = bwareafilt(bw2,1);
bw2 = imadd(bw2,bw);
bw3 = imclose(bw2, strel('disk', 8));
bw4 = imadd(bw, imdilate(bwperim(bw3), strel('disk',3)));
figure; imshow(bw2);
figure; imshow(bw3);
figure; imshow(bw4);
Categories
Find more on White in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






