使用 initUndistortRectifyMap 来取消扭曲的图像点
using initUndistortRectifyMap to undistort image points
我有一个应用程序,我必须不断将像素位置不扭曲到它们的"正确"位置。
我首先在 Python 中尝试了它,以下代码产生了良好的结果:
cam_m=np.array([[2590.2742, 0, 997.78192],[0, 2582.9724, 509.76907],[0, 0, 1]])
dist_c=np.array([0.088253155, 0.96952456, 0.0033740622, -0.00064934365, -6.0030732])
map1,map2=cv2.initUndistortRectifyMap(cam_m, dist_c, None, None, (1920,1024), cv2.CV_32FC1)
当我试图将其转换为C++时,它只是返回废话。我打电话给:
cv::initUndistortRectifyMap(this->m_cameraMatrix,this->m_distortionCoeffs,
cv::Mat::eye(3, 3, CV_32FC1),cv::Mat::eye(3, 3, CV_32FC1),
cv::Size(dimX,dimY),CV_32FC1,this->m_undistortMapX,this->m_undistortMapY);
但是在映射中,它返回的值很大,当我输出m_undistortMapX它包含大约 -2.0e+21 的值。 this->m_undistortMapX
和 this->m_undistortMapY
在类中声明,之前未初始化。其他参数看起来也正确:
std::cout << this->m_cameraMatrix << std::endl;
std::cout << this->m_distortionCoeffs << std::endl;
std::cout << dimX <<" / "<<dimY<<std::endl;
输出
[2590.2742, 0, 997.78192;
0, 2582.9724, 509.76907;
0, 0, 1]
[0.088253155, 0.96952456, 0.0033740622, -0.00064934365, -6.0030732]
1920 / 1024
所以就像在Python中一样,我想。任何想法还会出现什么问题?!
试试这个;首先得到最优的新相机矩阵newcam1,然后找到x和y方向map1x和map1y的映射矩阵。最后通过使用不失真功能,您可以获得image_undistorted
Mat newcam1,map1x,map1y;
newcam1 = getOptimalNewCameraMatrix(CM1, Dist1, Size(w, h), 0);
initUndistortRectifyMap(CM1, Dist1, R1, newcam1, Size(w, h), CV_32FC1, map1x, map1y);
undistort(img, image_undistorted, CM1, Dist1, newcam1);
我遇到了类似的问题,偶然发现了你的问题,所以也许这可以帮助某人。另一个答案提供了一个可行的解决方案,但没有解释出了什么问题。
在 CPP 版本中,您为 P 矩阵提供了 None/Identity 矩阵,由于某种原因,OpenCV 允许这样做,尽管它会生成垃圾(生成的映射将使用 1 f.e. 的焦距(。我会尝试看看我是否可以在 OpenCV 中解决这个问题。
我无法评论为什么 python 版本对您有用,特别是由于自 2017 年以来 OpenCV python 绑定中发生的变化。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 将图像添加到资源文件夹UWP C++
- 彩色图像的卤化物处理平均值
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 重新定位图像时如何前进到下一个内存块
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 为什么 BMP 图像上的 imwrite 会卡住/不返回?
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何使用CImg打开图像?
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 使用 CUDA 和纹理进行图像减法