Wrong Intrinsic and Distrotion coefficients

asked 2019-09-10 07:16:33 -0600

Hi, I have calibrated the camera of my LG phone using 30 checkerboard images. I have checked the found corners on the calibration images one by one, they are ok, on the right place between the squares. But when I use the camera intrinsic matrix, and the distortion coefficient matrix( which I obtained by the calibrateCamera() function) to undistort an image I get strange results.


Original image:
image description


"undistorted" image: image description

the calibration code snippet :

void calibrateCamera(vector<string> imagefilePaths){
  int numBoards = 30;
  int numCornersHor = 9;
  int numCornersVer = 7;

  vector<vector<Point3f>> object_points;
  vector<vector<Point2f>> image_points;

  vector<Point2f> corners;
  vector<Point3f> obj;
  int numSquares = numCornersHor * numCornersVer;
  Size board_sz = Size(numCornersHor, numCornersVer);
  float scale =0.04;

  for(int j=0;j<numSquares;j++){
        obj.push_back(Point3f( (j/numCornersHor)*scale , (j%numCornersHor)*scale, 0.0f));
  }

  Mat image;

  for(int i = 0; i < imagefilePaths.size(); i++ ){
    image = imread( imagefilePaths[i], IMREAD_COLOR);
    Mat gray_image;
    cvtColor(image, gray_image, CV_BGR2GRAY);

    bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

    if(found){
      cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
      drawChessboardCorners(gray_image, board_sz, corners, found);
    }

    imwrite( ("/home/gabor/cpp/ocv_test/chess_" + to_string(i) + ".jpg") , gray_image );
    //imshow("win2", gray_image);
    //int key = waitKey(1000);

    if(found!=0){
      image_points.push_back(corners);
      object_points.push_back(obj);
    }

  }

  Mat intrinsic = Mat(3, 3, CV_32FC1);
  Mat distCoeffs;
  vector<Mat> rvecs;
  vector<Mat> tvecs;

  calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);

  /*FileStorage fs("/home/gabor/cpp/ocv_test/calibration/LGK5_.yml", FileStorage::WRITE);
  fs << "intrinsics" << intrinsic;
  fs << "distortion_coefficients" << distCoeffs;
  fs << "translation vectors" << tvecs;
  fs.release();
*/

}

Is it something with the corner finding parameters? Thanks.

edit retag flag offensive close merge delete

Comments

  1. What looks wrong to you about the image? If it is the curved black boundary, that is normal because when you undistort an image, the original image edge becomes curved and in order to display the full image, there are areas (outside of the original image) where you don't have data to undistort (hence the black pixels). Try undistorting an image of the checkerboard or other image with long straight lines (do the lines look straight in the undistorted image...?)

  2. For each detected corner, draw a circle onto the image and save the image and see if you are finding the corners accurately. Also, what valued did you get for reprojection error from calibration?

  3. Also note that the camera on your phone is presumably autofocus, so the intrinsics will change with focus.

swebb_denver gravatar imageswebb_denver ( 2019-09-11 14:51:18 -0600 )edit

@swebb_denver 1. The right-hand side of the undistorted image is not correct. I know what you mean about the black areas, it is not just that. I have undistorted an other few images using these parameters and they are the same, the right side is not ok. 2. I have. the detected corners are all right on every single image. The rms value returned, is: 0.367076 3. I guess that the problem is the camera autofocus, as you mentioned. Plus I just realized now, that the phone's camera saves the images in a seemingly random fashion, i.e. independently from the phone's real pose in the space. one image is saved as 640480 the other is 480640 even if I hold the phone in the landscape position taking both pictures. strange.. Now I dig a bit deeper into it. And thanks for the comments

geregabor gravatar imagegeregabor ( 2019-09-12 02:44:49 -0600 )edit