Ask Your Question

Alek.B's profile - activity

2017-09-01 17:14:25 -0500 received badge  Student (source)
2016-07-18 17:48:33 -0500 commented answer Undistort image before estimating pose using solvePnP

Thanks. I got it.

2016-07-18 17:47:37 -0500 received badge  Scholar (source)
2016-07-18 16:22:00 -0500 asked a question Undistort image before estimating pose using solvePnP

I have to estimate the pose of the camera using a known marker. The camera is calibrated and I have all the calibration coefficients.

The current version of the algorithm extracts 4 co-planar points from the frame and uses them to estimate the pose using the solvePnP function.

The algorithms seems to work fine but I have a doubt. Since the solvePnP takes as input also the calibration coefficients, do I need to undistort the image before looking the 4 points?

In the following code, are the initUndistortRectifyMap/remap functions necessary?

while(1) {

    frame = camera->getFrame();

    imshow("frame", frame);

    // Estimation of Map1 and Map2 for image rectification (to be done only on the first iteration)
    if (initRectifyMap_flag)
        // Rectify the image
        // The initialization of the Rectification Parameters will be carried out only at the first frame.
        initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(), getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, frame.size(), 1, frame.size(), 0), frame.size(), CV_16SC2, map1, map2);
        initRectifyMap_flag = false;

    // Remapping of the current frame
    remap(frame, src, map1, map2, INTER_LINEAR, BORDER_TRANSPARENT, Scalar::all(255));

    // Here is the code for the extraction of the 4 points based on the content of the variable src

    // Pose estimation
    solvePnP(Mat(refMarkerPoint), Mat(markerPoints), cameraMatrix, distCoeffs, rvec, tvec,false);
2016-02-11 00:46:57 -0500 asked a question solvePnpRansac GPU assertion failed

I'm trying to use the gpu version of solvePnPRansac to estimate the pose of the camera, but I get the following error:

OpenCV Error: Assertion failed (object.rows == 1 && object.cols > 0 && object.type() == CV_32FC3) in cv::gpu::solvePnPRansac, file C:\opencv\sources\modules\gpu\src\calib3d.cpp, line 218

If I run my code using the solvePnP or cpu-based solvePnPRansac, everything works fine.

I took a look into the code of calib3d.cpp at line 218 and the problem seems to be related to the first parameter of the function (that should be the list of the reference points).

It seems that the format that I use to send the reference point to the function is wrong, but I didn't figure out which one is correct.

Here is my code:

// Reference Points and Detected Points for Pose Estimation
std::vector<cv::Point3f> refMarkerPoint;
std::vector<cv::Point2f> markerPoints;


cv::Mat rvec = cv::Mat::zeros(1, 3, CV_32F);
cv::Mat tvec = cv::Mat::zeros(1, 3, CV_32F);
cv::Mat rotation = cv::Mat::zeros(1, 3, CV_32F);
cv::Mat tvec2 = cv::Mat::zeros(1, 3, CV_32F);
Vec3d eulerAngles;


if (cx_right != 0 && cx_left != 0 && markerPoints.size() > 0)
    marker_detected = 1;
    //solvePnPRansac(refMarkerPoint, markerPoints, cameraMatrix, distCoeffs, rvec, tvec, false, 20, 4, 20, noArray(), CV_ITERATIVE); // CV_EPNP, CV_P3P
    //solvePnP(Mat(refMarkerPoint), Mat(markerPoints), cameraMatrix, distCoeffs, rvec, tvec);
   gpu::solvePnPRansac(Mat(refMarkerPoint), Mat(markerPoints), cameraMatrix, distCoeffs, rvec, tvec);

    cv::Rodrigues(rvec, rotation);


Do you have any suggestions?