Skip to content

Commit 48d852d

Browse files
author
AleksandrPanov
committed
fix ArUco, ChArUco, diamond markers, add calibration photos, add tests (with check corners and ids), add aruco_samples_utility.hpp
1 parent 5cc328d commit 48d852d

36 files changed

+659
-417
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <opencv2/highgui.hpp>
2+
#include <opencv2/aruco.hpp>
3+
#include <opencv2/calib3d.hpp>
4+
#include <ctime>
5+
6+
static bool readCameraParameters(std::string filename, cv::Mat &camMatrix, cv::Mat &distCoeffs) {
7+
cv::FileStorage fs(filename, cv::FileStorage::READ);
8+
if(!fs.isOpened())
9+
return false;
10+
fs["camera_matrix"] >> camMatrix;
11+
fs["distortion_coefficients"] >> distCoeffs;
12+
return true;
13+
}
14+
15+
static bool saveCameraParams(const std::string &filename, cv::Size imageSize, float aspectRatio, int flags,
16+
const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, double totalAvgErr) {
17+
cv::FileStorage fs(filename, cv::FileStorage::WRITE);
18+
if(!fs.isOpened())
19+
return false;
20+
21+
time_t tt;
22+
time(&tt);
23+
struct tm *t2 = localtime(&tt);
24+
char buf[1024];
25+
strftime(buf, sizeof(buf) - 1, "%c", t2);
26+
27+
fs << "calibration_time" << buf;
28+
29+
fs << "image_width" << imageSize.width;
30+
fs << "image_height" << imageSize.height;
31+
32+
if(flags & cv::CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;
33+
34+
if(flags != 0) {
35+
sprintf(buf, "flags: %s%s%s%s",
36+
flags & cv::CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
37+
flags & cv::CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
38+
flags & cv::CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
39+
flags & cv::CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
40+
}
41+
42+
fs << "flags" << flags;
43+
44+
fs << "camera_matrix" << cameraMatrix;
45+
fs << "distortion_coefficients" << distCoeffs;
46+
47+
fs << "avg_reprojection_error" << totalAvgErr;
48+
49+
return true;
50+
}
51+
52+
static bool readDetectorParameters(std::string filename, cv::Ptr<cv::aruco::DetectorParameters> &params) {
53+
cv::FileStorage fs(filename, cv::FileStorage::READ);
54+
if(!fs.isOpened())
55+
return false;
56+
fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
57+
fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
58+
fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
59+
fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
60+
fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
61+
fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
62+
fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
63+
fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
64+
fs["minDistanceToBorder"] >> params->minDistanceToBorder;
65+
fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
66+
fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
67+
fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
68+
fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
69+
fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
70+
fs["markerBorderBits"] >> params->markerBorderBits;
71+
fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
72+
fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
73+
fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
74+
fs["minOtsuStdDev"] >> params->minOtsuStdDev;
75+
fs["errorCorrectionRate"] >> params->errorCorrectionRate;
76+
return true;
77+
}
78+
79+
static bool readDictionary(std::string filename, cv::Ptr<cv::aruco::Dictionary> &dictionary)
80+
{
81+
cv::FileStorage fs(filename, cv::FileStorage::READ);
82+
if (!fs.isOpened())
83+
return false;
84+
int nMarkers = 0, markerSize = 0;
85+
fs["nmarkers"] >> nMarkers;
86+
fs["markersize"] >> markerSize;
87+
88+
cv::Mat bytes(0, 0, CV_8UC1), marker(markerSize, markerSize, CV_8UC1);
89+
std::string markerString;
90+
for (int i = 0; i < nMarkers; i++)
91+
{
92+
std::ostringstream ostr;
93+
ostr << i;
94+
fs["marker_" + ostr.str()] >> markerString;
95+
for (int j = 0; j < (int)markerString.size(); j++)
96+
marker.at<unsigned char>(j) = (markerString[j] == '0') ? 0 : 1;
97+
bytes.push_back(cv::aruco::Dictionary::getByteListFromBits(marker));
98+
}
99+
dictionary = cv::makePtr<cv::aruco::Dictionary>(bytes, markerSize);
100+
return true;
101+
}

modules/aruco/samples/calibrate_camera.cpp

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ the use of this software, even if advised of the possibility of such damage.
4444
#include <vector>
4545
#include <iostream>
4646
#include <ctime>
47+
#include "aruco_samples_utility.hpp"
4748

4849
using namespace std;
4950
using namespace cv;
@@ -64,6 +65,7 @@ const char* keys =
6465
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
6566
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
6667
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
68+
"{cd | | Input file with custom dictionary }"
6769
"{@outfile |<none> | Output file with calibrated camera parameters }"
6870
"{v | | Input from video file, if ommited, input comes from camera }"
6971
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
@@ -74,80 +76,7 @@ const char* keys =
7476
"{pc | false | Fix the principal point at the center }";
7577
}
7678

77-
/**
78-
*/
79-
static bool readDetectorParameters(string filename, Ptr<aruco::DetectorParameters> &params) {
80-
FileStorage fs(filename, FileStorage::READ);
81-
if(!fs.isOpened())
82-
return false;
83-
fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
84-
fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
85-
fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
86-
fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
87-
fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
88-
fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
89-
fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
90-
fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
91-
fs["minDistanceToBorder"] >> params->minDistanceToBorder;
92-
fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
93-
fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
94-
fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
95-
fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
96-
fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
97-
fs["markerBorderBits"] >> params->markerBorderBits;
98-
fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
99-
fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
100-
fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
101-
fs["minOtsuStdDev"] >> params->minOtsuStdDev;
102-
fs["errorCorrectionRate"] >> params->errorCorrectionRate;
103-
return true;
104-
}
105-
106-
107-
108-
/**
109-
*/
110-
static bool saveCameraParams(const string &filename, Size imageSize, float aspectRatio, int flags,
111-
const Mat &cameraMatrix, const Mat &distCoeffs, double totalAvgErr) {
112-
FileStorage fs(filename, FileStorage::WRITE);
113-
if(!fs.isOpened())
114-
return false;
115-
116-
time_t tt;
117-
time(&tt);
118-
struct tm *t2 = localtime(&tt);
119-
char buf[1024];
120-
strftime(buf, sizeof(buf) - 1, "%c", t2);
121-
122-
fs << "calibration_time" << buf;
123-
124-
fs << "image_width" << imageSize.width;
125-
fs << "image_height" << imageSize.height;
126-
127-
if(flags & CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;
128-
129-
if(flags != 0) {
130-
sprintf(buf, "flags: %s%s%s%s",
131-
flags & CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
132-
flags & CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
133-
flags & CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
134-
flags & CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
135-
}
136-
137-
fs << "flags" << flags;
138-
139-
fs << "camera_matrix" << cameraMatrix;
140-
fs << "distortion_coefficients" << distCoeffs;
14179

142-
fs << "avg_reprojection_error" << totalAvgErr;
143-
144-
return true;
145-
}
146-
147-
148-
149-
/**
150-
*/
15180
int main(int argc, char *argv[]) {
15281
CommandLineParser parser(argc, argv, keys);
15382
parser.about(about);
@@ -161,7 +90,6 @@ int main(int argc, char *argv[]) {
16190
int markersY = parser.get<int>("h");
16291
float markerLength = parser.get<float>("l");
16392
float markerSeparation = parser.get<float>("s");
164-
int dictionaryId = parser.get<int>("d");
16593
string outputFile = parser.get<String>(0);
16694

16795
int calibrationFlags = 0;
@@ -205,8 +133,22 @@ int main(int argc, char *argv[]) {
205133
waitTime = 10;
206134
}
207135

208-
Ptr<aruco::Dictionary> dictionary =
209-
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
136+
Ptr<aruco::Dictionary> dictionary;
137+
if (parser.has("d")) {
138+
int dictionaryId = parser.get<int>("d");
139+
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
140+
}
141+
else if (parser.has("cd")) {
142+
bool readOk = readDictionary(parser.get<string>("cd"), dictionary);
143+
if(!readOk) {
144+
cerr << "Invalid dictionary file" << endl;
145+
return 0;
146+
}
147+
}
148+
else {
149+
cerr << "Dictionary not specified" << endl;
150+
return 0;
151+
}
210152

211153
// create board object
212154
Ptr<aruco::GridBoard> gridboard =

modules/aruco/samples/calibrate_camera_charuco.cpp

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ the use of this software, even if advised of the possibility of such damage.
4343
#include <opencv2/imgproc.hpp>
4444
#include <vector>
4545
#include <iostream>
46-
#include <ctime>
46+
#include "aruco_samples_utility.hpp"
4747

4848
using namespace std;
4949
using namespace cv;
@@ -63,6 +63,7 @@ const char* keys =
6363
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
6464
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
6565
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
66+
"{cd | | Input file with custom dictionary }"
6667
"{@outfile |<none> | Output file with calibrated camera parameters }"
6768
"{v | | Input from video file, if ommited, input comes from camera }"
6869
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
@@ -74,80 +75,7 @@ const char* keys =
7475
"{sc | false | Show detected chessboard corners after calibration }";
7576
}
7677

77-
/**
78-
*/
79-
static bool readDetectorParameters(string filename, Ptr<aruco::DetectorParameters> &params) {
80-
FileStorage fs(filename, FileStorage::READ);
81-
if(!fs.isOpened())
82-
return false;
83-
fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
84-
fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
85-
fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
86-
fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
87-
fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
88-
fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
89-
fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
90-
fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
91-
fs["minDistanceToBorder"] >> params->minDistanceToBorder;
92-
fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
93-
fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
94-
fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
95-
fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
96-
fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
97-
fs["markerBorderBits"] >> params->markerBorderBits;
98-
fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
99-
fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
100-
fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
101-
fs["minOtsuStdDev"] >> params->minOtsuStdDev;
102-
fs["errorCorrectionRate"] >> params->errorCorrectionRate;
103-
return true;
104-
}
105-
106-
107-
108-
/**
109-
*/
110-
static bool saveCameraParams(const string &filename, Size imageSize, float aspectRatio, int flags,
111-
const Mat &cameraMatrix, const Mat &distCoeffs, double totalAvgErr) {
112-
FileStorage fs(filename, FileStorage::WRITE);
113-
if(!fs.isOpened())
114-
return false;
115-
116-
time_t tt;
117-
time(&tt);
118-
struct tm *t2 = localtime(&tt);
119-
char buf[1024];
120-
strftime(buf, sizeof(buf) - 1, "%c", t2);
121-
122-
fs << "calibration_time" << buf;
123-
124-
fs << "image_width" << imageSize.width;
125-
fs << "image_height" << imageSize.height;
126-
127-
if(flags & CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;
128-
129-
if(flags != 0) {
130-
sprintf(buf, "flags: %s%s%s%s",
131-
flags & CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
132-
flags & CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
133-
flags & CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
134-
flags & CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
135-
}
136-
137-
fs << "flags" << flags;
138-
139-
fs << "camera_matrix" << cameraMatrix;
140-
fs << "distortion_coefficients" << distCoeffs;
14178

142-
fs << "avg_reprojection_error" << totalAvgErr;
143-
144-
return true;
145-
}
146-
147-
148-
149-
/**
150-
*/
15179
int main(int argc, char *argv[]) {
15280
CommandLineParser parser(argc, argv, keys);
15381
parser.about(about);
@@ -161,7 +89,6 @@ int main(int argc, char *argv[]) {
16189
int squaresY = parser.get<int>("h");
16290
float squareLength = parser.get<float>("sl");
16391
float markerLength = parser.get<float>("ml");
164-
int dictionaryId = parser.get<int>("d");
16592
string outputFile = parser.get<string>(0);
16693

16794
bool showChessboardCorners = parser.get<bool>("sc");
@@ -207,8 +134,22 @@ int main(int argc, char *argv[]) {
207134
waitTime = 10;
208135
}
209136

210-
Ptr<aruco::Dictionary> dictionary =
211-
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
137+
Ptr<aruco::Dictionary> dictionary;
138+
if (parser.has("d")) {
139+
int dictionaryId = parser.get<int>("d");
140+
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
141+
}
142+
else if (parser.has("cd")) {
143+
bool readOk = readDictionary(parser.get<string>("cd"), dictionary);
144+
if(!readOk) {
145+
cerr << "Invalid dictionary file" << endl;
146+
return 0;
147+
}
148+
}
149+
else {
150+
cerr << "Dictionary not specified" << endl;
151+
return 0;
152+
}
212153

213154
// create charuco board object
214155
Ptr<aruco::CharucoBoard> charucoboard =

0 commit comments

Comments
 (0)