为什么不使用polyfit
?
[y x] = find( bw ); %// get x y coordinates of all curve points
这里有两个小“技巧”需要使用:
You have two curves, thus you need to split your data points to the left and right curves
right = x<300;
xr = x(right);
yr = y(right);
xl = x(~right);
yl = y(~right);
Since your curves are close to vertical, it would be better to fit x=f(y)
rather than the "classic" y=f(x)
:
pr = polyfit( yr, xr, 3 ); %// fit 3rd deg poly
pl = polyfit( yl, xl, 3 );
yy = linspace( 1, size(bw,1), 50 );
figure; imshow(bw, 'border', 'tight' );
hold all
plot( polyval( pr, yy ), yy, '.-', 'LineWidth', 1 );
plot( polyval( pl, yy ), yy, '.-', 'LineWidth', 1 );
你会得到:
如果你想从估计的曲线创建一个新的精细掩模,可以按照以下步骤操作:
yy = 1:size(bw,1); %// single value per row
xxr=polyval(pr,yy); %// corresponding column values
xxl=polyval(pl,yy);
设置一个相同尺寸的新掩模
nbw = false(size(bw));
nbw( sub2ind(size(bw),yy,round(xxr)) )=true;
nbw( sub2ind(size(bw), yy, round(xxl) )) = true;
结果如下
figure;imshow(nbw,'border','tight');
(x,y)
坐标,然后拟合一个函数[x;y]=f(t)
。 - Jonas