Ask Your Question

Revision history [back]

Difference between undistort and initUndistortRectifyMap+remap?

Hello! I am currently trying to speed up the my image acquisition by replacing a call to cv::undistort for every image with a single use of initUndistortRectifyMap and then only a cv::remap for every image.

Currently, my test callback looks like this:

        cv::Mat undistorted;
        cv::undistort(img,undistorted,camm,dist);

        Mat map1,map2;
        Mat newCamMatrix;
        cv::initUndistortRectifyMap(camm,dist,Mat(),newCamMatrix, cv::Size(img.cols,img.rows), CV_32FC1, map1,map2);

        cout << "old matrix" << endl << camm << endl;
        cout << "new matrix" << endl << newCamMatrix << endl;


        Mat undis2;
        cv::remap(img,undis2,map1,map2,cv::INTER_CUBIC);

        cv::imwrite("/opt/tmp/undis1.png", undistorted);
        cv::imwrite("/opt/tmp/undis2.png", undis2);

camm is here my camera matrix and dist my distortion vector. Both undistorted images look similar, but the second image (the remapped one) is shifted about 20pixels in vertical direction and shows a different part of the original image.

The undistort code can be found e.g. here: https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/undistort.cpp

Does anyone know why the undistortion is done in stripes? And more important: How can I use initUndistortRectifyMap to get the same result as with undistort and which is the correct result?