3

我想在这里为图像中的彩色像素簇生成一个多项式“拟合”

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

4

1 回答 1

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 Aw - y ||^2 以便我们可以使用它来预测 y = w 。X

这里有 2 个版本。一个是带有 l2 正则化的 vanilla 最小二乘,另一个是带有 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")

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

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

有关更多详细信息,请参见Matlab 中二次多项式曲率的度量

拟合曲线

于 2018-10-15T08:50:28.487 回答