SHARE
TWEET

Untitled

a guest Apr 24th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Polynomial regression function
  3. cv::vector<double> fitPoly(cv::vector<cv::Point> points, int n) {
  4.  
  5.   //Number of points
  6.   int nPoints = points.size();
  7.  
  8.   // Vectors for all the points’ xs and ys
  9.   cv::vector<float> xValues = cv::vector<float>();
  10.   cv::vector<float> yValues = cv::vector<float>();
  11.  
  12.   // Split  the points into two vectors for x and y values
  13.   for(int i = 0; i < nPoints; i++) {
  14.     xValues.push_back(points[i].x);
  15.     yValues.push_back(points[i].y);
  16.   }
  17.  
  18.   //Augmented matrix
  19.   double matrixSystem[n+1][n+2];
  20.   for (int row = 0; row < n+1; row++) {
  21.     for (int col = 0; col < n+1; col++) {
  22.       matrixSystem[row][col] = 0;
  23.       for (int i = 0; i < nPoints; i++) matrixSystem[row][col] += pow(xValues[i], row + col);
  24.     }
  25.    
  26.     matrixSystem[row][n+1] = 0;
  27.     for (int i = 0; i < nPoints; i++) matrixSystem[row][n+1] += pow(xValues[i], row) ∗ yValues[i];
  28.   }
  29.  
  30.   //Array that holds all the coefficients
  31.   double coeffVec[n+2];
  32.  
  33.   //Gauss  reduction
  34.   for (int  i = 0;  i <= n − 1; i++)
  35.     for (int k = i + 1; k <= n ; k++) {
  36.       double t = matrixSystem[k][i] / matrixSystem[i][i];
  37.       for (int j = 0; j <= n+1; j++) matrixSystem[k][j] = matrixSystem[k][j] − t ∗ matrixSystem[i][j];
  38.     }
  39.  
  40.   // Back−substitution
  41.   for (int i = n; i >= 0; i−−) {
  42.     coeffVec[i] = matrixSystem[i][n+1];
  43.     for (int j = 0; j <= n+1; j++) if (j != i) coeffVec[i] = coeffVec[i] − matrixSystem[i][j] ∗ coeffVec[j];
  44.  
  45.     coeffVec[i] = coeffVec[i] / matrixSystem[i][i];
  46.   }
  47.  
  48.   // Construct the cv vector and return it
  49.   cv::vector<double> result = cv::vector<double>();
  50.  
  51.   for (int i = 0; i < n+1; i++) result.push_back(coeffVec[i]);
  52.   return result;
  53. }
  54.  
  55. // Returns  the  point  f o r  the  equation  determined
  56. //by a vector of coefficents, at a certain x location
  57. cv::Point pointAtX (cv::vector<double> coeff, double x) {
  58.   double y = 0;
  59.   for (int i = 0; i < coeff.size(); i++) y += pow(x, i) ∗ coeff[i];
  60.   return cv::Point(x, y);
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top