取样点为(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
10.76
- 偏移量是多少? - nkjt