Skip to content

Commit 6277302

Browse files
author
AleksandrPanov
committed
fix aruco_calibration.markdown
1 parent a5c12ab commit 6277302

File tree

3 files changed

+51
-128
lines changed

3 files changed

+51
-128
lines changed

modules/aruco/samples/calibrate_camera.cpp

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// This file is part of OpenCV project.
2-
// It is subject to the license terms in the LICENSE file found in the top-level directory
3-
// of this distribution and at http://opencv.org/license.html
4-
51
#include <ctime>
62
#include <iostream>
73
#include <vector>
@@ -31,7 +27,7 @@ const char* keys =
3127
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
3228
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
3329
"{cd | | Input file with custom dictionary }"
34-
"{@outfile |<none> | Output file with calibrated camera parameters }"
30+
"{@outfile |cam.yml| Output file with calibrated camera parameters }"
3531
"{v | | Input from video file, if ommited, input comes from camera }"
3632
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
3733
"{dp | | File of marker detector parameters }"
@@ -55,7 +51,7 @@ int main(int argc, char *argv[]) {
5551
int markersY = parser.get<int>("h");
5652
float markerLength = parser.get<float>("l");
5753
float markerSeparation = parser.get<float>("s");
58-
string outputFile = parser.get<String>(0);
54+
string outputFile = parser.get<string>(0);
5955

6056
int calibrationFlags = 0;
6157
float aspectRatio = 1;
@@ -99,7 +95,7 @@ int main(int argc, char *argv[]) {
9995
waitTime = 10;
10096
}
10197

102-
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(0);
98+
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
10399
if (parser.has("d")) {
104100
int dictionaryId = parser.get<int>("d");
105101
dictionary = aruco::getPredefinedDictionary(
@@ -119,16 +115,16 @@ int main(int argc, char *argv[]) {
119115
return 0;
120116
}
121117

122-
// Create board object
118+
//! [CalibrationWithArucoBoard1]
119+
// Create board object and ArucoDetector
123120
aruco::GridBoard gridboard(Size(markersX, markersY), markerLength, markerSeparation, dictionary);
121+
aruco::ArucoDetector detector(dictionary, detectorParams);
124122

125123
// Collected frames for calibration
126124
vector<vector<vector<Point2f>>> allMarkerCorners;
127125
vector<vector<int>> allMarkerIds;
128126
Size imageSize;
129127

130-
aruco::ArucoDetector detector(dictionary, detectorParams);
131-
132128
while(inputVideo.grab()) {
133129
Mat image, imageCopy;
134130
inputVideo.retrieve(image);
@@ -141,10 +137,9 @@ int main(int argc, char *argv[]) {
141137

142138
// Refind strategy to detect more markers
143139
if(refindStrategy) {
144-
detector.refineDetectedMarkers(
145-
image, gridboard, markerCorners, markerIds, rejectedMarkers
146-
);
140+
detector.refineDetectedMarkers(image, gridboard, markerCorners, markerIds, rejectedMarkers);
147141
}
142+
//! [CalibrationWithArucoBoard1]
148143

149144
// Draw results
150145
image.copyTo(imageCopy);
@@ -153,10 +148,8 @@ int main(int argc, char *argv[]) {
153148
aruco::drawDetectedMarkers(imageCopy, markerCorners, markerIds);
154149
}
155150

156-
putText(
157-
imageCopy, "Press 'c' to add current frame. 'ESC' to finish and calibrate",
158-
Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 2
159-
);
151+
putText(imageCopy, "Press 'c' to add current frame. 'ESC' to finish and calibrate",
152+
Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 2);
160153
imshow("out", imageCopy);
161154

162155
// Wait for key pressed
@@ -166,19 +159,22 @@ int main(int argc, char *argv[]) {
166159
break;
167160
}
168161

162+
//! [CalibrationWithArucoBoard2]
169163
if(key == 'c' && !markerIds.empty()) {
170164
cout << "Frame captured" << endl;
171165
allMarkerCorners.push_back(markerCorners);
172166
allMarkerIds.push_back(markerIds);
173167
imageSize = image.size();
174168
}
175169
}
170+
//! [CalibrationWithArucoBoard2]
176171

177172
if(allMarkerIds.empty()) {
178173
cerr << "Not enough captures for calibration" << endl;
179174
return 0;
180175
}
181176

177+
//! [CalibrationWithArucoBoard3]
182178
Mat cameraMatrix, distCoeffs;
183179

184180
if(calibrationFlags & CALIB_FIX_ASPECT_RATIO) {
@@ -195,10 +191,7 @@ int main(int argc, char *argv[]) {
195191
for(size_t frame = 0; frame < nFrames; frame++) {
196192
Mat currentImgPoints, currentObjPoints;
197193

198-
gridboard.matchImagePoints(
199-
allMarkerCorners[frame], allMarkerIds[frame],
200-
currentObjPoints, currentImgPoints
201-
);
194+
gridboard.matchImagePoints(allMarkerCorners[frame], allMarkerIds[frame], currentObjPoints, currentImgPoints);
202195

203196
if(currentImgPoints.total() > 0 && currentObjPoints.total() > 0) {
204197
processedImagePoints.push_back(currentImgPoints);
@@ -207,16 +200,11 @@ int main(int argc, char *argv[]) {
207200
}
208201

209202
// Calibrate camera
210-
double repError = calibrateCamera(
211-
processedObjectPoints, processedImagePoints, imageSize,
212-
cameraMatrix, distCoeffs, noArray(), noArray(), noArray(),
213-
noArray(), noArray(), calibrationFlags
214-
);
215-
216-
bool saveOk = saveCameraParams(
217-
outputFile, imageSize, aspectRatio, calibrationFlags, cameraMatrix,
218-
distCoeffs, repError
219-
);
203+
double repError = calibrateCamera(processedObjectPoints, processedImagePoints, imageSize, cameraMatrix, distCoeffs,
204+
noArray(), noArray(), noArray(), noArray(), noArray(), calibrationFlags);
205+
//! [CalibrationWithArucoBoard3]
206+
bool saveOk = saveCameraParams(outputFile, imageSize, aspectRatio, calibrationFlags,
207+
cameraMatrix, distCoeffs, repError);
220208

221209
if(!saveOk) {
222210
cerr << "Cannot save output file" << endl;
@@ -225,6 +213,5 @@ int main(int argc, char *argv[]) {
225213

226214
cout << "Rep Error: " << repError << endl;
227215
cout << "Calibration saved to " << outputFile << endl;
228-
229216
return 0;
230217
}

modules/aruco/samples/calibrate_camera_charuco.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// This file is part of OpenCV project.
2-
// It is subject to the license terms in the LICENSE file found in the top-level directory
3-
// of this distribution and at http://opencv.org/license.html
4-
51
#include <iostream>
62
#include <vector>
73
#include <opencv2/calib3d.hpp>
@@ -29,7 +25,7 @@ const char* keys =
2925
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
3026
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
3127
"{cd | | Input file with custom dictionary }"
32-
"{@outfile |<none> | Output file with calibrated camera parameters }"
28+
"{@outfile |cam.yml| Output file with calibrated camera parameters }"
3329
"{v | | Input from video file, if ommited, input comes from camera }"
3430
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
3531
"{dp | | File of marker detector parameters }"
@@ -118,19 +114,18 @@ int main(int argc, char *argv[]) {
118114
return 0;
119115
}
120116

121-
// Create charuco board object
122-
aruco::CharucoBoard board(Size(squaresX, squaresY), squareLength, markerLength, dictionary);
123117
aruco::CharucoParameters charucoParams;
124-
125118
if(refindStrategy) {
126119
charucoParams.tryRefineMarkers = true;
127120
}
128121

122+
//! [CalibrationWithCharucoBoard1]
123+
// Create charuco board object and CharucoDetector
124+
aruco::CharucoBoard board(Size(squaresX, squaresY), squareLength, markerLength, dictionary);
129125
aruco::CharucoDetector detector(board, charucoParams, detectorParams);
130126

131127
// Collect data from each frame
132-
vector<Mat> allCharucoCorners;
133-
vector<Mat> allCharucoIds;
128+
vector<Mat> allCharucoCorners, allCharucoIds;
134129

135130
vector<vector<Point2f>> allImagePoints;
136131
vector<vector<Point3f>> allObjectPoints;
@@ -143,14 +138,14 @@ int main(int argc, char *argv[]) {
143138
inputVideo.retrieve(image);
144139

145140
vector<int> markerIds;
146-
vector<vector<Point2f>> markerCorners, rejectedMarkers;
147-
Mat currentCharucoCorners;
148-
Mat currentCharucoIds;
141+
vector<vector<Point2f>> markerCorners;
142+
Mat currentCharucoCorners, currentCharucoIds;
149143
vector<Point3f> currentObjectPoints;
150144
vector<Point2f> currentImagePoints;
151145

152146
// Detect ChArUco board
153147
detector.detectBoard(image, currentCharucoCorners, currentCharucoIds);
148+
//! [CalibrationWithCharucoBoard1]
154149

155150
// Draw results
156151
image.copyTo(imageCopy);
@@ -174,6 +169,7 @@ int main(int argc, char *argv[]) {
174169
break;
175170
}
176171

172+
//! [CalibrationWithCharucoBoard2]
177173
if(key == 'c' && currentCharucoCorners.total() > 3) {
178174
// Match image points
179175
board.matchImagePoints(currentCharucoCorners, currentCharucoIds, currentObjectPoints, currentImagePoints);
@@ -194,12 +190,14 @@ int main(int argc, char *argv[]) {
194190
imageSize = image.size();
195191
}
196192
}
193+
//! [CalibrationWithCharucoBoard2]
197194

198195
if(allCharucoCorners.size() < 4) {
199196
cerr << "Not enough corners for calibration" << endl;
200197
return 0;
201198
}
202199

200+
//! [CalibrationWithCharucoBoard3]
203201
Mat cameraMatrix, distCoeffs;
204202

205203
if(calibrationFlags & CALIB_FIX_ASPECT_RATIO) {
@@ -208,16 +206,12 @@ int main(int argc, char *argv[]) {
208206
}
209207

210208
// Calibrate camera using ChArUco
211-
double repError = calibrateCamera(
212-
allObjectPoints, allImagePoints, imageSize,
213-
cameraMatrix, distCoeffs, noArray(), noArray(), noArray(),
214-
noArray(), noArray(), calibrationFlags
215-
);
209+
double repError = calibrateCamera(allObjectPoints, allImagePoints, imageSize, cameraMatrix, distCoeffs,
210+
noArray(), noArray(), noArray(), noArray(), noArray(), calibrationFlags);
211+
//! [CalibrationWithCharucoBoard3]
216212

217-
bool saveOk = saveCameraParams(
218-
outputFile, imageSize, aspectRatio, calibrationFlags,
219-
cameraMatrix, distCoeffs, repError
220-
);
213+
bool saveOk = saveCameraParams(outputFile, imageSize, aspectRatio, calibrationFlags,
214+
cameraMatrix, distCoeffs, repError);
221215

222216
if(!saveOk) {
223217
cerr << "Cannot save output file" << endl;
@@ -233,9 +227,7 @@ int main(int argc, char *argv[]) {
233227
Mat imageCopy = allImages[frame].clone();
234228

235229
if(allCharucoCorners[frame].total() > 0) {
236-
aruco::drawDetectedCornersCharuco(
237-
imageCopy, allCharucoCorners[frame], allCharucoIds[frame]
238-
);
230+
aruco::drawDetectedCornersCharuco(imageCopy, allCharucoCorners[frame], allCharucoIds[frame]);
239231
}
240232

241233
imshow("out", imageCopy);
@@ -245,6 +237,5 @@ int main(int argc, char *argv[]) {
245237
}
246238
}
247239
}
248-
249240
return 0;
250241
}

modules/aruco/tutorials/aruco_calibration/aruco_calibration.markdown

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ The ArUco module can also be used to calibrate a camera. Camera calibration cons
88
camera intrinsic parameters and distortion coefficients. This parameters remain fixed unless the camera
99
optic is modified, thus camera calibration only need to be done once.
1010

11-
Camera calibration is usually performed using the OpenCV ```calibrateCamera()``` function. This function
11+
Camera calibration is usually performed using the OpenCV `cv::calibrateCamera()` function. This function
1212
requires some correspondences between environment points and their projection in the camera image from
1313
different viewpoints. In general, these correspondences are obtained from the corners of chessboard
14-
patterns. See ```calibrateCamera()``` function documentation or the OpenCV calibration tutorial for
14+
patterns. See `cv::calibrateCamera()` function documentation or the OpenCV calibration tutorial for
1515
more detailed information.
1616

1717
Using the ArUco module, calibration can be performed based on ArUco markers corners or ChArUco corners.
@@ -33,46 +33,13 @@ visible in all the viewpoints.
3333

3434
![ChArUco calibration viewpoints](images/charucocalibration.png)
3535

36-
The function to calibrate is `cv::calibrateCamera()`. Example:
36+
The example of using `cv::calibrateCamera()` for cv::aruco::CharucoBoard:
3737

38-
@code{.cpp}
39-
Ptr<aruco::CharucoBoard> board = ... // create charuco board
40-
Size imageSize = ... // camera image size
41-
42-
vector<vector<Point2f>> allCharucoCorners;
43-
vector<vector<int>> allCharucoIds;
44-
vector<vector<Point2f>> allImagePoints;
45-
vector<vector<Point3f>> allObjectPoints;
46-
47-
// Detect charuco board from several viewpoints and fill
48-
// allCharucoCorners, allCharucoIds, allImagePoints and allObjectPoints
49-
while(inputVideo.grab()) {
50-
detector.detectBoard(
51-
image, currentCharucoCorners, currentCharucoIds
52-
);
53-
board.matchImagePoints(
54-
currentCharucoCorners, currentCharucoIds,
55-
currentObjectPoints, currentImagePoints
56-
);
57-
58-
...
59-
}
60-
61-
// After capturing in several viewpoints, start calibration
62-
Mat cameraMatrix, distCoeffs;
63-
vector<Mat> rvecs, tvecs;
64-
65-
// Set calibration flags (same than in calibrateCamera() function)
66-
int calibrationFlags = ...
67-
68-
double repError = calibrateCamera(
69-
allObjectPoints, allImagePoints, imageSize,
70-
cameraMatrix, distCoeffs, rvecs, tvecs, noArray(),
71-
noArray(), noArray(), calibrationFlags
72-
);
73-
@endcode
38+
@snippet samples/calibrate_camera_charuco.cpp CalibrationWithCharucoBoard1
39+
@snippet samples/calibrate_camera_charuco.cpp CalibrationWithCharucoBoard2
40+
@snippet samples/calibrate_camera_charuco.cpp CalibrationWithCharucoBoard3
7441

75-
The ChArUco corners and ChArUco identifiers captured on each viewpoint are stored in the vectors ```allCharucoCorners``` and ```allCharucoIds```, one element per viewpoint.
42+
The ChArUco corners and ChArUco identifiers captured on each viewpoint are stored in the vectors `allCharucoCorners` and `allCharucoIds`, one element per viewpoint.
7643

7744
The `calibrateCamera()` function will fill the `cameraMatrix` and `distCoeffs` arrays with the camera calibration parameters. It will return the reprojection
7845
error obtained from the calibration. The elements in `rvecs` and `tvecs` will be filled with the estimated pose of the camera (respect to the ChArUco board)
@@ -82,7 +49,8 @@ Finally, the `calibrationFlags` parameter determines some of the options for the
8249

8350
A full working example is included in the `calibrate_camera_charuco.cpp` inside the `samples` folder.
8451

85-
Note: The samples now take input via commandline via the [OpenCV Commandline Parser](http://docs.opencv.org/trunk/d0/d2e/classcv_1_1CommandLineParser.html#gsc.tab=0). For this file the example parameters will look like
52+
The samples now take input via commandline via the `cv::CommandLineParser`. For this file the example
53+
parameters will look like:
8654
@code{.cpp}
8755
"camera_calib.txt" -w=5 -h=7 -sl=0.04 -ml=0.02 -d=10
8856
-v="path_aruco/tutorials/aruco_calibration/images/img_%02d.jpg
@@ -100,39 +68,16 @@ based on ArUco boards. As in the previous case, it requires the detections of an
10068

10169
![ArUco calibration viewpoints](images/arucocalibration.png)
10270

103-
Example of ```calibrateCameraAruco()``` use:
71+
The example of using `cv::calibrateCamera()` for cv::aruco::GridBoard:
10472

105-
@code{.cpp}
106-
Ptr<aruco::Board> board = ... // create aruco board
107-
Size imgSize = ... // camera image size
108-
109-
vector<vector<vector<Point2f>>> allMarkerCorners;
110-
vector<vector<int>> allMarkerIds;
111-
112-
// Detect aruco board from several viewpoints and fill allMarkerCorners, allMarkerIds
113-
detector.detectMarkers(image, markerCorners, markerIds, rejectedMarkers);
114-
...
115-
116-
// After capturing in several viewpoints, match image points and start calibration
117-
board->matchImagePoints(
118-
allMarkerCorners[frame], allMarkerIds[frame],
119-
currentObjPoints, currentImgPoints
120-
);
121-
122-
Mat cameraMatrix, distCoeffs;
123-
vector<Mat> rvecs, tvecs;
124-
int calibrationFlags = ... // Set calibration flags (same than in calibrateCamera() function)
125-
126-
double repError = calibrateCamera(
127-
processedObjectPoints, processedImagePoints, imageSize,
128-
cameraMatrix, distCoeffs, rvecs, tvecs, noArray(),
129-
noArray(), noArray(), calibrationFlags
130-
);
131-
@endcode
73+
@snippet samples/calibrate_camera.cpp CalibrationWithArucoBoard1
74+
@snippet samples/calibrate_camera.cpp CalibrationWithArucoBoard2
75+
@snippet samples/calibrate_camera.cpp CalibrationWithArucoBoard3
13276

13377
A full working example is included in the `calibrate_camera.cpp` inside the `samples` folder.
13478

135-
Note: The samples now take input via commandline via the [OpenCV Commandline Parser](http://docs.opencv.org/trunk/d0/d2e/classcv_1_1CommandLineParser.html#gsc.tab=0). For this file the example parameters will look like
79+
The samples now take input via commandline via the `cv::CommandLineParser`. For this file the example
80+
parameters will look like:
13681
@code{.cpp}
13782
"camera_calib.txt" -w=5 -h=7 -l=100 -s=10 -d=10
13883
@endcode

0 commit comments

Comments
 (0)