Search code examples
c++opencvtriangulation

How to properly define arguments to use in triangulatePoints (opencv)?



I try to use triangulatePoints from opencv but I think I do something wrong (I read one of the question about triangulatePoints on stackoverflow but not everything I understand). Assume that I have one point coords - pt1 and pt2 that correspond to coordinates of one point in left and right camera. Pt1 and pt2 is cv::Point.

So I have:

cv::Mat cam0(3, 4, CV_64F, k_data1)   //k_data1 is [R|t] 3x4 matrix for left camera
cv::Mat cam1(3, 4, CV_64F, k_data2)   //k_data2 is [R|t] 3x4 matrix for right camera
cv::Point pt1;                        //for left camera
cv::Point pt2                         //for right camera

I also define

cv::Mat pnt3D(1, 1, CV_64FC4).

And my question is how to properly define this two points (cv::Point)?
I have tried to do this:

cv::Mat_<cv::Point> cam0pnts;
cam0pnts.at<cv::Point>(0) = pt1;
cv::Mat_<cv::Point> cam1pnts;
cam1pnts.at<cv::Point>(0) = pt2;

But app throws some exceptions, so maybe I do something wrong.

EDIT:

Ok, with some help from @Optimus 1072 I corrected some lines of code and I get something like this:

double pCam0[16], pCam1[16];

cv::Point pt1 = m_history.getPoint(0);
cv::Point pt2 = m_history.getPoint(1);
m_cam1.GetOpenglProjectionMatrix(pCam0, 640, 480);
m_cam2.GetOpenglProjectionMatrix(pCam1, 640, 480);
cv::Mat cam0(3, 4, CV_64F, pCam0); 
cv::Mat cam1(3, 4, CV_64F, pCam1); 

vector<cv::Point2f> pt1Vec;
vector<cv::Point2f> pt2Vec;
pt1Vec.push_back(pt1);
pt2Vec.push_back(pt2);

cv::Mat pnt3D(1,1, CV_64FC4);

cv::triangulatePoints(cam0, cam1, pt1Vec, pt2Vec, pnt3D);

But still I get an exception:

...opencv\opencv-2.4.0\opencv\modules\calib3d\src\triangulate.cpp:75: error: (-209) Number of proj points coordinates must be == 2


Solution

  • Finally this works:

    cv::Mat pointsMat1(2, 1, CV_64F);
    cv::Mat pointsMat2(2, 1, CV_64F);
    
    int size0 = m_history.getHistorySize();
    
    for(int i = 0; i < size0; i++)
    {
      cv::Point pt1 = m_history.getOriginalPoint(0, i);
      cv::Point pt2 = m_history.getOriginalPoint(1, i);
    
      pointsMat1.at<double>(0,0) = pt1.x;
      pointsMat1.at<double>(1,0) = pt1.y;
      pointsMat2.at<double>(0,0) = pt2.x;
      pointsMat2.at<double>(1,0) = pt2.y;
    
      cv::Mat pnts3D(4, 1, CV_64F);
    
      cv::triangulatePoints(m_projectionMat1, m_projectionMat2, pointsMat1, pointsMat2, pnts3D);
    }