gpt4 book ai didi

c++ - 使用cv::fisheye::undistortImage使鱼眼镜头图像失真

转载 作者:行者123 更新时间:2023-12-03 07:21:44 24 4
gpt4 key购买 nike

我正在尝试使用OpenCV扭曲鱼眼图像。我从相机的内部存储器中获得了相机矩阵和失真系数。我假设它们是准确的。如您在下面的代码中看到的,我正在使用cv::fisheye::undistortImage。我发现这个GitHub Issue post声称它应该可以工作,但是,未失真的图像框架看起来并不正确!因此,很明显,某些操作无效。
这是代码:

#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/core/mat.hpp>

int main()
{
cv::Mat cameraMatrix = cv::Mat(3,3, CV_64F, double(0));
cv::Mat distortionCoeffs = cv::Mat(1,4, CV_64F, double(0));

cameraMatrix.at<double>(0, 0) = 286.7037963867188;
cameraMatrix.at<double>(0, 1) = 0;
cameraMatrix.at<double>(0, 2) = 413.3463134765625;
cameraMatrix.at<double>(1, 0) = 0;
cameraMatrix.at<double>(1, 1) = 286.7817993164062;
cameraMatrix.at<double>(1, 2) = 397.1785888671875;
cameraMatrix.at<double>(2, 0) = 0;
cameraMatrix.at<double>(2, 1) = 0;
cameraMatrix.at<double>(2, 2) = 1;

distortionCoeffs.at<double>(0,0) = -0.01078350003808737;
distortionCoeffs.at<double>(0,1) = 0.04842806980013847;
distortionCoeffs.at<double>(0,2) = -0.04542399942874908;
distortionCoeffs.at<double>(0,3) = 0.008737384341657162;

cv::Mat input_frame = cv::imread("fisheye_input.png");
cv::Mat output_frame;

cv::fisheye::undistortImage(input_frame,output_frame,cameraMatrix,distortionCoeffs, cv::noArray(), cv::Size(input_frame.cols,input_frame.rows));

cv::imshow("Input Image", input_frame);
cv::imshow("Output Image", output_frame);
cv::waitKey(-1);
return 0;
}
代码输出:
enter image description here
如果您想自己尝试,这里是原始的鱼眼镜头图像:
enter image description here

最佳答案

我无法真正找到代码中的真正问题。当我阅读documentation时,我看到了:

The function is simply a combination offisheye::initUndistortRectifyMap (with unity R ) and remap (withbilinear interpolation).


当我深入研究使用这两个功能组合( initUndistortRectifyMapremap)而不是使用 undistortImage时,我发现了@Micka回答的 this post。我尝试了一下,并且工作正常,这是代码和结果:
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/core/mat.hpp>

#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat cameraMatrix = cv::Mat(3,3, cv::DataType<double>::type);
cv::Mat distortionCoeffs = cv::Mat(4,1, cv::DataType<double>::type);

cameraMatrix.at<double>(0, 0) = 286.7037963867188;
cameraMatrix.at<double>(0, 1) = 0;
cameraMatrix.at<double>(0, 2) = 413.3463134765625;
cameraMatrix.at<double>(1, 0) = 0;
cameraMatrix.at<double>(1, 1) = 286.7817993164062;
cameraMatrix.at<double>(1, 2) = 397.1785888671875;
cameraMatrix.at<double>(2, 0) = 0;
cameraMatrix.at<double>(2, 1) = 0;
cameraMatrix.at<double>(2, 2) = 1;

distortionCoeffs.at<double>(0,0) = -0.01078350003808737;
distortionCoeffs.at<double>(1,0) = 0.04842806980013847;
distortionCoeffs.at<double>(2,0) = -0.04542399942874908;
distortionCoeffs.at<double>(3,0) = 0.008737384341657162;

cv::Mat E = cv::Mat::eye(3, 3, cv::DataType<double>::type);

cv::Mat input_frame = cv::imread("fishEye.png");

cv::Size size = { input_frame.cols, input_frame.rows };

cv::Mat map1;
cv::Mat map2;
//cv::fisheye::undistortImage(input_frame,output_frame,cameraMatrix,distortionCoeffs, E, cv::Size(input_frame.cols,input_frame.rows));

cv::fisheye::initUndistortRectifyMap(cameraMatrix, distortionCoeffs, E, cameraMatrix, size, CV_16SC2, map1, map2);

cv::Mat undistort;

cv::remap(input_frame, undistort, map1, map2, cv::INTER_LINEAR,
CV_HAL_BORDER_CONSTANT);

cv::imshow("Input Image", input_frame);
cv::imshow("Output Image", undistort );
cv::waitKey(-1);
return 0;
}
输出:
enter image description here

关于c++ - 使用cv::fisheye::undistortImage使鱼眼镜头图像失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64903037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com