Skip to content

calibrateCamera and initUndistortRectifyMap fail to get undistort Image #14596

Closed

Description

@HaoQChen

OpenC Version: 3.2
I'm trying to calibrate my camera whose focal length is 4mm using OpenCV, but I found that calibrateCamera some time will get a non-monotonic camera model and lead to the failure of undistortImage.

I use the camera model to project a 3D plane into (u,v),It look like this which is non-monotonic.
image

So when I use initUndistortRectifyMap and remap to undistort image, it look like this. Somebody say that if camera model is non-monotonic, initUndistortRectifyMap will lead to this situation.
image

And what is weirder, if I change the chessboard num use to calibration, it changes a lot. I succeed only once when I use 20 images.

I try to use CV_CALIB_FIX_K3, it works with larger re-projection error.

I try to use fisheye class, but larger re-projection error.

And I have another camera whose focal length is 1.8mm, I failed everytime even use fisheye, unless use CV_CALIB_FIX_K3.
image

As you can see, the undistort Image is still distort a lot

What's happening? What can i do to have a more accurate camera model?

My code as below:

bool Calibration(vector<string>images_path_buf, Size size_chessboards, Size2f size_squares, string _undistort_path)
{
	vector<Point2f> image_corners;
	vector<vector<Point2f>> images_corners_buf;
	vector<vector<Point3f>> points_objects_buf;

	vector<Mat> images_buf;
	Mat image_processing;
	Mat image_processing_gray;

	Size size_image;

	vector<int> index_successful;
	int images_num = images_path_buf.size();


//get images from the images' path
	for (int index_path = 0; index_path < images_path_buf.size(); index_path++){
		image_processing = imread(images_path_buf.at(index_path));
		images_buf.push_back(image_processing);
	}
	
//get image size
	size_image.width = images_buf.at(0).cols;
	size_image.height = images_buf.at(0).rows;

//get accurate coordinare of chessboards
	for (int index_image = 0; index_image < images_num; index_image++){
		image_processing = images_buf.at(index_image);

		if (false == findChessboardCorners(image_processing, size_chessboards, image_corners)){
			cout << "Can not find chessboard: " << images_path_buf.at(index_image) << endl;
		}else{//if find chessboard corners successfully
			cvtColor(image_processing, image_processing_gray, CV_RGB2GRAY);
			find4QuadCornerSubpix(image_processing_gray, image_corners, Size(5, 5));
			images_corners_buf.push_back(image_corners);
			index_successful.push_back(index_image);
		}
	}
	if (3 >= index_successful.size()){
		cout << "Fail:Can not get corners in enough images!" << endl;
		return false;
	}
	
	vector<int>::iterator i_i_s = index_successful.begin();

//get corners' coordinare in world frame
	for (int num = 0; num < index_successful.size(); num++){
		vector<Point3f> pointss_object;
		for (int i = 0; i < size_chessboards.height; i++){
			for (int j = 0; j < size_chessboards.width; j++){
				Point3f point_object;
				point_object.x = i * size_squares.height;
				point_object.y = j * size_squares.width;
				point_object.z = 0;	
				pointss_object.push_back(point_object);
			}
		}
		points_objects_buf.push_back(pointss_object);
	}

//calibration the camera with above data 
	Mat camera_matrix = Mat(3, 3, CV_64FC1, Scalar::all(0));
	Mat dist_coeffs = Mat(1, 8, CV_64FC1, Scalar::all(0));//distortion coefficients
	vector<cv::Vec3d> rotation_vecs;//rotation vectors
    vector<Mat> translation_mats;

	double reproject_error = calibrateCamera(points_objects_buf, images_corners_buf, size_image, 
		camera_matrix, dist_coeffs, rotation_vecs, translation_mats, 
        CV_CALIB_FIX_K3, 
		TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30000, DBL_EPSILON));//CV_CALIB_FIX_K3 CV_CALIB_ZERO_TANGENT_DIST
    cout << "Reproject error is: " << reproject_error << endl;
//show result of Undistort
	Mat map1, map2;
	initUndistortRectifyMap(camera_matrix, dist_coeffs, Mat(), getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, size_image, 1, size_image, 0), size_image, CV_16SC2, map1, map2);
	
    for (int i = 0; i < index_successful.size(); i++){
		i_i_s = index_successful.begin() + i;
		Mat image_cali;
		drawChessboardCorners(images_buf.at(*i_i_s), size_chessboards, images_corners_buf.at(i), true);
		remap(images_buf.at(*i_i_s), image_cali, map1, map2, INTER_LINEAR);
		string image_name = images_path_buf.at(*i_i_s);
        imwrite(_undistort_path + image_name.substr(image_name.find_last_of('/')), image_cali); 
		imshow("calibrate_camera", image_cali);
		waitKey(2000);
	}
//write data to xml file
	FileStorage fs_intrinsic_parameters("intrinsic_parameters.yml", FileStorage::WRITE);
	fs_intrinsic_parameters << "camera_matrix" << camera_matrix << "dist_coeffs" << dist_coeffs;
	fs_intrinsic_parameters.release();
	return true;
}

Activity

  1. HaoQChen commented on May 22, 2019

    @HaoQChen
    Author

    The 4mm camera:
    90°width
    image
    The 1.8mm camera:
    165°width
    image

  2. xoox commented on May 23, 2019

    @xoox
    Contributor

    Hi, find4QuadCornerSubpix() is not reliable with large distortion images. It fits situations where discrete squares are used and the images have little distortion.

    Please consider using cornerSubPix() or have a try with the newer findChessboardCornersSB().

  3. HaoQChen commented on May 23, 2019

    @HaoQChen
    Author

    Hi, find4QuadCornerSubpix() is not reliable with large distortion images. It fits situations where discrete squares are used and the images have little distortion.

    Please consider using cornerSubPix() or have a try with the newer findChessboardCornersSB().

    umm,In my case, find4QuadCornerSubpix is better then cornerSubPix(re-project error is lower). And I check every image, the chessboard can be found well.

  4. HaoQChen commented on May 23, 2019

    @HaoQChen
    Author

    Hi, find4QuadCornerSubpix() is not reliable with large distortion images. It fits situations where discrete squares are used and the images have little distortion.

    Please consider using cornerSubPix() or have a try with the newer findChessboardCornersSB().

    It's so sensitive about the image that it will failed even if I just change one image. Maybe the camera's model is unsuitable?

  5. xoox commented on May 23, 2019

    @xoox
    Contributor

    You may share an archive of your calibration images, so that other participants have chances to test with them hoping to find the key factors.

    Some hints on capturing calibration images are very helpful. It's strongly suggested to read them before going further. Scroll down to the "Calibration hints" section on this page: https://www.dlr.de/rm/en/desktopdefault.aspx/tabid-3925/6084_read-9196/

  6. HaoQChen commented on May 25, 2019

    @HaoQChen
    Author

    After setting the flag CV_CALIB_RATIONAL_MODEL, it work good now~~~~~I found an answer in stackoverflow said that flag CV_CALIB_RATIONAL_MODEL will tell OpenCV to use a model that supports wider angle lenses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category: calib3dquestion (invalid tracker)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions