Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 我如何';安装一条直线';到一组像素?_Image Processing_Image Segmentation_Data Fitting_Polynomial Approximations - Fatal编程技术网

Image processing 我如何';安装一条直线';到一组像素?

Image processing 我如何';安装一条直线';到一组像素?,image-processing,image-segmentation,data-fitting,polynomial-approximations,Image Processing,Image Segmentation,Data Fitting,Polynomial Approximations,我想为图像中的彩色像素簇生成一个多项式“拟合” (重点是我想测量该簇与水平线的近似程度)。 我想使用grabit或类似的东西,然后将其视为图形中的点云。但是,有没有一个更快的函数可以直接在图像文件上执行此操作? 谢谢 下面是一个Python实现。基本上我们找到所有(席,彝)坐标的有色区域,然后建立一个正则最小二乘系统,我们要找到权重向量,(W0,…,WD),使得Y= W0+W1XI+W2席席2 + +…wd xi^d最小平方意义上的“尽可能接近” import numpy as np impor

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

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

下面是一个Python实现。基本上我们找到所有(席,彝)坐标的有色区域,然后建立一个正则最小二乘系统,我们要找到权重向量,(W0,…,WD),使得Y= W0+W1XI+W2席席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]
我们想找到最小化| | Aw-y | ^2的向量w 所以我们可以用它来预测y=w。x

这里有两个版本。一种是带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")
对于简单的直线拟合,请将
功能
的参数
顺序
设置为1。然后,您可以使用直线的坡度来了解它与水平线的距离(例如,通过检查其坡度的角度)

也可以将其设置为所需的任何多项式次数。我觉得3级看起来不错。在这种情况下,对应于x^3的系数绝对值的6倍(
w_未加权[3]
w_加权[3]
)是直线曲率的一种度量

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


您是否有基础数据或只是它的绘图?颜色是否有一些需要考虑的重要性(如权重)?为了补充@MarkSetchell所说的,如果你有生成绘图的数据,你可以得到明亮区域的坐标,然后用最小二乘法拟合多项式曲线或拟合样条曲线。谢谢,我没有数据。这就是为什么我想以某种方式抓住它。关于颜色,如果可能的话,我会使用强度(“火”标度)来衡量适合度,但考虑到轮廓的不规则性,即使使用二值图像(这里不是这种情况),我也会很高兴,在我看来,如果超过一立方,那就太过分了。非常感谢@lightalchest。我能问一下:我能不能再测量一下拟合线本身(长度和“像乌鸦一样”的长度)?我需要从图像中重新提取它吗?@matteoEO多项式的权重为
w\u加权
(或
w\u未加权
,取决于您使用的是哪种)。给定这些权重,可以使用数值方法计算曲线的长度。在实践中,我想你应该1)分割区域2)将多项式曲线拟合到每个区域,然后3)测量长度。