Bad behavior using cv::undistort with newCameraMatrix

asked 2015-07-08 13:30:17 -0500

Javelin gravatar image

updated 2015-07-08 13:31:34 -0500

I calibrated a wide angle camera (~120° horizontal, ~110° vertical) using the Pinhole camera model. The result is quite good but large areas of the image are discarded.

image description

So I'm trying to use the getOptimalNewCameraMatrix function (using alpha = 1) to calculate the newCameraMatrix and then pass it to cv::undistord, but the resulting frame is really distorted.

image description.

The code I use is:

    // Matrices calculation
    data.rms = calibrateCamera(objectPoints, imagePoints, data.size, data.cameraMatrix,
        data.distCoeffs, data.rvecs, data.tvecs, settings.calibFlags);

    if(!settings.cropCorrected) {
        Rect roi;
        data.newCameraMatrix = getOptimalNewCameraMatrix(data.cameraMatrix, data.distCoeffs, 
        data.size, 1, data.size, &roi, true);
    }

    // Undistort
    cv::undistort(in, corrected, cameraMatrix, distCoeffs, newCameraMatrix);

This is simple but there is something not working. I have already tried to use roi to crop the inner area, in the hope that it would have been anyway bigger than without the additional matrix, but in fact it's not.

How can i resolve the situation?

edit retag flag offensive close merge delete