Skip to content

Commit 5fd0f91

Browse files
author
AleksandrPanov
committed
fix ChArUco and calibrate, add tests, add samples_utility.hpp
1 parent dcca3ec commit 5fd0f91

26 files changed

+399
-432
lines changed

modules/aruco/samples/calibrate_camera.cpp

Lines changed: 22 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 "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,26 @@ 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+
{
139+
int dictionaryId = parser.get<int>("d");
140+
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
141+
}
142+
else if (parser.has("cd"))
143+
{
144+
bool readOk = readDictionary(parser.get<string>("cd"), dictionary);
145+
if(!readOk)
146+
{
147+
cerr << "Invalid dictionary file" << endl;
148+
return 0;
149+
}
150+
}
151+
else
152+
{
153+
cerr << "Dictionary not specified" << endl;
154+
return 0;
155+
}
210156

211157
// create board object
212158
Ptr<aruco::GridBoard> gridboard =

modules/aruco/samples/calibrate_camera_charuco.cpp

Lines changed: 22 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 "samples_utility.hpp"
4748

4849
using namespace std;
4950
using namespace cv;
@@ -63,6 +64,7 @@ const char* keys =
6364
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
6465
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
6566
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
67+
"{cd | | Input file with custom dictionary }"
6668
"{@outfile |<none> | Output file with calibrated camera parameters }"
6769
"{v | | Input from video file, if ommited, input comes from camera }"
6870
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
@@ -74,80 +76,7 @@ const char* keys =
7476
"{sc | false | Show detected chessboard corners after calibration }";
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 squaresY = parser.get<int>("h");
16291
float squareLength = parser.get<float>("sl");
16392
float markerLength = parser.get<float>("ml");
164-
int dictionaryId = parser.get<int>("d");
16593
string outputFile = parser.get<string>(0);
16694

16795
bool showChessboardCorners = parser.get<bool>("sc");
@@ -207,8 +135,26 @@ int main(int argc, char *argv[]) {
207135
waitTime = 10;
208136
}
209137

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

213159
// create charuco board object
214160
Ptr<aruco::CharucoBoard> charucoboard =

modules/aruco/samples/create_board.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ the use of this software, even if advised of the possibility of such damage.
3939

4040
#include <opencv2/highgui.hpp>
4141
#include <opencv2/aruco.hpp>
42+
#include <iostream>
4243

4344
using namespace cv;
4445

@@ -54,11 +55,36 @@ const char* keys =
5455
"DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
5556
"DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
5657
"DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
58+
"{cd | | Input file with custom dictionary }"
5759
"{m | | Margins size (in pixels). Default is marker separation (-s) }"
5860
"{bb | 1 | Number of bits in marker borders }"
5961
"{si | false | show generated image }";
6062
}
6163

64+
static bool readDictionary(std::string filename, Ptr<aruco::Dictionary> &dictionary)
65+
{
66+
FileStorage fs(filename, FileStorage::READ);
67+
if (!fs.isOpened())
68+
return false;
69+
int nMarkers = 0, markerSize = 0;
70+
fs["nmarkers"] >> nMarkers;
71+
fs["markersize"] >> markerSize;
72+
73+
Mat bytes(0, 0, CV_8UC1), marker(markerSize, markerSize, CV_8UC1);
74+
std::string markerString;
75+
for (int i = 0; i < nMarkers; i++)
76+
{
77+
std::ostringstream ostr;
78+
ostr << i;
79+
fs["marker_" + ostr.str()] >> markerString;
80+
for (int j = 0; j < (int)markerString.size(); j++)
81+
marker.at<unsigned char>(j) = (markerString[j] == '0') ? 0 : 1;
82+
bytes.push_back(aruco::Dictionary::getByteListFromBits(marker));
83+
}
84+
dictionary = makePtr<aruco::Dictionary>(bytes, markerSize);
85+
return true;
86+
}
87+
6288
int main(int argc, char *argv[]) {
6389
CommandLineParser parser(argc, argv, keys);
6490
parser.about(about);
@@ -72,7 +98,6 @@ int main(int argc, char *argv[]) {
7298
int markersY = parser.get<int>("h");
7399
int markerLength = parser.get<int>("l");
74100
int markerSeparation = parser.get<int>("s");
75-
int dictionaryId = parser.get<int>("d");
76101
int margins = markerSeparation;
77102
if(parser.has("m")) {
78103
margins = parser.get<int>("m");
@@ -93,8 +118,26 @@ int main(int argc, char *argv[]) {
93118
imageSize.height =
94119
markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins;
95120

96-
Ptr<aruco::Dictionary> dictionary =
97-
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
121+
Ptr<aruco::Dictionary> dictionary;
122+
if (parser.has("d"))
123+
{
124+
int dictionaryId = parser.get<int>("d");
125+
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
126+
}
127+
else if (parser.has("cd"))
128+
{
129+
bool readOk = readDictionary(parser.get<std::string>("cd"), dictionary);
130+
if(!readOk)
131+
{
132+
std::cerr << "Invalid dictionary file" << std::endl;
133+
return 0;
134+
}
135+
}
136+
else
137+
{
138+
std::cerr << "Dictionary not specified" << std::endl;
139+
return 0;
140+
}
98141

99142
Ptr<aruco::GridBoard> board = aruco::GridBoard::create(markersX, markersY, float(markerLength),
100143
float(markerSeparation), dictionary);

0 commit comments

Comments
 (0)