Elmi
Elmi

Reputation: 6193

Correct spatial camera orientation

I'm performing a camera calibration as described in OpenCV calibration sample (which works fine for lens correction). Additionally I now want to do a spatial correction. Means when the camera is not parallel to the checkerboard I want to get the rotation that is necessary to make checkerboard parallel to camera. That's what I'm doing:

// calculate intrinsic matrix and distortion coefficients for lens correction and get
// the rotation "rotation"
cvCalibrateCamera2(object_points,image_points,point_counts,cvGetSize(gray_image),
                   intrinsic_matrix, distortion_coeffs,
                   rotation,NULL,
                   0);
...
// convert the rotation to a 3x3 matrix
cv::Rodrigues(rotation,rotMtx,cv::noArray());
// generate maps for lens correction
cv::initUndistortRectifyMap(intrinsic,distortion,cv::Mat(),
                            cv::getOptimalNewCameraMatrix(intrinsic,distortion,imageSize,1,imageSize,0),
                                                         imageSize, CV_16SC2,*handle->mapx,*handle->mapy);
...
// perform lens correction
cv::remap(cv::Mat(sImage),dImage,*handle->mapx,*handle->mapy,cv::INTER_LINEAR);
...
// apply the rotation to make the mage parallel to the camera
cv::warpPerspective(dImage,dImage2,rotMtx,cv::Size(handle->width,handle->height),cv::INTER_LANCZOS4,cv::BORDER_TRANSPARENT,cv::Scalar());

The rotation 3x1 rotation values that are returned by cvCalibrateCamera2() are !=0, so there is something returned. But warpPerspective() does not rotate the image.

What is wrong here? How should I "parallelise" the image correctly?

Upvotes: 4

Views: 1197

Answers (1)

Pranjal
Pranjal

Reputation: 96

From my understanding of the subject, the rotation matrix you are using to 'parallelize' the image provides rotation between two image planes. What you are looking for is a transformation from image plane to camera plane. This calls for calculating a homography between planes. You'll need to figure out what matrix takes you from your image plane to your camera plane. Use this matrix to parallelize your image.

Also, calibrateCamera gives you the forward transformations, to go back to the original you need to invert your matrix.

Try reading this - Homography

Hint: Use focal length & principal point information to move between image & camera planes.

Upvotes: 2

Related Questions