How to Close an Open Contour
Show older comments
I have generated the skeleton of a binary shape, and now I want to connect the endpoints in a way that forms a closed contour. The goal is to identify the endpoints of the skeletonized structure and connect them in a meaningful way to reconstruct a continuous, closed boundary. I wrote the code in matlab but something is wrong.

clc;
clear;
close all;
img = imread('imag10_Seg.png');
bw = imbinarize(img);
skeletonImage = bwmorph(bw, 'skel', inf);
figure();
imshow(skeletonImage);
endpointsImage = bwmorph(skeletonImage, 'endpoints');
[labeledImage, numBlobs] = bwlabel(skeletonImage);
measurements = regionprops(labeledImage, 'PixelList');
[rows, cols] = find(endpointsImage);
endpoints = [cols, rows];
[labeledImage, numBlobs] = bwlabel(bw);
measurements = regionprops(labeledImage, 'PixelList');
for k = 1 : numBlobs
imline();
end
Answers (1)
Without the source image, I'm just going to use a contrived source.
% a grayscale image
inpict = imread('opencontour.png');
imshow(inpict)
% binarize it somehow
bw = imbinarize(inpict);
% use bwskel() instead to get rid of extraneous branches
% this may end up truncating the contour if you're not careful
skel = bwskel(bw,'minbranchlength',30);
% find all the endpoints
skelEP = bwmorph(skel,'endpoints');
% there should probably be some effort here
% to make sure that there are only two relevant
% endpoints left
% ....
% draw lines between the endpoints
% this only makes sense if there are only two endpoints
% bridge = bwconvhull(skelEP);
% alternatively, this just draws a polyline between the points
% it still only makes sense if there are only two endpoints
V = vertcat(regionprops(skelEP,'centroid').Centroid);
bridge = brline_forum(size(skelEP),V);
% combine the two
outpict = skel | bridge;
imshow(outpict)
There's probably a way to use watershed here, but I'm not sure if that's any more convenient/reliable.
Categories
Find more on Image Processing Toolbox 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!
