Skip to content

Commit

Permalink
fix ChArUco and calibrate, add tests, add samples_utility.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrPanov committed Dec 8, 2021
1 parent dcca3ec commit 5fd0f91
Show file tree
Hide file tree
Showing 26 changed files with 399 additions and 432 deletions.
98 changes: 22 additions & 76 deletions modules/aruco/samples/calibrate_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ the use of this software, even if advised of the possibility of such damage.
#include <vector>
#include <iostream>
#include <ctime>
#include "samples_utility.hpp"

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

/**
*/
static bool readDetectorParameters(string filename, Ptr<aruco::DetectorParameters> &params) {
FileStorage fs(filename, FileStorage::READ);
if(!fs.isOpened())
return false;
fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
fs["minDistanceToBorder"] >> params->minDistanceToBorder;
fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
fs["markerBorderBits"] >> params->markerBorderBits;
fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
fs["minOtsuStdDev"] >> params->minOtsuStdDev;
fs["errorCorrectionRate"] >> params->errorCorrectionRate;
return true;
}



/**
*/
static bool saveCameraParams(const string &filename, Size imageSize, float aspectRatio, int flags,
const Mat &cameraMatrix, const Mat &distCoeffs, double totalAvgErr) {
FileStorage fs(filename, FileStorage::WRITE);
if(!fs.isOpened())
return false;

time_t tt;
time(&tt);
struct tm *t2 = localtime(&tt);
char buf[1024];
strftime(buf, sizeof(buf) - 1, "%c", t2);

fs << "calibration_time" << buf;

fs << "image_width" << imageSize.width;
fs << "image_height" << imageSize.height;

if(flags & CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;

if(flags != 0) {
sprintf(buf, "flags: %s%s%s%s",
flags & CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
flags & CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
flags & CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
flags & CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
}

fs << "flags" << flags;

fs << "camera_matrix" << cameraMatrix;
fs << "distortion_coefficients" << distCoeffs;

fs << "avg_reprojection_error" << totalAvgErr;

return true;
}



/**
*/
int main(int argc, char *argv[]) {
CommandLineParser parser(argc, argv, keys);
parser.about(about);
Expand All @@ -161,7 +90,6 @@ int main(int argc, char *argv[]) {
int markersY = parser.get<int>("h");
float markerLength = parser.get<float>("l");
float markerSeparation = parser.get<float>("s");
int dictionaryId = parser.get<int>("d");
string outputFile = parser.get<String>(0);

int calibrationFlags = 0;
Expand Down Expand Up @@ -205,8 +133,26 @@ int main(int argc, char *argv[]) {
waitTime = 10;
}

Ptr<aruco::Dictionary> dictionary =
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
Ptr<aruco::Dictionary> dictionary;
if (parser.has("d"))
{
int dictionaryId = parser.get<int>("d");
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
}
else if (parser.has("cd"))
{
bool readOk = readDictionary(parser.get<string>("cd"), dictionary);
if(!readOk)
{
cerr << "Invalid dictionary file" << endl;
return 0;
}
}
else
{
cerr << "Dictionary not specified" << endl;
return 0;
}

// create board object
Ptr<aruco::GridBoard> gridboard =
Expand Down
98 changes: 22 additions & 76 deletions modules/aruco/samples/calibrate_camera_charuco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ the use of this software, even if advised of the possibility of such damage.
#include <vector>
#include <iostream>
#include <ctime>
#include "samples_utility.hpp"

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

/**
*/
static bool readDetectorParameters(string filename, Ptr<aruco::DetectorParameters> &params) {
FileStorage fs(filename, FileStorage::READ);
if(!fs.isOpened())
return false;
fs["adaptiveThreshWinSizeMin"] >> params->adaptiveThreshWinSizeMin;
fs["adaptiveThreshWinSizeMax"] >> params->adaptiveThreshWinSizeMax;
fs["adaptiveThreshWinSizeStep"] >> params->adaptiveThreshWinSizeStep;
fs["adaptiveThreshConstant"] >> params->adaptiveThreshConstant;
fs["minMarkerPerimeterRate"] >> params->minMarkerPerimeterRate;
fs["maxMarkerPerimeterRate"] >> params->maxMarkerPerimeterRate;
fs["polygonalApproxAccuracyRate"] >> params->polygonalApproxAccuracyRate;
fs["minCornerDistanceRate"] >> params->minCornerDistanceRate;
fs["minDistanceToBorder"] >> params->minDistanceToBorder;
fs["minMarkerDistanceRate"] >> params->minMarkerDistanceRate;
fs["cornerRefinementMethod"] >> params->cornerRefinementMethod;
fs["cornerRefinementWinSize"] >> params->cornerRefinementWinSize;
fs["cornerRefinementMaxIterations"] >> params->cornerRefinementMaxIterations;
fs["cornerRefinementMinAccuracy"] >> params->cornerRefinementMinAccuracy;
fs["markerBorderBits"] >> params->markerBorderBits;
fs["perspectiveRemovePixelPerCell"] >> params->perspectiveRemovePixelPerCell;
fs["perspectiveRemoveIgnoredMarginPerCell"] >> params->perspectiveRemoveIgnoredMarginPerCell;
fs["maxErroneousBitsInBorderRate"] >> params->maxErroneousBitsInBorderRate;
fs["minOtsuStdDev"] >> params->minOtsuStdDev;
fs["errorCorrectionRate"] >> params->errorCorrectionRate;
return true;
}



/**
*/
static bool saveCameraParams(const string &filename, Size imageSize, float aspectRatio, int flags,
const Mat &cameraMatrix, const Mat &distCoeffs, double totalAvgErr) {
FileStorage fs(filename, FileStorage::WRITE);
if(!fs.isOpened())
return false;

time_t tt;
time(&tt);
struct tm *t2 = localtime(&tt);
char buf[1024];
strftime(buf, sizeof(buf) - 1, "%c", t2);

fs << "calibration_time" << buf;

fs << "image_width" << imageSize.width;
fs << "image_height" << imageSize.height;

if(flags & CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;

if(flags != 0) {
sprintf(buf, "flags: %s%s%s%s",
flags & CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
flags & CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
flags & CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
flags & CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
}

fs << "flags" << flags;

fs << "camera_matrix" << cameraMatrix;
fs << "distortion_coefficients" << distCoeffs;

fs << "avg_reprojection_error" << totalAvgErr;

return true;
}



/**
*/
int main(int argc, char *argv[]) {
CommandLineParser parser(argc, argv, keys);
parser.about(about);
Expand All @@ -161,7 +90,6 @@ int main(int argc, char *argv[]) {
int squaresY = parser.get<int>("h");
float squareLength = parser.get<float>("sl");
float markerLength = parser.get<float>("ml");
int dictionaryId = parser.get<int>("d");
string outputFile = parser.get<string>(0);

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

Ptr<aruco::Dictionary> dictionary =
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
Ptr<aruco::Dictionary> dictionary;
if (parser.has("d"))
{
int dictionaryId = parser.get<int>("d");
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
}
else if (parser.has("cd"))
{
bool readOk = readDictionary(parser.get<string>("cd"), dictionary);
if(!readOk)
{
cerr << "Invalid dictionary file" << endl;
return 0;
}
}
else
{
cerr << "Dictionary not specified" << endl;
return 0;
}

// create charuco board object
Ptr<aruco::CharucoBoard> charucoboard =
Expand Down
49 changes: 46 additions & 3 deletions modules/aruco/samples/create_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ the use of this software, even if advised of the possibility of such damage.

#include <opencv2/highgui.hpp>
#include <opencv2/aruco.hpp>
#include <iostream>

using namespace cv;

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

static bool readDictionary(std::string filename, Ptr<aruco::Dictionary> &dictionary)
{
FileStorage fs(filename, FileStorage::READ);
if (!fs.isOpened())
return false;
int nMarkers = 0, markerSize = 0;
fs["nmarkers"] >> nMarkers;
fs["markersize"] >> markerSize;

Mat bytes(0, 0, CV_8UC1), marker(markerSize, markerSize, CV_8UC1);
std::string markerString;
for (int i = 0; i < nMarkers; i++)
{
std::ostringstream ostr;
ostr << i;
fs["marker_" + ostr.str()] >> markerString;
for (int j = 0; j < (int)markerString.size(); j++)
marker.at<unsigned char>(j) = (markerString[j] == '0') ? 0 : 1;
bytes.push_back(aruco::Dictionary::getByteListFromBits(marker));
}
dictionary = makePtr<aruco::Dictionary>(bytes, markerSize);
return true;
}

int main(int argc, char *argv[]) {
CommandLineParser parser(argc, argv, keys);
parser.about(about);
Expand All @@ -72,7 +98,6 @@ int main(int argc, char *argv[]) {
int markersY = parser.get<int>("h");
int markerLength = parser.get<int>("l");
int markerSeparation = parser.get<int>("s");
int dictionaryId = parser.get<int>("d");
int margins = markerSeparation;
if(parser.has("m")) {
margins = parser.get<int>("m");
Expand All @@ -93,8 +118,26 @@ int main(int argc, char *argv[]) {
imageSize.height =
markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins;

Ptr<aruco::Dictionary> dictionary =
aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
Ptr<aruco::Dictionary> dictionary;
if (parser.has("d"))
{
int dictionaryId = parser.get<int>("d");
dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
}
else if (parser.has("cd"))
{
bool readOk = readDictionary(parser.get<std::string>("cd"), dictionary);
if(!readOk)
{
std::cerr << "Invalid dictionary file" << std::endl;
return 0;
}
}
else
{
std::cerr << "Dictionary not specified" << std::endl;
return 0;
}

Ptr<aruco::GridBoard> board = aruco::GridBoard::create(markersX, markersY, float(markerLength),
float(markerSeparation), dictionary);
Expand Down
Loading

0 comments on commit 5fd0f91

Please sign in to comment.