Ask Your Question
0

fisheye::undistortImage() doesn't work. What wrong with my code.

asked 2015-06-22 21:48:08 -0600

takmin gravatar image

First, I computed camera parameters with fisheye::calibration() and saved it as "fisheye0.txt".

%YAML:1.0
IntParam:
   camera_matrix: !!opencv-matrix
      rows: 3
      cols: 3
      dt: d
      data: [ 4.5573419244698465e+002, 0., 7.9970141134416019e+002, 0.,
            4.5329184346860586e+002, 5.9806119342862326e+002, 0., 0., 1. ]
  distortion: !!opencv-matrix
      rows: 4
      cols: 1
      dt: d
      data: [ -6.5992454656838007e-004, -9.8884504633100923e-001,
            4.0189095183182335e+000, -2.4857042509933436e+000 ]

Then, I try to undistort an image with following code:

//// Read Camera Parameters //////
cv::Mat camera_matrix, distortion;
cv::FileStorage fs("fisheye0.txt", cv::FileStorage::READ);
cv::FileNode fn = fs["IntParam"];
fn["camera_matrix"] >> camera_matrix;
fn["distortion"] >> distortion;

// load image
cv::Mat distort_img = cv::imread("distort.jpg");

// undistortion
cv::Mat undistort_img;
cv::fisheye::undistortImage(distort_img, undistort_img, camera_matrix, distortion, camera_matrix);

// save image
cv::imwrite("undistort.jpg", undistort_img);

The result was very strange.

This is input image "distort.jpg" distort.jpg

And this is the output image "undistort.jpg" undistort.jpg

A distort image couldn't be undistorted. What's wrong with my code?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-08-17 05:50:08 -0600

takmin gravatar image

I solved this problem.

That was because fisheye camera parameter was wrong.

I computed fisheye camera parameters by the following code:

rms_err = fisheye::calibrate(object_points, image_points, img_size, camera_matrix, distortion,
            cv::noArray(), cv::noArray(),  fisheye::CALIB_CHECK_COND | fisheye::CALIB_FIX_SKEW);

I solved the problem by changing the code:

rms_err = fisheye::calibrate(object_points, image_points, img_size, camera_matrix, distortion,
cv::noArray(), cv::noArray(), fisheye::CALIB_RECOMPUTE_EXTRINSIC | fisheye::CALIB_CHECK_COND | fisheye::CALIB_FIX_SKEW);
edit flag offensive delete link more

Comments

Hi, may I see your whole code, I am curious about how to do the calibration part ? Thank you!

JH gravatar imageJH ( 2016-06-07 11:21:43 -0600 )edit

I would like to see it as well if it's possible. I'm running the same code but I don't get the correct undistorted image! the camera is > 170 degree wide

mohammad gravatar imagemohammad ( 2016-06-08 14:11:43 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-06-22 21:48:08 -0600

Seen: 9,153 times

Last updated: Aug 17 '15