I want to connect the ends of the white contour in the image to form a complete circle, eliminating the gaps.
Show older comments
Hi!
In this image, I want to connect the ends of the white contour to form a complete circle. Currently, there is a break in its structure, and my goal is to detect the endpoints and link them to obtain a closed shape. I want to use the bwskel function in MATLAB.

img = imread('imag5_Seg.png');
bw = imbinarize(img);
bw = logical(bw);
schelet = bwskel(bw);
figure();
imshow(schelet);
Accepted Answer
More Answers (1)
Mathieu NOE
on 18 Mar 2025
Edited: Mathieu NOE
on 18 Mar 2025
hello
sorry , this is a "no image processing" code - it may or may not correspond to your needs , but it does the job !
hope it helps
you will have to download this Fex submission for smoothing (I like it very much) but yiu ca of course use another smoother
result :

code :
%% load file
file_name='image.png';
img=imread(file_name);
if size(img,3)==3
img=rgb2gray(img);
end
% binarize image
outpic = zeros(size(img));
ind = find(img>128);
outpic(ind) = 1;
[y,x] = ind2sub(size(img),ind); % get x,y points coordinates of the white area
%% method 1
centroid_x = mean(x);
centroid_y = mean(y);
[theta,r] = cart2pol(x-centroid_x,y-centroid_y);
% sort theta in ascending order
[theta,ind] = sort(theta);
r = r(ind);
% remove duplicates
[theta,IA,IC] = unique(theta);
r = r(IA);
% take the average of r within an angle of range dt (if no data then NaN output)
Npoints = 100;
theta_new = linspace(min(theta),max(theta),Npoints);
dt = mean(diff(theta_new));
for k = 1:Npoints
ind = (theta>=theta_new(k)-dt/2) & (theta<theta_new(k)+dt/2);
if isempty(ind)
r_new(k) = NaN;
else
r_new(k) = mean(r(ind));
end
end
% replace / interpolate the NaN values
r_new = fillmissing(r_new,'spline');
% convert to cartesian
[xn,yn] = pol2cart(theta_new,r_new);
% add back centroid info
xn = xn + centroid_x;
yn = yn + centroid_y;
% smoothing (if needed)
Y = {xn,yn};
[zz,s,exitflag] = smoothn(Y,10);
xn = zz{1};
yn = zz{2};
% closing the curve
xn(end+1) = xn(1);
yn(end+1) = yn(1);
%% final plot
figure(1),
imshow(outpic)
hold on
plot(xn,yn,'r','linewidth',2);
hold off
2 Comments
FLOREA
on 18 Mar 2025
Image Analyst
on 18 Mar 2025
Why not? Is it because you insist on using bwskel? If so, then why do you have to use that function?
Categories
Find more on Blue 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!

