【发布时间】:2013-09-15 13:34:31
【问题描述】:
我正在取消cv::undistort,但它会裁剪图像。我想要所有未失真的图像,以便未失真的尺寸大于原始尺寸,如下所示:
我想我需要使用cv::getOptimalNewCameraMatrix,但我的试验没有运气......有什么帮助吗?
【问题讨论】:
标签: opencv distortion
我正在取消cv::undistort,但它会裁剪图像。我想要所有未失真的图像,以便未失真的尺寸大于原始尺寸,如下所示:
我想我需要使用cv::getOptimalNewCameraMatrix,但我的试验没有运气......有什么帮助吗?
【问题讨论】:
标签: opencv distortion
最好是继承 OpenCV 的类并重载方法 undistort(),以便访问您需要的所有图像。
【讨论】:
仅作记录: 您应该使用cv::getOptimalNewCameraMatrix 并将alpha 参数设置为1。alpha 0 仅显示图像上的有效点,alpha 1 显示所有原始点以及黑色区域。 cv::getOptimalNewCameraMatrix aslo 还为您提供了一个 ROI 来裁剪来自 cv::undistort 的结果。
【讨论】:
这段代码可以解决问题:
void loadUndistortedImage(std::string fileName, Mat & outputImage,
Mat & cameraMatrix, Mat & distCoeffs) {
Mat image = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
// setup enlargement and offset for new image
double y_shift = 60;
double x_shift = 70;
Size imageSize = image.size();
imageSize.height += 2*y_shift;
imageSize.width += 2*x_shift;
// create a new camera matrix with the principal point
// offest according to the offset above
Mat newCameraMatrix = cameraMatrix.clone();
newCameraMatrix.at<double>(0, 2) += x_shift; //adjust c_x by x_shift
newCameraMatrix.at<double>(1, 2) += y_shift; //adjust c_y by y_shift
// create undistortion maps
Mat map1;
Mat map2;
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
newCameraMatrix, imageSize, CV_16SC2, map1, map2);
//remap
remap(image, outputImage, map1, map2, INTER_LINEAR);
}
见http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html 和http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html
【讨论】: