如何提取适合粗线轮廓中间的线方程

How to extract a line equation that fit in the middle of a thick line contour

我检测到一条线轮廓,我想拟合一个线方程来描述它。

我尝试了最小二乘拟合,但是由于透视变形,线的一端较粗,因此线方程在一端漂移到一侧。

我也考虑过使用zhang-suen细化的方法,但是这样的算法对于简单的线来说似乎有点过头了

一个简单有效的方法是计算线上点的第一个principal component。这是matlab中的代码:

% Read image
im = imread('https://i.stack.imgur.com/pJ5Si.png');

% Binarize image and extract indices of line pixels
imbw = imbinarize(rgb2gray(im), 'global');  % Threshold with Otsu's method
[y, x] = ind2sub(size(imbw), find(imbw));   % Get indices of line pixels

% Extract first principal component
C = cov(x, y);                              % Compute covariance of x and y
coeff = pcacov(C);                          % Compute eigenvectors of C
vector_xy = [coeff(1,1), coeff(2,1)];       % Get fist principal component

% Plot
figure; imshow(im); hold on
xx = vector_xy(1) * [-1 1] * size(imbw,2) + mean(x(:));
yy = vector_xy(2) * [-1 1] * size(imbw,2) + mean(y(:));
plot(xx,yy,'c','LineWidth',2)
axis on, legend('Principal Axis','Location','NorthWest')

你可以用

得到线性方程y = a*x + b的系数
a = vector_xy(2) / vector_xy(1);
b = mean(y(:)) - a * mean(x(:));