如何在 MATLAB 中测量图像蒙版中线段的长度?

How can I measure the length of line segments in image mask in MATLAB?

我有一个二进制图像 (BW_roi.mat),显示如下。我想测量每条线段的长度。

我尝试了这个 link 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)。但是如何获取具体patch的线段长度呢?

我建议将凸包与算法 here 结合使用,以减少检测到的多边形中的顶点数量。似乎工作得很好。我将留给您使用 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