如何将线“拟合”到像素簇?

时间:2018-10-14 12:29:19

标签: image-processing image-segmentation data-fitting polynomial-approximations

我想为图像here中的彩色像素簇生成多项式“拟合”

(要点是我想测量该群集近似于一条水平线的数量)。 我考虑过使用grabit或类似的东西,然后将其视为图形中的点云。但是有直接在图像文件上执行此操作的更快的功能吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

这是一个Python实现。基本上,我们找到有色区域的所有(xi,yi)坐标,然后建立一个正则化最小二乘系统,在该系统中我们要找到权重向量(w0,...,wd),以使yi = w0 + w1 xi + w2 xi ^ 2 + ... + wd xi ^ d在最小二乘意义上“尽可能接近”。

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。 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")

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

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

有关其他详细信息,请参见A measure for the curvature of a quadratic polynomial in Matlab

fitted curve