Ask Your Question
1

Difference between undistort and initUndistortRectifyMap+remap?

asked 2014-09-24 06:38:28 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-05-20 10:19:34 -0600

MarcelDebout gravatar image

Hey FooBar!

I figured out, that changing the line:

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

to

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

should solve your problem.

The funciton

initUndistortRectifyMap

offers you to apply a transformation. The translation is given by the c_x and c_y values of your camera matrix.

edit flag offensive delete link more

Comments

Worked perfectly for me, thanks!

cvHobbyIst gravatar imagecvHobbyIst ( 2015-07-24 08:05:28 -0600 )edit

That's really correct

neurobot gravatar imageneurobot ( 2016-11-10 22:19:37 -0600 )edit

Question Tools

Stats

Asked: 2014-09-24 06:38:28 -0600

Seen: 9,243 times

Last updated: Sep 24 '14