Skip to content

Commit 46e94a2

Browse files
committed
Update atuco calibration tutorials and faq
1 parent e247b68 commit 46e94a2

File tree

4 files changed

+224
-269
lines changed

4 files changed

+224
-269
lines changed

modules/aruco/samples/calibrate_camera.cpp

Lines changed: 76 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
1-
/*
2-
By downloading, copying, installing or using the software you agree to this
3-
license. If you do not agree to this license, do not download, install,
4-
copy or use the software.
5-
6-
License Agreement
7-
For Open Source Computer Vision Library
8-
(3-clause BSD License)
9-
10-
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
11-
Third party copyrights are property of their respective owners.
12-
13-
Redistribution and use in source and binary forms, with or without modification,
14-
are permitted provided that the following conditions are met:
15-
16-
* Redistributions of source code must retain the above copyright notice,
17-
this list of conditions and the following disclaimer.
18-
19-
* Redistributions in binary form must reproduce the above copyright notice,
20-
this list of conditions and the following disclaimer in the documentation
21-
and/or other materials provided with the distribution.
22-
23-
* Neither the names of the copyright holders nor the names of the contributors
24-
may be used to endorse or promote products derived from this software
25-
without specific prior written permission.
26-
27-
This software is provided by the copyright holders and contributors "as is" and
28-
any express or implied warranties, including, but not limited to, the implied
29-
warranties of merchantability and fitness for a particular purpose are
30-
disclaimed. In no event shall copyright holders or contributors be liable for
31-
any direct, indirect, incidental, special, exemplary, or consequential damages
32-
(including, but not limited to, procurement of substitute goods or services;
33-
loss of use, data, or profits; or business interruption) however caused
34-
and on any theory of liability, whether in contract, strict liability,
35-
or tort (including negligence or otherwise) arising in any way out of
36-
the use of this software, even if advised of the possibility of such damage.
37-
*/
38-
39-
40-
#include <opencv2/highgui.hpp>
1+
#include <ctime>
2+
#include <iostream>
3+
#include <vector>
414
#include <opencv2/calib3d.hpp>
42-
#include <opencv2/objdetect/aruco_detector.hpp>
43-
#include <opencv2/aruco/aruco_calib.hpp>
5+
#include <opencv2/highgui.hpp>
446
#include <opencv2/imgproc.hpp>
45-
#include <vector>
46-
#include <iostream>
47-
#include <ctime>
7+
#include <opencv2/objdetect.hpp>
488
#include "aruco_samples_utility.hpp"
499

5010
using namespace std;
@@ -138,7 +98,9 @@ int main(int argc, char *argv[]) {
13898
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(0);
13999
if (parser.has("d")) {
140100
int dictionaryId = parser.get<int>("d");
141-
dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
101+
dictionary = aruco::getPredefinedDictionary(
102+
aruco::PredefinedDictionaryType(dictionaryId)
103+
);
142104
}
143105
else if (parser.has("cd")) {
144106
FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
@@ -153,80 +115,107 @@ int main(int argc, char *argv[]) {
153115
return 0;
154116
}
155117

156-
// create board object
157-
Ptr<aruco::GridBoard> gridboard = new aruco::GridBoard(Size(markersX, markersY), markerLength, markerSeparation, dictionary);
118+
// Create board object
119+
Ptr<aruco::GridBoard> gridboard = new aruco::GridBoard(
120+
Size(markersX, markersY), markerLength, markerSeparation, dictionary
121+
);
158122
Ptr<aruco::Board> board = gridboard.staticCast<aruco::Board>();
159123

160-
// collected frames for calibration
161-
vector< vector< vector< Point2f > > > allCorners;
162-
vector< vector< int > > allIds;
163-
Size imgSize;
124+
// Collected frames for calibration
125+
vector<vector<vector<Point2f>>> allMarkerCorners;
126+
vector<vector<int>> allMarkerIds;
127+
Size imageSize;
164128

165129
aruco::ArucoDetector detector(dictionary, detectorParams);
166130

167131
while(inputVideo.grab()) {
168132
Mat image, imageCopy;
169133
inputVideo.retrieve(image);
170134

171-
vector< int > ids;
172-
vector< vector< Point2f > > corners, rejected;
135+
vector<int> markerIds;
136+
vector<vector<Point2f>> markerCorners, rejectedMarkers;
173137

174-
// detect markers
175-
detector.detectMarkers(image, corners, ids, rejected);
138+
// Detect markers
139+
detector.detectMarkers(image, markerCorners, markerIds, rejectedMarkers);
176140

177-
// refind strategy to detect more markers
178-
if(refindStrategy) detector.refineDetectedMarkers(image, *board, corners, ids, rejected);
141+
// Refind strategy to detect more markers
142+
if(refindStrategy) {
143+
detector.refineDetectedMarkers(
144+
image, *board, markerCorners, markerIds, rejectedMarkers
145+
);
146+
}
179147

180-
// draw results
148+
// Draw results
181149
image.copyTo(imageCopy);
182-
if(ids.size() > 0) aruco::drawDetectedMarkers(imageCopy, corners, ids);
183-
putText(imageCopy, "Press 'c' to add current frame. 'ESC' to finish and calibrate",
184-
Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 2);
185150

151+
if(!markerIds.empty()) {
152+
aruco::drawDetectedMarkers(imageCopy, markerCorners, markerIds);
153+
}
154+
155+
putText(
156+
imageCopy, "Press 'c' to add current frame. 'ESC' to finish and calibrate",
157+
Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 2
158+
);
186159
imshow("out", imageCopy);
160+
161+
// Wait for key pressed
187162
char key = (char)waitKey(waitTime);
188-
if(key == 27) break;
189-
if(key == 'c' && ids.size() > 0) {
163+
164+
if(key == 27) {
165+
break;
166+
}
167+
168+
if(key == 'c' && !markerIds.empty()) {
190169
cout << "Frame captured" << endl;
191-
allCorners.push_back(corners);
192-
allIds.push_back(ids);
193-
imgSize = image.size();
170+
allMarkerCorners.push_back(markerCorners);
171+
allMarkerIds.push_back(markerIds);
172+
imageSize = image.size();
194173
}
195174
}
196175

197-
if(allIds.size() < 1) {
176+
if(allMarkerIds.empty()) {
198177
cerr << "Not enough captures for calibration" << endl;
199178
return 0;
200179
}
201180

202181
Mat cameraMatrix, distCoeffs;
203-
vector< Mat > rvecs, tvecs;
204-
double repError;
205182

206183
if(calibrationFlags & CALIB_FIX_ASPECT_RATIO) {
207184
cameraMatrix = Mat::eye(3, 3, CV_64F);
208-
cameraMatrix.at< double >(0, 0) = aspectRatio;
185+
cameraMatrix.at<double>(0, 0) = aspectRatio;
209186
}
210187

211-
// prepare data for calibration
212-
vector< vector< Point2f > > allCornersConcatenated;
213-
vector< int > allIdsConcatenated;
214-
vector< int > markerCounterPerFrame;
215-
markerCounterPerFrame.reserve(allCorners.size());
216-
for(unsigned int i = 0; i < allCorners.size(); i++) {
217-
markerCounterPerFrame.push_back((int)allCorners[i].size());
218-
for(unsigned int j = 0; j < allCorners[i].size(); j++) {
219-
allCornersConcatenated.push_back(allCorners[i][j]);
220-
allIdsConcatenated.push_back(allIds[i][j]);
188+
// Prepare data for calibration
189+
vector<Point3f> objectPoints;
190+
vector<Point2f> imagePoints;
191+
vector<Mat> processedObjectPoints, processedImagePoints;
192+
size_t nFrames = allMarkerCorners.size();
193+
194+
for(size_t frame = 0; frame < nFrames; frame++) {
195+
Mat currentImgPoints, currentObjPoints;
196+
197+
board->matchImagePoints(
198+
allMarkerCorners[frame], allMarkerIds[frame],
199+
currentObjPoints, currentImgPoints
200+
);
201+
202+
if(currentImgPoints.total() > 0 && currentObjPoints.total() > 0) {
203+
processedImagePoints.push_back(currentImgPoints);
204+
processedObjectPoints.push_back(currentObjPoints);
221205
}
222206
}
223-
// calibrate camera
224-
repError = aruco::calibrateCameraAruco(allCornersConcatenated, allIdsConcatenated,
225-
markerCounterPerFrame, board, imgSize, cameraMatrix,
226-
distCoeffs, rvecs, tvecs, calibrationFlags);
227207

228-
bool saveOk = saveCameraParams(outputFile, imgSize, aspectRatio, calibrationFlags, cameraMatrix,
229-
distCoeffs, repError);
208+
// Calibrate camera
209+
double repError = calibrateCamera(
210+
processedObjectPoints, processedImagePoints, imageSize,
211+
cameraMatrix, distCoeffs, noArray(), noArray(), noArray(),
212+
noArray(), noArray(), calibrationFlags
213+
);
214+
215+
bool saveOk = saveCameraParams(
216+
outputFile, imageSize, aspectRatio, calibrationFlags, cameraMatrix,
217+
distCoeffs, repError
218+
);
230219

231220
if(!saveOk) {
232221
cerr << "Cannot save output file" << endl;

0 commit comments

Comments
 (0)