how to get all undistorted image with opencv

白昼怎懂夜的黑 提交于 2019-12-06 11:09:23

Just for the record: You should use cv::getOptimalNewCameraMatrix and set the aplha parameter to 1. Aplha 0 only shows valid points on the image, alpha 1 shows all the original points as well as black regions.

This code would do the trick:

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

See http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html and http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html

the best would be subclass OpenCV's class and overload the method undistort(), in order to access all the images you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!