c++ - 正确使用 initUn DistorifyMap 并从 OpenCV 重新映射

标签 c++ image opencv distortion

我想要消除相机图像的扭曲。 OpenCV的不失真函数太慢了,所以我想像文档中提到的那样将其分成2个调用initUn DistreifyRectifyMap(作为初始化步骤)和remap(在渲染循环)。
首先,我尝试了一个测试程序,主要方法是:

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

我的相机图像的格式是BGRA。代码编译并启动,但生成的图像是错误的:
enter image description here

有什么想法吗,我的代码有什么问题吗?

最佳答案

它有效,是的。说实话,我不记得到底是什么问题了。我交换了宽度和高度或者类似的东西。

这是我的运行代码:

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

关于c++ - 正确使用 initUn DistorifyMap 并从 OpenCV 重新映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30913746/

相关文章:

opencv - opencv中matlab 'fix'的等效函数是什么?

python - 如何使用 Python OpenCV 计算图像中形状的面积?

c# - DataGridView 中的图像问题

html - CSS/HTML - 在手机上隐藏图像并使用替代文本

c++ - 我想要返回类型的指针,但我想让我的函数返回派生类的指针

c# - 在屏幕上检索凝视坐标

opencv - openCV中的总和

c++ - 如何使用另一个模板化类的实例来对类进行模板化?

c++ - 将大型数组从 MainWindow 传递到 QDialogs

c++ - 性能下降 - 可能是设计不当的方法