我正在绘制图像轮廓点 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 点。有没有其他方法可以使用样条线填充那些缺失的点?
我也愿意接受其他寻找平滑边缘的想法。
谢谢你的帮助!
最佳答案
由于您的图像看起来平滑且采样良好,我建议您为每个边缘像素找到真实边缘的亚像素位置。有了这个,我们消除了对凸包的需要,这可能对您的特定图像有用,但不能推广到任意形状。
这是一些代码来完成我的建议。
% 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 处采样。这要实现起来要复杂得多。另外,对于单字母变量感到抱歉... :)
关于matlab - 通过插值在matlab中平滑图像轮廓,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49289534/