![]() |
OpenCV 4.12.0-dev
Open Source Computer Vision
|
Prev Tutorial: Create calibration pattern
Next Tutorial: Camera calibration With OpenCV
Original author | Victor Eruhimov |
Compatibility | OpenCV >= 4.0 |
The goal of this tutorial is to learn how to calibrate a camera given a set of chessboard images.
Test data: use images in your data/chess folder.
Now, let us write code that detects a chessboard in an image and finds its distance from the camera. You can apply this method to any object with known 3D geometry; which you detect in an image.
Test data: use chess_test*.jpg images from your data folder.
Mat img = imread(argv[1], IMREAD_GRAYSCALE);
bool found = findChessboardCorners( img, boardSize, ptvec, CALIB_CB_ADAPTIVE_THRESH );
FileStorage fs( filename, FileStorage::READ ); Mat intrinsics, distortion; fs["camera_matrix"] >> intrinsics; fs["distortion_coefficients"] >> distortion;
vector<Point3f> boardPoints; // fill the array ... solvePnP(Mat(boardPoints), Mat(foundBoardCorners), cameraMatrix, distCoeffs, rvec, tvec, false);
Question: how would you calculate distance from the camera origin to any one of the corners? Answer: After obtaining the camera pose using solvePnP, the rotation (rvec) and translation (tvec) vectors define the transformation between the world (chessboard) coordinates and the camera coordinate system. To calculate the distance from the camera’s origin to any chessboard corner, first transform the 3D point from the chessboard coordinate system to the camera coordinate system (if not already done) and then compute its Euclidean distance using the L2 norm, for example:
// assuming 'point' is the 3D position of a chessboard corner in the camera coordinate system double distance = norm(point);
This is equivalent to applying the L2 norm on the 3D point’s coordinates (x, y, z).