# Fisheye undistortion

**URL:** <https://forum.opencv.org/t/fisheye-undistortion/18696>\
**Category:** C++\
**Tags:** calib3d, fisheye\
**Created:** [September 9, 2024, 4:37am UTC](https://forum.opencv.org/t/fisheye-undistortion/18696 "2024-09-09T04:37:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![afaure](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@afaure](https://forum.opencv.org/u/afaure)\
**Post date:** [September 9, 2024, 4:37am UTC](https://forum.opencv.org/t/fisheye-undistortion/18696/1 "2024-09-09T04:37:32Z")

</div>

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:

```auto
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):

 ![undistorted](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1c57857b5246aa039f9a1d3eb59b61014eabb846.jpeg)

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 9, 2024, 7:08am UTC](https://forum.opencv.org/t/fisheye-undistortion/18696/2 "2024-09-09T07:08:57Z")

</div>

please provide the Python code as well, for comparison.

I don’t see the issue yet. current bet: mismatch between arguments and parameters.

---

<div class="post-metadata">

**Author:** ![afaure](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@afaure](https://forum.opencv.org/u/afaure)\
**Post date:** [September 9, 2024, 9:06am UTC](https://forum.opencv.org/t/fisheye-undistortion/18696/3 "2024-09-09T09:06:33Z")

</div>

Hi @crackwitz ,

Thanks for replying.

The python code is as follows:

```auto
import cv2
import numpy as np

DIM = (1920,1080)

k_mat = np.array(
    [[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]]
)

d_mat = np.array(
    [[-0.19509397],
    [0.07629731],
    [-0.18873016],
    [0.17534094]]
)

balance = 0.0

camera_mat = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(
    k_mat, d_mat, DIM, np.eye(3), balance=balance
)

mapx, mapy = cv2.fisheye.initUndistortRectifyMap(
    k_mat, d_mat, np.eye(3), camera_mat, DIM, cv2.CV_32FC1
)

def undistort(img):
    dst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
    return dst

cap = cv2.VideoCapture("input.jpeg")
ret, img = cap.read()

undistorted = undistort(img)
cv2.imwrite("python_undistorted.jpeg", undistorted)

```

And the image is undistorted properly:

 ![python_undistorted](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/3aad1e02c0bd4040481ee4e88c30516f60085cda.jpeg)

---

<div class="post-metadata">

**Author:** ![afaure](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@afaure](https://forum.opencv.org/u/afaure)\
**Post date:** [September 9, 2024, 9:34am UTC](https://forum.opencv.org/t/fisheye-undistortion/18696/4 "2024-09-09T09:34:49Z")

</div>

I managed to find out what was wrong: I had a namespace issue. I was using the `initUndistortRectifyMap` function from a different scope, changing that line to `fisheye::initUndistortRectifyMap` fixed the problem.  
Thanks anyways!  
Cheers
