Merge pull request #1529 from vpisarev:polyfit_accuracy_2

pull/1541/head^2
Roman Donchenko 11 years ago committed by OpenCV Buildbot
commit 4c7ecf20d8
  1. 47
      modules/contrib/src/polyfit.cpp

@ -47,26 +47,37 @@
#include "precomp.hpp" #include "precomp.hpp"
typedef double polyfit_type;
void cv::polyfit(const Mat& src_x, const Mat& src_y, Mat& dst, int order) void cv::polyfit(const Mat& src_x, const Mat& src_y, Mat& dst, int order)
{ {
CV_Assert((src_x.rows>0)&&(src_y.rows>0)&&(src_x.cols==1)&&(src_y.cols==1) const int wdepth = DataType<polyfit_type>::depth;
&&(dst.cols==1)&&(dst.rows==(order+1))&&(order>=1)); int npoints = src_x.checkVector(1);
Mat X; int nypoints = src_y.checkVector(1);
X = Mat::zeros(src_x.rows, order+1,CV_32FC1);
Mat copy; CV_Assert(npoints == nypoints && npoints >= order+1);
for(int i = 0; i <=order;i++)
Mat srcX = Mat_<polyfit_type>(src_x), srcY = Mat_<polyfit_type>(src_y);
Mat X = Mat::zeros(order + 1, npoints, wdepth);
polyfit_type* pSrcX = (polyfit_type*)srcX.data;
polyfit_type* pXData = (polyfit_type*)X.data;
int stepX = (int)(X.step/X.elemSize1());
for (int y = 0; y < order + 1; ++y)
{ {
copy = src_x.clone(); for (int x = 0; x < npoints; ++x)
pow(copy,i,copy); {
Mat M1 = X.col(i); if (y == 0)
copy.col(0).copyTo(M1); pXData[x] = 1;
else if (y == 1)
pXData[x + stepX] = pSrcX[x];
else pXData[x + y*stepX] = pSrcX[x]* pXData[x + (y-1)*stepX];
}
} }
Mat X_t, X_inv;
transpose(X,X_t); Mat A, b, w;
Mat temp = X_t*X; mulTransposed(X, A, false);
Mat temp2; b = X*srcY;
invert (temp,temp2); solve(A, b, w, DECOMP_SVD);
Mat temp3 = temp2*X_t; w.convertTo(dst, std::max(std::max(src_x.depth(), src_y.depth()), CV_32F));
Mat W = temp3*src_y;
W.copyTo(dst);
} }

Loading…
Cancel
Save