matlab - 如何在 Matlab 中使 bwboundaries 的端点保持一致?
问题描述
我有一个有 2 个边缘的图像
如果我然后用下面的代码绘制边缘的边界:
imshow(I); hold on; [B,L,N] = bwboundaries(I);
for k=1:length(B),
boundary = B{k};
BL=size(boundary);
plot(boundary(1,2), boundary(1,1), '*g','MarkerSize',15);
for j=1:10:BL(1)
plot(boundary(j,2), boundary(j,1), '.r','MarkerSize',5);
end
end
如上图所示,左边缘的起点(绿色星号)位于图像的左侧,这是我所期望的。但是,右边缘的起点朝向中间
显然这是因为bwboundaries
处理顺时针方向的跟踪对象,而需要逆时针跟踪第二个边缘才能在图像的右边界开始和结束
Matlab 如何能够从bwboundaries
右侧边缘获取位置并正确确定端点?
解决方案
不是您问题的完整答案,而是我提出的一个想法。您可以检查边界的所有点是否与图像边界“接近”,然后找到最小/最大值(x, y)
,这将描述您感兴趣的那些“末端”。
B = bwboundaries(img);
% Threshold for "closeness" to an image border.
thrNear = 5;
for k = 1:numel(B)
b = B{k};
nearTop = b(:, 1) < thrNear;
nearBottom = b(:, 1) > (size(img, 1) - thrNear);
nearLeft = b(:, 2) < thrNear;
nearRight = b(:, 2) > (size(img, 2) - thrNear);
closeToTop = b(nearTop, :)
closeToBottom = b(nearBottom, :)
closeToLeft = b(nearLeft, :)
closeToRight = b(nearRight, :)
end
例如,对于原始图像中的正确形状,您会得到:
closeToTop = [](0x2)
closeToBottom = [](0x2)
closeToLeft = [](0x2)
closeToRight =
79 283
79 284
79 285
79 286
79 287
80 287
81 287
81 286
81 285
81 284
81 283
215 283
215 284
215 285
215 286
215 287
216 287
217 287
217 286
217 285
217 284
217 283
现在,寻找最大值x
(287),并找到合适的(非相邻)y
值(79-81 vs. 215-217)。对每个图像边框重复此操作。
我希望,你明白我的想法。老实说,我不想完全实现它,但如果我的描述不够精确,请不要犹豫。
推荐阅读
- html - SCSS 覆盖显示 none 仍在显示
- node.js - NodeJs/Express Vue CLI3 应用程序错误无法设置未定义的属性渲染
- node.js - 用于组合数学的 Npm 包
- python - 我如何获得每个文本和语音通道并拒绝@所有人阅读它,然后允许另一个角色阅读和发送?
- c - c中相同结构的不同typedef
- java - 为什么输出中有负数?
- python-3.x - Opencv 视频再次读取和保存已损坏
- sockets - 使用 Scapy 解析 TCP DNS 请求
- haskell - 使用列表理解的 Haskell 中的完整甲板
- c++ - 如何将字符插入字符串向量