适合线 – MatLab不同意O​​penCV

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

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

我们怎样才能从两个图书馆获得不同的信息?

OpenCV代码 – (在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正试图将给定inputx y误差最小化(例如,如果x是独立的, y您的因variables)。

在这种情况下,通过点(10,10)和(20,20)的线可能是最好的select。 如果您尝试计算x=10 y值,那么接近x=20所有三个点的近垂直线会有非常大的误差。

虽然我不认识OpenCV语法,但我猜测CV_DIST_L2是一个距离度量,这意味着您要尽量减less线和xy平面中每个点之间的总距离。 在这种情况下,通过点集中间的更加垂直的线将是最接近的。

哪个“正确”取决于你的观点。