如何使用opencv获取所有未失真的图像

时间:2013-09-11 13:32:53

标签: opencv distortion

我正在注意cv::undistort,但它会裁剪图像。我希望得到所有未失真的图像,这样原来的尺寸就像原来的那样大了:

enter image description here

我想我需要使用cv::getOptimalNewCameraMatrix,但我的试验没有运气......有些帮助吗?

3 个答案:

答案 0 :(得分:4)

仅供记录: 您应该使用cv::getOptimalNewCameraMatrix并将alpha参数设置为1. Alpha 0仅显示图像上的有效点,alpha 1显示所有原始点以及黑色区域。

答案 1 :(得分:3)

这段代码可以解决问题:

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.htmlhttp://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html

答案 2 :(得分:0)

最好的是子类OpenCV的类并重载方法undistort(),以便访问你需要的所有图像。