Matlab:跟踪一条闭合曲线
我有一个包含闭合曲线的图像。 我想以逆时针的方式追踪任何闭合曲线。我发现我可以使用Matlab:跟踪一条闭合曲线,matlab,curve-fitting,Matlab,Curve Fitting,我有一个包含闭合曲线的图像。 我想以逆时针的方式追踪任何闭合曲线。我发现我可以使用bwtraceboundary函数,但我不知道如何正确使用它 目前我的代码是: function [] = project() close all; origImg = imread('closed.png'); BW = im2bw(imcomplement(rgb2gray(origImg))); imshow(BW,[]); s = size(BW); for row = 2:15:s(1) for
bwtraceboundary
函数,但我不知道如何正确使用它
目前我的代码是:
function [] = project()
close all;
origImg = imread('closed.png');
BW = im2bw(imcomplement(rgb2gray(origImg)));
imshow(BW,[]);
s = size(BW);
for row = 2:15:s(1)
for col = 1:s(2)
if BW(row,col)
break;
end
end
contour = bwtraceboundary(BW,[row col],'W',8,1000,'counterclockwise');
if(~isempty(contour))
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2);
hold on;
plot(col,row,'gx','LineWidth',2);
else
hold on;
plot(col,row,'rx','LineWidth',2);
end
结束
目前我的代码没有跟踪“关闭”区域
知道如何继续吗?我给你带了代码并对其进行了注释,这样你就知道发生了什么 你从matlab的例子中得到了这些代码,但它并不像你想象的那样。您不需要所有的迭代,您可能希望修改代码以获得跟踪轮廓的更多点。因为我不想为你写代码,所以我认为向你解释你在做什么是个好主意,所以就在这里。你可以复制粘贴它,应该可以工作,你应该看到一些动画正在进行 代码:
谢谢!那么,有没有一种方法可以追踪所有的封闭曲线,包括内部曲线?@zinon这是一个完全不同的问题,也是一个更复杂的问题。我猜您希望您的代码能够遵循原始图像中的“蓝线”,包括内部循环。这显然不是严格的,如果可以的话,可能需要复杂的算法。试着找到图像的骨架之类的东西。你可能想提出一个新问题,说明如何追踪外圆而不是内圆,并寻求帮助。是的,我想沿着“蓝色”线的方向。嗯。您可能需要细化图像,构建一个图表,然后寻找一个新的图像。找到后,需要检查边阶为4的所有顶点,并检查曲线的第二可微性,可能需要交换一些边。
% READ IMAGE
origImg = imread('so.png');
% Create a binary image of the original image being white the contours and
% black the rest
BW = im2bw(imcomplement(rgb2gray(origImg)));
% show it
imshow(BW,[]);
% wait 2 seconds so you can see it
pause(2)
s = size(BW);
% Go iterating thougth rows and cols until you find something white
% (contour)
for row = 2:15:s(1)
for col = 1:s(2)
if BW(row,col)
break; %something white found!
end
end
% Trace that boundary! Starting in the white point (row,col)
contour = bwtraceboundary(BW,[row col],'W',8,200,'counterclockwise');
% Did we actually found a contour or its just empty (e.g. it was
% point)
if(~isempty(contour))
% There is a contour! lets plot it (we will only get a piece of it,
% because we specified that only 200 points of it will be used!
hold on;
% plot the contour in green
plot(contour(:,2),contour(:,1),'g','LineWidth',2);
hold on;
% plot the initial point of the conotur in blue
plot(col,row,'bx','LineWidth',2);
pause(1)
else
% If there was not a contour just put a red X on it
hold on;
plot(col,row,'rx','LineWidth',2);
end
end