我正在绘制图像轮廓点using this threshold method,但我的轮廓有直线段。我想在每个点绘制垂直角度,所以我真的需要曲线。
我可以使用凸包来获得平滑的曲线。
图像生成如下:
B = bwboundaries(BW3);
outline = B{1,1};
plot(outline(:,2),outline(:,1),'r.','LineWidth',1)
K = convhull(outline(:,2),outline(:,1));
plot(outline(K,2),outline(K,1),'b+--','LineWidth',1)
但我怎样才能填补空白"凸壳点之间?我想在每个红点的蓝色曲线上有一个点。
我尝试使用interp1来实现这个目标:
outline2 = outline;
outline2(:,2)=interp1(outline(K,1),outline(K,2),outline(:,1),'spline');
但出现以下错误: "使用griddedInterpolant时出错 网格向量必须包含唯一的点。"
我认为它是因为轮廓形成一个循环,而不是每个y的唯一x点。是否有不同的方法使用样条曲线填充缺失的点?
我也愿意接受其他想法以找到顺利的优势。
感谢您的帮助!
答案 0 :(得分:2)
由于您的图像看起来很平滑且采样效果很好,我建议您为每个边缘像素找到真实边缘的子像素位置。通过这种方式,我们不再需要凸包,这可能对您的特定图像有用,但不能推广到任意形状。
以下是一些完成我建议的代码。
% A test image in the range 0-1, the true edge is assumed to be at 0.5
img = double(gaussianedgeclip(60-rr));
% Get rough outline
p = bwboundaries(img>0.5);
p = p{1,1};
% Refine outline
n = size(p,1);
q = p; % output outline
for ii=1:n
% Find the normal at point p(ii,:)
if ii==1
p1 = p(end,:);
else
p1 = p(ii-1,:);
end
if ii==n
p2 = p(1,:);
else
p2 = p(ii+1,:);
end
g = p2-p1;
g = (g([2,1]).*[-1,1])/norm(g);
% Find a set of points along a line perpendicular to the outline
s = p(ii,:) + g.*linspace(-2,2,9)';
% NOTE: The line above requires newer versions of MATLAB. If it
% fails, use bsxfun or repmat to compute s.
v = interp2(img,s(:,2),s(:,1));
% Find where this 1D sample intersects the 0.5 point,
% using linear interpolation
if v(1)<0.5
j = find(v>0.5,1,'first');
else
j = find(v<0.5,1,'first');
end
x = (v(j-1)-0.5) / (v(j-1)-v(j));
q(ii,:) = s(j-1,:) + (s(j,:)-s(j-1,:))*x;
end
% Plot
clf
imshow(img,[])
hold on
plot(p(:,2),p(:,1),'r.','LineWidth',1)
plot(q(:,2),q(:,1),'b.-','LineWidth',1)
set(gca,'xlim',[68,132],'ylim',[63,113])
生成测试图像的第一行需要DIPimage,但其余代码仅使用标准MATLAB函数,但您使用的bwboundaries
除外,它们来自图像处理工具箱。
输出点集q
未在整数x或y处采样。完成这件事要复杂得多。
另外,抱歉单字母变量......:)
答案 1 :(得分:1)
使用行军广场(https://en.wikipedia.org/wiki/Marching_squares#Isoline)查找初始大纲。
然后,如果您想要对导数进行良好估计,请使用插值三次样条曲线(https://en.wikipedia.org/wiki/Spline_interpolation#Algorithm_to_find_the_interpolating_cubic_spline)。
这里有一点技术性:似乎你想要像素中心的斜率。但是从行进立方体获得的样条将通过已知点的边缘而不是通过中心。你可以
将中心点投影到样条曲线上最近的点(遗憾的是需要更高次多项式的求解);
隐式化立方弧并计算隐式函数的梯度。
如果您的准确度要求不严格,您可能只需在标记像素中使用分段方向即可。