Ask Your Question

apalomer's profile - activity

2017-05-10 05:04:03 -0600 asked a question undistortPoints not giving the exact inverse of distortion model.

Hello,

I was doing some tests using the distortion model of OpenCV. Basically what I did is, implement the distortion equations and see if the cv::undistortPoints function gives me the inverse of these equations. I realized that cv::undistortPoints does not exactly give you the inverse of the distortion equations. When I saw this, I went to the implementation of cv::undistortPoints and realized that in the end condition of the iterative process of computing the inverse of the distortion model, OpenCV always does 5 iterations (if there are no distortion coefficients provided to the function it actually does 0 iterations) and does not use any error metric on the undistorted point to see if it is precisely undistorted. Haveing this in mind, I copied and modified the termination condition of the iteration process to take and error metrics into account. This gave me the exact inverse of the distortion model. The code showing this is attached at the end of this post. My question is:

Does this happen because OpenCV prefers performance (spending a bit less time) over accuracy (spending a bit more time) or is this just a "bug"? (it is obvious that with the termination condition that I propose the function will take more time to undistort each point)

Thank you very much!

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;

// This is a copy of the opencv implementation
void cvUndistortPoints_copy( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatrix,
                   const CvMat* _distCoeffs,
                   const CvMat* matR, const CvMat* matP )
{
    double A[3][3], RR[3][3], k[8]={0,0,0,0,0,0,0,0}, fx, fy, ifx, ify, cx, cy;
    CvMat matA=cvMat(3, 3, CV_64F, A), _Dk;
    CvMat _RR=cvMat(3, 3, CV_64F, RR);
    const CvPoint2D32f* srcf;
    const CvPoint2D64f* srcd;
    CvPoint2D32f* dstf;
    CvPoint2D64f* dstd;
    int stype, dtype;
    int sstep, dstep;
    int i, j, n, iters = 1;

    CV_Assert( CV_IS_MAT(_src) && CV_IS_MAT(_dst) &&
        (_src->rows == 1 || _src->cols == 1) &&
        (_dst->rows == 1 || _dst->cols == 1) &&
        _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 &&
        (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) &&
        (CV_MAT_TYPE(_dst->type) == CV_32FC2 || CV_MAT_TYPE(_dst->type) == CV_64FC2));

    CV_Assert( CV_IS_MAT(_cameraMatrix) &&
        _cameraMatrix->rows == 3 && _cameraMatrix->cols == 3 );

    cvConvert( _cameraMatrix, &matA );

    if( _distCoeffs )
    {
        CV_Assert( CV_IS_MAT(_distCoeffs) &&
            (_distCoeffs->rows == 1 || _distCoeffs->cols == 1) &&
            (_distCoeffs->rows*_distCoeffs->cols == 4 ||
             _distCoeffs->rows*_distCoeffs->cols == 5 ||
             _distCoeffs->rows*_distCoeffs->cols == 8));

        _Dk = cvMat( _distCoeffs->rows, _distCoeffs->cols,
            CV_MAKETYPE(CV_64F,CV_MAT_CN(_distCoeffs->type)), k);

        cvConvert( _distCoeffs, &_Dk );
        iters = 5;
    }

    if( matR )
    {
        CV_Assert( CV_IS_MAT(matR) && matR->rows == 3 && matR->cols == 3 );
        cvConvert( matR, &_RR );
    }
    else
        cvSetIdentity(&_RR);

    if( matP )
    {
        double PP[3][3];
        CvMat _P3x3, _PP=cvMat(3, 3, CV_64F, PP);
        CV_Assert( CV_IS_MAT(matP) && matP->rows == 3 && (matP->cols == 3 || matP->cols == 4));
        cvConvert( cvGetCols(matP, &_P3x3, 0, 3), &_PP );
        cvMatMul( &_PP, &_RR, &_RR );
    }

    srcf = (const CvPoint2D32f*)_src->data.ptr;
    srcd = (const CvPoint2D64f*)_src->data.ptr;
    dstf = (CvPoint2D32f*)_dst->data.ptr;
    dstd = (CvPoint2D64f*)_dst->data.ptr;
    stype = CV_MAT_TYPE(_src->type);
    dtype = CV_MAT_TYPE(_dst->type);
    sstep ...
(more)
2017-05-09 11:36:53 -0600 received badge  Student (source)
2017-05-09 11:35:23 -0600 received badge  Editor (source)
2017-05-09 10:01:10 -0600 asked a question How is undistortPoints solved

Hello,

I was trying to implement a different function to undistort points (to test potential new distortion models) and I came across the implementation that OpenCV has here. There, in the for loop (here) it actually computes the undistorted point. Can somebody tell me how does it solve the equation system I have tried to identify it to some iterative solutions (least squares, Newton-Rapson...) and I don't see how it does it.

Thank you very much!

2016-09-12 12:33:49 -0600 received badge  Supporter (source)
2016-08-11 20:59:50 -0600 received badge  Scholar (source)
2016-07-07 14:58:04 -0600 asked a question Aruco + Opencv 2.4.10

Hello

Please forgive me if my question is stupid.

I am trying to detect a marker to compute the relative position of the camera. I've read that for this the Aruco library is the best. Moreover, I've seen that it is integrated with opencv 3. Everytime I try to change opencv I end up formating my computer, so I do not want to move to opencv3. I downloade the verison 2.0.7 of the aruco library. I compiled it and install it without problem. With this, I've been able to create markers of many tipes using the utility provided aruco_print_dictironary. Now I want to detect this marker that I've printed but I don't know what to do. Ideally I would like to know the pixel detections of the marker so I can compute the position of the camera using my own algorithm (that I am developping for academic purposes).

Any hint on how can I get this pairs of 2D-3D points (imagin that I want to solve the camera position using cv::solvePnP instead of the aruco camera position solver).

Thank you!!

2016-06-21 10:55:23 -0600 asked a question Detecting corners of partially ocluded chessboard

Hello,

For an application that I am working with, I am interested in detecting the chessboard corners regardless of weather or not the chessboard is fully visible. How can I detect all the corners of the chess in the image? How does the corners detection work in OpenCV. Could I reuse some part?

Thank you.