Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

undistortion of a camera image with remap

Hi,

I need an undistorted image of a camera for an AR application. cv::undistort is too slow for my purpose, so I want to try initUndistortRectifyMap and remap to do the init only once and safe computational time. Here is my first test:

//create source matrix
cv::Mat srcImg(res.first, res.second, cvFormat, const_cast<char*>(pImg));

cv::Mat cam(3, 3, cv::DataType<float>::type);
cam.at<float>(0, 0) = 528.53618582196384f;
cam.at<float>(0, 1) = 0.0f;
cam.at<float>(0, 2) = 314.01736116032430f;

cam.at<float>(1, 0) = 0.0f;
cam.at<float>(1, 1) = 532.01912214324500f;
cam.at<float>(1, 2) = 231.43930864205211f;

cam.at<float>(2, 0) = 0.0f;
cam.at<float>(2, 1) = 0.0f;
cam.at<float>(2, 2) = 1.0f;

cv::Mat dist(5, 1, cv::DataType<float>::type);  
dist.at<float>(0, 0) = -0.11839989180635836f;
dist.at<float>(1, 0) = 0.25425420873955445f;
dist.at<float>(2, 0) = 0.0013269901775205413f;
dist.at<float>(3, 0) = 0.0015787467748277866f;
dist.at<float>(4, 0) = -0.11567938093172066f;

cv::Mat map1, map2;
cv::initUndistortRectifyMap(cam, dist, cv::Mat(), cam, cv::Size(res.second, res.first), CV_32FC1, map1, map2);

cv::remap(srcImg, *m_undistImg, map1, map2, cv::INTER_CUBIC);

At first, I create an opencv matrix with my image (format is BGRA), then I create the camera and distortion matrix. After this, I call initUndistortRectifyMap and then remap. As you can see in screen.jpg the camera image is wrong. I have no idea whats the problem. Any suggestions? What's wrong in my code?

Best regards

Pellaeon