我如何'fit a line'到一簇像素?

How do I 'fit a line' to a cluster of pixels?

我想为图像中的彩色像素簇生成一个多项式 'fit' here

(重点是我想测量该簇与水平线的近似程度)。 我想过使用 grabit 或类似的东西,然后将其视为图表中的点云。但是有没有更快的功能可以直接在图像文件上这样做? 谢谢!

这是一个 Python 实现。基本上我们找到彩色区域的所有 (xi, yi) 坐标,然后建立一个正则化最小二乘系统,我们想要找到权重向量,(w0, ..., wd) 使得 yi = w0 + w1 xi + w2 xi^2 + ... + wd xi^d "as close as possible" 在最小二乘意义上。

import numpy as np
import matplotlib.pyplot as plt

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

def feature(x, order=3):
    """Generate polynomial feature of the form
    [1, x, x^2, ..., x^order] where x is the column of x-coordinates
    and 1 is the column of ones for the intercept.
    """
    x = x.reshape(-1, 1)
    return np.power(x, np.arange(order+1).reshape(1, -1)) 

I_orig = plt.imread("2Md7v.jpg")
# Convert to grayscale
I = rgb2gray(I_orig)

# Mask out region
mask = I > 20

# Get coordinates of pixels corresponding to marked region
X = np.argwhere(mask)

# Use the value as weights later
weights = I[mask] / float(I.max())
# Convert to diagonal matrix
W = np.diag(weights)

# Column indices
x = X[:, 1].reshape(-1, 1)
# Row indices to predict. Note origin is at top left corner
y = X[:, 0]

我们想找到最小化||的向量w啊-y ||^2 这样我们就可以用它来预测 y = w 。 x

这里有2个版本。一个是带 l2 正则化的香草最小二乘法,另一种是带 l2 正则化的加权最小二乘法。

# Ridge regression, i.e., least squares with l2 regularization. 
# Should probably use a more numerically stable implementation, 
# e.g., that in Scikit-Learn
# alpha is regularization parameter. Larger alpha => less flexible curve
alpha = 0.01

# Construct data matrix, A
order = 3
A = feature(x, order)
# w = inv (A^T A + alpha * I) A^T y
w_unweighted = np.linalg.pinv( A.T.dot(A) + alpha * np.eye(A.shape[1])).dot(A.T).dot(y)
# w = inv (A^T W A + alpha * I) A^T W y
w_weighted = np.linalg.pinv( A.T.dot(W).dot(A) + alpha * \
                             np.eye(A.shape[1])).dot(A.T).dot(W).dot(y)

结果

# Generate test points
n_samples = 50
x_test = np.linspace(0, I_orig.shape[1], n_samples)
X_test = feature(x_test, order)

# Predict y coordinates at test points
y_test_unweighted = X_test.dot(w_unweighted)
y_test_weighted = X_test.dot(w_weighted)

# Display
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.imshow(I_orig)
ax.plot(x_test, y_test_unweighted, color="green", marker='o', label="Unweighted")
ax.plot(x_test, y_test_weighted, color="blue", marker='x', label="Weighted")
fig.legend()
fig.savefig("curve.png")

对于简单的直线拟合,将 feature 的参数 order 设置为 1。然后您可以使用直线的梯度来了解它与水平线的接近程度(例如,通过检查其倾斜角度)。

也可以将其设置为您想要的任何多项式次数。我发现3级看起来很不错。在这种情况下,x^3 对应的系数的绝对值的 6 倍(w_unweighted[3]w_weighted[3])是直线曲率的一种度量。

有关更多详细信息,请参阅