Converting the outline of an image into a curved function
12 views (last 30 days)
I want to convert the contour (or skeleton) on the binary image into a curve (or function).
The reason is that tangent and normal lines are required at each part of the curve.
The image below explains the content of the question.
I need your help.
yanqi liu on 19 Nov 2021
clc;clear all;close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/805454/image.jpeg');
bw = imbinarize(rgb2gray(img));
bt = bw;
bw = ~bw;
bw = imclose(bw, strel('square', 5));
c = repmat(round(size(bw,2)*0.3), size(bw,1), 1);
r = 1:size(bw,1);
bw2 = bwselect(bw, c, r);
bw2 = imerode(bw2, strel('square', 5));
[L,~] = bwlabel(bw2);
stats = regionprops(L);
rect1 = round(stats(1).BoundingBox);
rect2 = round(stats(2).BoundingBox);
bw1 = imcrop(bt, rect1); bw1 = imclose(bw1, strel('line', 11, 90));
bw2 = imcrop(bt, rect2); bw2 = imclose(bw2, strel('line', 11, 90));
% curve fit
[y,x] = find(bw2);
[y,ind] = sort(y);
[y,ia,ic] = unique(y);
x = x(ia);
[fitobject,~,~] = fit(y,x,'smoothingspline');
figure; imshow(bw2, );
x2 = fitobject(y);
hold on; plot(x2, y,'r-','LineWidth',2);