From 1852d0b9b8d38c1d7c08127363b325b1a969a19f Mon Sep 17 00:00:00 2001 From: catree Date: Tue, 30 Aug 2022 00:45:47 +0200 Subject: [PATCH] Add flags to set the camera intrinsic parameters as an initial guess (can allow converging to the correct camera intrinsic parameters). Add -imshow-scale flag to resize the image when displaying the results. Add -enable-k3 flag to enable or disable the estimation of the K3 distortion coefficient. --- samples/cpp/calibration.cpp | 62 +++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/samples/cpp/calibration.cpp b/samples/cpp/calibration.cpp index 4f2bb8b8b5..1e8e149940 100644 --- a/samples/cpp/calibration.cpp +++ b/samples/cpp/calibration.cpp @@ -38,9 +38,6 @@ const char * usage = "\n" "\n"; - - - const char* liveCaptureHelp = "When the live video from camera is used as input, the following hot-keys may be used:\n" " , 'q' - quit the program\n" @@ -59,19 +56,25 @@ static void help(char** argv) " # of board views actually available)\n" " [-d=] # a minimum delay in ms between subsequent attempts to capture a next view\n" " # (used only for video capturing)\n" - " [-s=] # square size in some user-defined units (1 by default)\n" + " [-s=] # square size in some user-defined units (1 by default)\n" " [-o=] # the output filename for intrinsic [and extrinsic] parameters\n" " [-op] # write detected feature points\n" " [-oe] # write extrinsic parameters\n" " [-oo] # write refined 3D object points\n" " [-zt] # assume zero tangential distortion\n" - " [-a=] # fix aspect ratio (fx/fy)\n" + " [-a=] # fix aspect ratio (fx/fy)\n" " [-p] # fix the principal point at the center\n" " [-v] # flip the captured images around the horizontal axis\n" " [-V] # use a video file, and not an image list, uses\n" " # [input_data] string for the video file name\n" " [-su] # show undistorted images after calibration\n" - " [-ws=] # Half of search window for cornerSubPix (11 by default)\n" + " [-ws=] # half of search window for cornerSubPix (11 by default)\n" + " [-fx=] # focal length in X-dir as an initial intrinsic guess (if this flag is used, fx, fy, cx, cy must be set)\n" + " [-fy=] # focal length in Y-dir as an initial intrinsic guess (if this flag is used, fx, fy, cx, cy must be set)\n" + " [-cx=] # camera center point in X-dir as an initial intrinsic guess (if this flag is used, fx, fy, cx, cy must be set)\n" + " [-cy=] # camera center point in Y-dir as an initial intrinsic guess (if this flag is used, fx, fy, cx, cy must be set)\n" + " [-imshow-scale # image resize scaling factor when displaying the results (must be >= 1)\n" + " [-enable-k3=<0/1> # to enable (1) or disable (0) K3 coefficient for the distortion model\n" " [-dt=] # actual distance between top-left and top-right corners of\n" " # the calibration grid. If this parameter is specified, a more\n" " # accurate calibration method will be used which may be better\n" @@ -151,7 +154,6 @@ static bool runCalibration( vector > imagePoints, vector& newObjPoints, double& totalAvgErr) { - cameraMatrix = Mat::eye(3, 3, CV_64F); if( flags & CALIB_FIX_ASPECT_RATIO ) cameraMatrix.at(0,0) = aspectRatio; @@ -170,7 +172,7 @@ static bool runCalibration( vector > imagePoints, iFixedPoint = boardSize.width - 1; rms = calibrateCameraRO(objectPoints, imagePoints, imageSize, iFixedPoint, cameraMatrix, distCoeffs, rvecs, tvecs, newObjPoints, - flags | CALIB_FIX_K3 | CALIB_USE_LU); + flags | CALIB_USE_LU); printf("RMS error reported by calibrateCamera: %g\n", rms); bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs); @@ -191,7 +193,6 @@ static bool runCalibration( vector > imagePoints, return ok; } - static void saveCameraParams( const string& filename, Size imageSize, Size boardSize, float squareSize, float aspectRatio, int flags, @@ -346,7 +347,6 @@ static bool runAndSave(const string& outputFilename, return ok; } - int main( int argc, char** argv ) { Size boardSize, imageSize; @@ -375,6 +375,8 @@ int main( int argc, char** argv ) "{help ||}{w||}{h||}{pt|chessboard|}{n|10|}{d|1000|}{s|1|}{o|out_camera_data.yml|}" "{op||}{oe||}{zt||}{a||}{p||}{v||}{V||}{su||}" "{oo||}{ws|11|}{dt||}" + "{fx||}{fy||}{cx||}{cy||}" + "{imshow-scale|1|}{enable-k3|0|}" "{@input_data|0|}"); if (parser.has("help")) { @@ -419,6 +421,23 @@ int main( int argc, char** argv ) else inputFilename = parser.get("@input_data"); int winSize = parser.get("ws"); + cameraMatrix = Mat::eye(3, 3, CV_64F); + if (parser.has("fx") && parser.has("fy") && parser.has("cx") && parser.has("cy")) + { + cameraMatrix.at(0,0) = parser.get("fx"); + cameraMatrix.at(0,2) = parser.get("cx"); + cameraMatrix.at(1,1) = parser.get("fy"); + cameraMatrix.at(1,2) = parser.get("cy"); + flags |= CALIB_USE_INTRINSIC_GUESS; + std::cout << "Use the following camera matrix as an initial guess:\n" << cameraMatrix << std::endl; + } + int viewScaleFactor = parser.get("imshow-scale"); + bool useK3 = parser.get("enable-k3"); + std::cout << "Use K3 distortion coefficient? " << useK3 << std::endl; + if (!useK3) + { + flags |= CALIB_FIX_K3; + } float grid_width = squareSize * (boardSize.width - 1); bool release_object = false; if (parser.has("dt")) { @@ -554,8 +573,17 @@ int main( int argc, char** argv ) Mat temp = view.clone(); undistort(temp, view, cameraMatrix, distCoeffs); } + if (viewScaleFactor > 1) + { + Mat viewScale; + resize(view, viewScale, Size(), 1.0/viewScaleFactor, 1.0/viewScaleFactor, INTER_AREA); + imshow("Image View", viewScale); + } + else + { + imshow("Image View", view); + } - imshow("Image View", view); char key = (char)waitKey(capture.isOpened() ? 50 : 500); if( key == 27 ) @@ -596,9 +624,17 @@ int main( int argc, char** argv ) view = imread(imageList[i], 1); if(view.empty()) continue; - //undistort( view, rview, cameraMatrix, distCoeffs, cameraMatrix ); remap(view, rview, map1, map2, INTER_LINEAR); - imshow("Image View", rview); + if (viewScaleFactor > 1) + { + Mat rviewScale; + resize(rview, rviewScale, Size(), 1.0/viewScaleFactor, 1.0/viewScaleFactor, INTER_AREA); + imshow("Image View", rviewScale); + } + else + { + imshow("Image View", rview); + } char c = (char)waitKey(); if( c == 27 || c == 'q' || c == 'Q' ) break;