2

I want to undistort a camera image. The undistort function of OpenCV is too slow, so I want to split it like mentioned in the documentation into the 2 calls of initUndistortRectifyMap (as init step) and remap (in the render loop).
At first, I tried a test program with the principal approach:

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

//fill matrices
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);

The format of my camera image is BGRA. The code compiles and starts, but the resulting image is wrong:
enter image description here

Any ideas, what's wrong with my code?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Gilad Pellaeon
  • 111
  • 1
  • 1
  • 11
  • 1
    As a note for anyone finding this in future, if you see this sort of thing in images, (a sort of repeating pattern) its usually something to do with the order that you put the output bytes. Here there are 4 bytes per pixel (RGBA) and 4 semi-repeating panels. – MichaelCG8 Oct 28 '22 at 11:36

1 Answers1

4

It works, yes. To be honest, I don't remember exactly what the problem was. I interchanged width and height or somethink like that.

This is my running code:

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

//look if an update of the maps is necessary
if ((resolution.first != m_width) || (m_height != resolution.second))
{
    m_width  = resolution.first;
    m_height = resolution.second;

    cv::initUndistortRectifyMap(*m_camData, *m_distData, cv::Mat(), *m_camData, cv::Size(resolution.first, resolution.second), CV_32FC1, *m_undistMap1, *m_undistMap2);
}

//create undistorted image
cv::remap(srcImg, *m_undistortedImg, *m_undistMap1, *m_undistMap2, cv::INTER_LINEAR);

return reinterpret_cast<unsigned char*>(m_undistortedImg->data);
Gilad Pellaeon
  • 111
  • 1
  • 1
  • 11