Fisheye undistortion

afaure
Mon, 09 Sep 2024 04:37:32 +0000
C++

Hi,

I managed to get the undistortion of my camera working in Python. I am now trying to convert it into C++ using the same logic and same parameters, but fail to do so.

The k_mat and d_mat are the same in Python and C++, I can see that the resulting camera matrix is also the same. But then map_x and map_y end up as being different and my resulting image is not looking right at all.

For reference, here is my prototype C++ code:

float kmat_values[3][3] = {{1.51865043e+03, 0.00000000e+00, 9.97723183e+02},
                            {0.00000000e+00, 1.52017147e+03, 5.41360776e+02},
                            {0.00000000e+00, 0.00000000e+00, 1.00000000e+00}};

float dmat_values[4][1] = {{-0.19509397},
                            { 0.07629731},
                            {-0.18873016},
                            { 0.17534094}};

float balance = 0.0;
Size video_size(1920, 1080);

Mat kmat(3, 3, CV_32FC1, kmat_values);
Mat dmat(4, 1, CV_32FC1, dmat_values);

Mat mapx, mapy, camera;

void init() {
    Mat eye = Mat::eye(3, 3, CV_32FC1);

    fisheye::estimateNewCameraMatrixForUndistortRectify(
        kmat,
        dmat,
        video_size,
        eye,
        camera,
        balance
    );

    initUndistortRectifyMap(
        kmat,
        dmat,
        eye,
        camera,
        video_size,
        CV_32FC1,
        mapx,
        mapy
    );
}

void undistort_image(Mat &frame, Mat &undistorted) {
    remap(frame, undistorted, mapx, mapy, INTER_LINEAR, BORDER_CONSTANT);
}

I have left the main along with the reading and writing of the images out but can include them if required.

You will find the result image below (as a new user I can only include one media, I can try to include the raw image in a reply post if required):

Any help would be greatly appreciated. I have tried playing around with different types (e.g. CV_16SC1 etc) but don’t see any difference.

Thanks,
Antoine

4 posts - 2 participants