# Cv::fisheye::undistortPoints() returns -nan

**URL:** <https://forum.opencv.org/t/cv-undistortpoints-returns-nan/16935>\
**Category:** C++\
**Created:** [March 22, 2024, 1:57pm UTC](https://forum.opencv.org/t/cv-undistortpoints-returns-nan/16935 "2024-03-22T13:57:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Jacobsson\_Falc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/lucas_jacobsson_falc/32/9032_2.png) [@Lucas\_Jacobsson\_Falc](https://forum.opencv.org/u/Lucas_Jacobsson_Falc)\
**Post date:** [March 22, 2024, 1:57pm UTC](https://forum.opencv.org/t/cv-undistortpoints-returns-nan/16935/1 "2024-03-22T13:57:30Z")

</div>

I’m trying to undistort points using the fish-eye camera model. I got it working perfectly fine in python3 using

```auto
distorted = np.array([[list(target[0].pt)]], dtype=np.float32)
    new_xy = cv2.fisheye.undistortPoints(
        distorted,
        cam_cal.camera_matrix,
        cam_cal.distortion_coefficients,
        P=cam_cal.new_camera_matrix,
    )

```

Where “target” is a sequence of keypoints. Now I’m trying to do the equivalent in C++

```auto
cv::Point2f points_in = {800, 700};
cv::Point2f point_out;
std::vector<cv::Point2f> vec_in = {};
vec_in.push_back(points_in);
std::cout << vec_in << std::endl;
std::vector<cv::Point2f> vec_out = {};
cv::fisheye::undistortPoints(
    vec_in,
    vec_out,
    m_camera_matrix,
    m_dist_coeffs,
    cv::Mat::eye(3, 3, CV_64FC1),
    m_new_camera_matrix
);
std::cout << vec_out << std::endl;
point_out = vec_out[0];
vec_out.pop_back();

```

But the result is `[-nan, -nan]`. I also tried using `cv::Mat` instead of `std::vector<cv::Point2f>`but get the same result.

However, the following works and I get the expected results, so I really have no clue what I’m doing wrong.

```auto
m_new_camera_matrix = cv::Mat(3, 3, CV_64F);
    cv::fisheye::estimateNewCameraMatrixForUndistortRectify(
        m_camera_matrix,
        m_dist_coeffs,
        cv::Size(image_width, image_height),
        cv::Mat::eye(3, 3, CV_64F),
        m_new_camera_matrix
    );

    cv::fisheye::initUndistortRectifyMap(
        m_camera_matrix,
        m_dist_coeffs,
        cv::Mat::eye(3, 3, CV_64F),
        m_new_camera_matrix,
        cv::Size(image_width, image_height),
        CV_32FC1,
        m_map_1,
        m_map_2
    );
cv::remap(
        frame,
        output,
        m_map_1,
        m_map_2,
        cv::InterpolationFlags::INTER_LINEAR,
        cv::BorderTypes::BORDER_CONSTANT
    );

```

where

```auto
m_camera_matrix = 
[695.84766, 0, 823.49;
 0, 697.85657, 704.2644;
 0, 0, 1]
m_distortion_coefficients = 
[-0.00358979;
 0.01170594;
 -0.008970209999999999;
 0.00252217],
image_width = 1600
image_height = 1300

```

---

<div class="post-metadata">

**Author:** ![Lucas\_Jacobsson\_Falc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/lucas_jacobsson_falc/32/9032_2.png) [@Lucas\_Jacobsson\_Falc](https://forum.opencv.org/u/Lucas_Jacobsson_Falc)\
**Post date:** [March 25, 2024, 11:49am UTC](https://forum.opencv.org/t/cv-undistortpoints-returns-nan/16935/2 "2024-03-25T11:49:49Z")

</div>

I found the problem. I was initializing the camera matrix and distortion coefficients in a function using arrays, unaware that the initializer for `cv::Mat` refers to the array pointer, rather than copying the data itself. So what happened is that these were pointing to the stack, which is then freed when exiting the scope, and the data would be corrupted causing undefined behavior. Initializing the matrices like

```auto
m_camera_matrix = (cv::Mat_<double>(3,3) << 
        f_x, 0.0, c_x,
        0.0, f_y, c_y,
        0.0, 0.0, 1.0);

```

solved the problem
