CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    68

    OpenCV solve equation

    Hello,

    I'm hoping that there is somebody out there who is familiar with OpenCV, as I have a very simple problem.

    I have a linear equation Ax = B which I want to solve. This is done with the OpenCV function solve(A, B, x). (A fourth argument can be used to specify the method for solving.) However, I am not getting the correct results.

    For example, consider this code for a very simple equation:

    Code:
    Mat A(2, 2, CV_32F);
    Mat B(2, 1, CV_32F);
    Mat x(2, 1, CV_32F);
    
    A.data[0] = 2;
    A.data[1] = 1;
    A.data[2] = 1;
    A.data[3] = 1;
    B.data[0] = 4;
    B.data[1] = 3;
    
    solve(A, B, x);
    
    cout << "x0 = " << (float)x.data[0] << ", x1 = " << (float)x.data[1] << endl;
    This is solving the following equation:

    { 2 1 } { x0} = { 4 }
    { 1 1 } { x1} = { 3 }

    And I get the solution: x0 = 182, x1 = 62.

    This is clearly wrong. The correct solution should be: x0 = 1, x1 = 2.

    I have tried all the different solver types as the fourth argument, and they all return the same result.

    What am I doing wrong?

    Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: OpenCV solve equation

    If you have the source code, why not debug into the solve() function to see what it's doing?

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: OpenCV solve equation

    You are instructing the matrix to create itself as if it contains floats (CV_32F), but when you access the data pointer directly, its type is unsigned char*. Therefore, your assignments are not doing what you think they are doing.

    You have two options. Either you can use the setTo() or at<float>() method instead, which will do the type conversion, or you can use the Mat_<float> template rather than the Mat class, so that it knows its type at compile time and its operator() will do the write thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured