本文介绍了拟合一条线--MatLab不同意O​​penCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取样点(10,10),(20,0),(20,40),(20,20)。

Take sample points (10,10), (20,0), (20,40), (20,20).

在Matlab中,polyfit返回斜率1,但对于相同的数据,openCV fitline返回斜率10.7。通过手工计算,近垂直线(斜率10.7)是一个更好的最小二乘拟合。

In Matlab polyfit returns slope 1, but for the same data openCV fitline returns slope 10.7. From hand calculations the near vertical line (slope 10.7) is a much better least squares fit.

为什么我们从两个库得到不同的线?

How come we’re getting different lines from the two libraries?

OpenCV代码 - (在iOS上)

OpenCV code - (on iOS)

vector<cv::Point> vTestPoints;
vTestPoints.push_back(cv::Point( 10, 10 ));
vTestPoints.push_back(cv::Point( 20, 0 ));
vTestPoints.push_back(cv::Point( 20, 40 ));
vTestPoints.push_back(cv::Point( 20, 20 ));
Mat cvTest = Mat(vTestPoints);
cv::Vec4f testWeight;
fitLine( cvTest, testWeight, CV_DIST_L2, 0, 0.01, 0.01);
NSLog(@"Slope: %.2f",testWeight[1]/testWeight[0]);

xcode日志显示

2014-02-12 16:14:28.109 Application[3801:70b] Slope: 10.76

Matlab代码

>> px
px =  10 20 20 20

>> py
py = 10 0 20 40

>> polyfit(px,py,1)
ans = 1.0000e+000 -2.7733e-014


推荐答案

对于给定的输入,MATLAB试图最小化 y 中的错误 x (即好像 x 是你的独立和 y 你的因变量)。

MATLAB is trying to minimise the error in y for an given input x (i.e. as if x is your independent and y your dependant variable).

在这种情况下,通过积分(10,10)和(20,20)的线可能是最好的选择。如果您尝试计算 y x = 20 的近垂直线会出现非常大的错误/ code>给定 x = 10

In this case, the line that goes through the points (10,10) and (20,20) is probably the best bet. A near vertical line that goes close to all three points with x=20 would have a very large error if you tried to calculate a value for y given x=10.

虽然我不认识OpenCV语法,但我我猜那个 CV_DIST_L2 是一个距离度量,这意味着你试图最小化线与xy平面中每个点之间的总距离。在这种情况下,通过点集中间的更垂直线将是最接近的。

Although I don't recognise the OpenCV syntax, I'd guess that CV_DIST_L2 is a distance metric that means you're trying to minimise overall distance between the line and each point in the x-y plane. In that case a more vertical line which passes through the middle of the point set would be the closest.

哪个正确取决于您的点代表什么。

Which is "correct" depends on what your points represent.

这篇关于拟合一条线--MatLab不同意O​​penCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:30