matlab - 如何在 MATLAB 中测量图像掩码中线段的长度?
问题描述
我有一个二进制图像(BW_roi.mat),如下所示。我想测量每条线段的长度。
我尝试了此链接hough-transform中给出的解决方案。但这对我不起作用。它只是测量了一些线的长度,如下所示。
我尝试了其他代码
clc; clear all; close all;
load BW_ROI.mat
boundaries = bwboundaries(BW_roi_nodot);
patchno=1 %Let select patch 1
b = boundaries{patchno}; % Extract N by 2 matrix of (x,y) locations.
x = b(:, 1);
y = b(:, 2);
它虽然给了我构成这些多边形的点(x,y)。但是我怎样才能得到特定补丁的线段长度呢?
解决方案
我建议将凸包与此处的算法结合使用,以减少检测到的多边形中的顶点数。似乎工作得很好。我将留给您使用由 产生的邻接矩阵bwboundaries
来丢弃重复项。
load BW_ROI.mat
[B,L,N,A] = bwboundaries(BW_roi_nodot);
for k = 1:N
boundary = fliplr(B{k});
% take a convex hull of the bounded polygon
idxn = convhull(boundary, 'Simplify', true);
% use this algorithm to further simplify the polygon
% https://ww2.mathworks.cn/matlabcentral/fileexchange/41986-ramer-douglas-peucker-algorithm
point_reduced = DouglasPeucker(boundary(idxn,:), 5);
% make the shape, get the side lengths
shp = polyshape(point_reduced);
vertex_deltas = diff([shp.Vertices; shp.Vertices(1,:)], 1);
edge_lengths = sqrt(sum(vertex_deltas.^2, 2));
% plot animation
imshow(BW_roi_nodot), hold on
plot(shp), drawnow, pause(0.5)
end
推荐阅读
- java - JFrame 渲染关闭
- javascript - 提取对象中键的唯一值 - Javascript/d3 图例
- java - 在 Spring Boot 中拦截 SSLHandshakeException
- swift - 模拟器和电话不显示相同的匹配几何效果结果
- java - 如何在 Spring Webflux 运行时获取服务器端口?
- javascript - 更改过滤器时使用错误的道具反应组件渲染
- python - Python setup config install_requires “良好实践”
- javascript - html自定义选择onchange事件未触发
- php - 尝试在 Laravel 5.4.36 上安装 Cashier
- python - 未解决的属性错误未显示在 vs 代码中