首页 > 解决方案 > 如何在 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)。但是我怎样才能得到特定补丁的线段长度呢?

标签: matlabimage-processingsignal-processingmatlab-figure

解决方案


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

推荐阅读