How to get cropped image after fisheye calibration in OpenCV

568 Views Asked by At

I am trying to calibrated fisheye camera in OpenCV 3.2. This is a code:

 Mat newCamMat;
            cameraMatrix= getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 0, imageSize, 0); //Does not change anything

            fisheye::estimateNewCameraMatrixForUndistortRectify(cameraMatrix, distCoeffs, imageSize,
                                                                Matx33d::eye(), newCamMat, 1);
            fisheye::initUndistortRectifyMap(cameraMatrix, distCoeffs, Matx33d::eye(), newCamMat, imageSize,
                                             CV_16SC2, map1, map2);

I used these flags in calibration:

 fisheye::CALIB_FIX_SKEW | fisheye::CALIB_RECOMPUTE_EXTRINSIC | fisheye::CALIB_CHECK_COND

I want to get image cropped without the black areas around the usable area. For non-fisheye calibration I did it by setting alpha. For fisheye I dont know how to change it.Any suggestions?

Desired result (inside the red rectangle)

Calibration result

1

There are 1 best solutions below

3
On

You can use a masked setTo to set the Alpha channel value to be transparent.

Mat img = imread("fisheye.png",-1), gray, mask;

cvtColor(img, gray, cv::COLOR_BGR2GRAY);

threshold(gray, mask, 0, 255, cv::THRESH_BINARY_INV);

cvtColor(img, img, cv::COLOR_BGR2BGRA);

img.setTo(Scalar(0,0,0,0),mask);