Italiano - English

Solve system of linear equations with OpenCV: the 3 point parabola

The OpenCV library is one of the most relevant open source library for Computer Vision but it contains some very useful function for linear algebra and matrix manipulation. Here is shown how to use cv::solve function to solve system of linear equation. As real example here is considered the case of parabola passes through 3 points.

A system of linear equations with OpenCV

Generally speaking a system of linear equations of n variables in m equation is in the form:

A x = B

where

  • A is the matrix of coefficients with n cols and m rows
  • B is the vector of constants with 1 col and  m rows
  • x is the vector of variables with 1 col and m rows

Matrix form for the above linear system is:

Applying this with OpenCV for the case of 3 equations with 3 variables:

 
// the matrix of coefficients
cv::Mat A = (cv::Mat_<float>(3,3) <<
                   a11, a12, a13,
                   a21, a22, a23,
                   a31, a32, a33);
 
//the vector of constants
cv::Mat B = (cv::Mat_<float>(3,1) <<
                   b1, 
                   b2,
                   b3);
 
//the vector of variables (results)
cv::Mat x;
 

and now OpenCV solves the system:

 
cv::solve(A, B, x);
// printout the result
cout << "Result: " << x << endl;

A second way to solve the system is using the inverse matrix:

x = A-1 B

write this in OpenCV:

 
cv::Mat xinv = A.inv() * B;
// printout the result
cout << "Result: " << xinv << endl;

that's all !

The case of parabola given 3 points

A parabola passes through 3 points

Let's go to a real case. We want to calculate the coefficient a, b, c for the equation of a parabola where 3 points are given.

General equation for the parabola is:

The set of parabolas passes through the point P1(x1, y1) is:

Get the parabola passes trough 3 point P1(x1, y1), P2(x2, y2), P3(x3, y3) from solution to the following system:

Is this a system of linear equations ? YES !

We will use the technique as above to solve the system and find values for  a, b, c.

The coefficients for the linear system are the 3 points “x, y” while the variables for the system are the parabola coefficient a, b, c

Write now system for the 3 points parabola in useful form:

  1. Write the system in OpenCV for points P1(1, 0), P2(0, 2), P3(3, 2) :
     
      //set here your 3 points for the parabola
      pt1 = cv::Point2f(1,0);
      pt2 = cv::Point2f(0,2);
      pt3 = cv::Point2f(3,2);
     
      cv::Mat A = (cv::Mat_<float>(3, 3) <<
          std::pow(pt1.x, 2), pt1.x, 1,
          std::pow(pt2.x, 2), pt2.x, 1,
          std::pow(pt3.x, 2), pt3.x, 1);
     
      cv::Mat B = (cv::Mat_<float>(3, 1) <<
          pt1.y,
          pt2.y,
          pt3.y);
     
     // declare a vector for results
      cv::Mat abc;
  2. Solve the system:
     
    cv::solve(A, B, abc);
  3. Printout the result:
     
    cout << "Coefficients:" << endl << abc << endl;
    returns:
    Coefficients:
    [1;
    -3;
    2]
  4. Printout the equation:
     
      a = abc.at<float>(0);
      b = abc.at<float>(1);
      c = abc.at<float>(2);
      cout << "Equation: y = " << a << "x^2 + " << b << "x + " << c;
    returns:
    Equation: y = 1x^2 + -3x + 2

That's all. OpenCV is really nice !

The full code for the case of 3 point parabola

 
#include <opencv2\core.hpp>
using namespace std;
 
int main(int argc, char *argv[])
{
    cv::Point2f pt1, pt2, pt3; ///< 3 points for the parabola
    double a, b, c;            ///< Solved coefficients for the parabola equation
 
    //set here your 3 points for the parabola
    pt1 = cv::Point2f(1, 0);
    pt2 = cv::Point2f(0, 2);
    pt3 = cv::Point2f(3, 2);
 
    // OpenCV requires the linear system in the form Ax = B
    //
    // the linear system for 3points parabola is
    //
    // aX^2 + bX + c = Y ---> | a(pt1.x)^2 + b(pt1.x) + c(1) = pt1.y |
    //                        | a(pt2.x)^2 + b(pt2.x) + c(1) = pt2.y |
    //                        | a(pt3.x)^2 + b(pt3.x) + c(1) = pt3.y |
    // coefficients for the system are the 3 points
    // variables for the system are the parabola coefficient a,b,c
    //
    // Finally set the matrix for the linear system solver
    cv::Mat A = (cv::Mat_<float>(3, 3) <<
        std::pow(pt1.x, 2), pt1.x, 1,
        std::pow(pt2.x, 2), pt2.x, 1,
        std::pow(pt3.x, 2), pt3.x, 1);
 
    cv::Mat B = (cv::Mat_<float>(3, 1) <<
        pt1.y,
        pt2.y,
        pt3.y);
 
    // declare a vector for results
    cv::Mat abc;
 
    // solve the linear system
    cv::solve(A, B, abc);
 
    // printout the result
    cout << "Coefficients:\n " << abc << endl;
    a = abc.at<float>(0);
    b = abc.at<float>(1);
    c = abc.at<float>(2);
    cout << "Equation:\n y = " << a << "x^2 + " << b << "x + " << c << endl;
 
    return 0;
}
Vote this page:

1 Comments:

#1 Sent by androidusr 29-04-2014

rally easy ...opencv it's fantastic!

Leave your comment:

Note:
  • Your email email will not be visible or used in any way, and is not required
  • Please keep comments relevant
  • Any content deemed inappropriate or offensive may be edited and/or deleted
  • HTML code is not allowed. Please use BBCode to format your text
    [b]bold[/b], [u]underline[/u], [i]italic[/i], [code]code[/code]
The coding examples presented here are for illustration purposes only. The author takes no responsibility for end-user use
This work is property of Pk Lab. You can use it for free but you must retain author's copyright.