MATLAB: How to Close an Open Contour

close an open contourcurve fittingImage Processing Toolbox

Dear all, Attached image consists of two open contours. I would like to close these two contours. Please let me know if this is doable in Matlab.
Thanks Ashkan

Best Answer

Skeletonize the blob with bwmorph(), then use bwmorph() again to find the endpoints. Use regionprops() to get coordinates of the skeleton so you know which endpoints belong to the same blob. Then use imline() to draw a line connecting the endpoints of each blob. Demo of imline used to burn a line into an image is attached. Please try these steps yourself first and come back with your code if you can't do it. Again, the rough steps are
bwmorph(binaryImage, 'skel', inf);
bwmorph(binaryImage, 'endpoints');
[labeledImage, numBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'PixelList');
for k = 1 : numBlobs
% find endpoints of this blob, then
imline();
% etc
end
Finish it up (it shouldn't be too hard) and let me know of any problems.