Fitting spline to outline

4 ビュー (過去 30 日間)
John Fullerton
John Fullerton 2019 年 8 月 7 日
コメント済み: Mehri Mehrnia 2022 年 6 月 20 日
I have been trying to write a code (shown below) that will identify the outline of an object and then fit a spline to it.
I can identify the edge (using a ball for example below, image 1), but it picks up multiple points around the edge for each x and y value (image 2). So, I've tried to clean it up be removing some of the points.
However, the final spline doubles back on itself (image 4). How do I stop that from happening and just plot a smooth line connecting all the points?
% Get outline
BW = edge(Image, 'Canny', 0.6);
B = bwboundaries(BW);
J = num2cell(cell2mat(B),1);
F(:,1) = J{1,1};
F(:,2) = J{1,2};
%Show image and outline points
figure
imshow(I)
hold on
scatter(F(:,1),F(:,2), 'r0')
%Reduce number of points
for q = 20:20:(numel(F)/2)
F1((q/20),:) = F(q,:);
end
% Fit spline
x = F1(:,1);
y = F1(:,2);
pointspline = [x'; y'];
values = cscvn(pointspline);
sp = fn2fm(values,'B-')
%Show reduced points and spline
figure
scatter(F1(:,1),F1(:,2), 'r')
hold on
fnplt(sp)
1.
Sphere_Edge.png
2.
Sphere_Edge_Close.png
3.
Ball_Spline.png
4.
Spline_Close.png
  2 件のコメント
darova
darova 2019 年 8 月 8 日
If the curve double back can you just use a half of data?
%Reduce number of points
% for q = 20:20:(numel(F)/2)
% F1((q/20),:) = F(q,:);
% end
% can be written as:
F1 = F(1:20:end/2,:);
Mehri Mehrnia
Mehri Mehrnia 2022 年 6 月 20 日
something is wrong with this code, I used it for a boundary of image. it shifts it 90 degrees.

サインインしてコメントする。

回答 (1 件)

Pujitha Narra
Pujitha Narra 2019 年 8 月 19 日
Hi John,
There is a similar example here:
Hope this helps.

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by