mirror of
https://github.com/opencv/opencv.git
synced 2026-07-25 13:23:02 +04:00
Merge pull request #23018 from stopmosk:move-aruco-tutorial
Move Aruco tutorials and samples to main repo #23018 merge with https://github.com/opencv/opencv_contrib/pull/3401 merge with https://github.com/opencv/opencv_extra/pull/1143 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --------- Co-authored-by: AleksandrPanov <alexander.panov@xperience.ai> Co-authored-by: Alexander Smorkalov <alexander.smorkalov@xperience.ai>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/objdetect/aruco_detector.hpp>
|
||||
#include <opencv2/calib3d.hpp>
|
||||
#include <ctime>
|
||||
|
||||
namespace {
|
||||
inline static bool readCameraParameters(const std::string& filename, cv::Mat &camMatrix, cv::Mat &distCoeffs) {
|
||||
cv::FileStorage fs(filename, cv::FileStorage::READ);
|
||||
if (!fs.isOpened())
|
||||
return false;
|
||||
fs["camera_matrix"] >> camMatrix;
|
||||
fs["distortion_coefficients"] >> distCoeffs;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline static bool saveCameraParams(const std::string &filename, cv::Size imageSize, float aspectRatio, int flags,
|
||||
const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, double totalAvgErr) {
|
||||
cv::FileStorage fs(filename, cv::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 & cv::CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;
|
||||
|
||||
if (flags != 0) {
|
||||
sprintf(buf, "flags: %s%s%s%s",
|
||||
flags & cv::CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
|
||||
flags & cv::CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
|
||||
flags & cv::CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
|
||||
flags & cv::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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/objdetect/aruco_detector.hpp>
|
||||
#include <iostream>
|
||||
#include "aruco_samples_utility.hpp"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
namespace {
|
||||
const char* about = "Create an ArUco grid board image";
|
||||
const char* keys =
|
||||
"{@outfile |<none> | Output image }"
|
||||
"{w | | Number of markers in X direction }"
|
||||
"{h | | Number of markers in Y direction }"
|
||||
"{l | | Marker side length (in pixels) }"
|
||||
"{s | | Separation between two consecutive markers in the grid (in pixels)}"
|
||||
"{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
|
||||
"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 }";
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
|
||||
if(argc < 7) {
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int markersX = parser.get<int>("w");
|
||||
int markersY = parser.get<int>("h");
|
||||
int markerLength = parser.get<int>("l");
|
||||
int markerSeparation = parser.get<int>("s");
|
||||
int margins = markerSeparation;
|
||||
if(parser.has("m")) {
|
||||
margins = parser.get<int>("m");
|
||||
}
|
||||
|
||||
int borderBits = parser.get<int>("bb");
|
||||
bool showImage = parser.get<bool>("si");
|
||||
|
||||
String out = parser.get<String>(0);
|
||||
|
||||
if(!parser.check()) {
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Size imageSize;
|
||||
imageSize.width = markersX * (markerLength + markerSeparation) - markerSeparation + 2 * margins;
|
||||
imageSize.height =
|
||||
markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins;
|
||||
|
||||
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
|
||||
if (parser.has("d")) {
|
||||
int dictionaryId = parser.get<int>("d");
|
||||
dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
|
||||
}
|
||||
else if (parser.has("cd")) {
|
||||
FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
|
||||
bool readOk = dictionary.readDictionary(fs.root());
|
||||
if(!readOk)
|
||||
{
|
||||
std::cerr << "Invalid dictionary file" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "Dictionary not specified" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
aruco::GridBoard board(Size(markersX, markersY), float(markerLength), float(markerSeparation), dictionary);
|
||||
|
||||
// show created board
|
||||
//! [aruco_generate_board_image]
|
||||
Mat boardImage;
|
||||
board.generateImage(imageSize, boardImage, margins, borderBits);
|
||||
//! [aruco_generate_board_image]
|
||||
|
||||
if(showImage) {
|
||||
imshow("board", boardImage);
|
||||
waitKey(0);
|
||||
}
|
||||
|
||||
imwrite(out, boardImage);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/objdetect/aruco_detector.hpp>
|
||||
#include <iostream>
|
||||
#include "aruco_samples_utility.hpp"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
namespace {
|
||||
const char* about = "Create an ArUco marker image";
|
||||
|
||||
//! [aruco_create_markers_keys]
|
||||
const char* keys =
|
||||
"{@outfile |<none> | Output image }"
|
||||
"{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
|
||||
"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 }"
|
||||
"{id | | Marker id in the dictionary }"
|
||||
"{ms | 200 | Marker size in pixels }"
|
||||
"{bb | 1 | Number of bits in marker borders }"
|
||||
"{si | false | show generated image }";
|
||||
}
|
||||
//! [aruco_create_markers_keys]
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
|
||||
if(argc < 4) {
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int markerId = parser.get<int>("id");
|
||||
int borderBits = parser.get<int>("bb");
|
||||
int markerSize = parser.get<int>("ms");
|
||||
bool showImage = parser.get<bool>("si");
|
||||
|
||||
String out = parser.get<String>(0);
|
||||
|
||||
if(!parser.check()) {
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
|
||||
if (parser.has("d")) {
|
||||
int dictionaryId = parser.get<int>("d");
|
||||
dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
|
||||
}
|
||||
else if (parser.has("cd")) {
|
||||
FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
|
||||
bool readOk = dictionary.readDictionary(fs.root());
|
||||
if(!readOk) {
|
||||
std::cerr << "Invalid dictionary file" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "Dictionary not specified" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Mat markerImg;
|
||||
aruco::generateImageMarker(dictionary, markerId, markerSize, markerImg, borderBits);
|
||||
|
||||
if(showImage) {
|
||||
imshow("marker", markerImg);
|
||||
waitKey(0);
|
||||
}
|
||||
|
||||
imwrite(out, markerImg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/objdetect/aruco_detector.hpp>
|
||||
#include "aruco_samples_utility.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
namespace {
|
||||
const char* about = "Pose estimation using a ArUco Planar Grid board";
|
||||
|
||||
//! [aruco_detect_board_keys]
|
||||
const char* keys =
|
||||
"{w | | Number of squares in X direction }"
|
||||
"{h | | Number of squares in Y direction }"
|
||||
"{l | | Marker side length (in pixels) }"
|
||||
"{s | | Separation between two consecutive markers in the grid (in pixels)}"
|
||||
"{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
|
||||
"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 }"
|
||||
"{c | | Output file with calibrated camera parameters }"
|
||||
"{v | | Input from video or image file, if omitted, input comes from camera }"
|
||||
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
|
||||
"{dp | | File of marker detector parameters }"
|
||||
"{rs | | Apply refind strategy }"
|
||||
"{r | | show rejected candidates too }";
|
||||
}
|
||||
//! [aruco_detect_board_keys]
|
||||
|
||||
static void readDetectorParamsFromCommandLine(CommandLineParser &parser, aruco::DetectorParameters& detectorParams) {
|
||||
if(parser.has("dp")) {
|
||||
FileStorage fs(parser.get<string>("dp"), FileStorage::READ);
|
||||
bool readOk = detectorParams.readDetectorParameters(fs.root());
|
||||
if(!readOk) {
|
||||
cerr << "Invalid detector parameters file" << endl;
|
||||
throw -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void readCameraParamsFromCommandLine(CommandLineParser &parser, Mat& camMatrix, Mat& distCoeffs) {
|
||||
if(parser.has("c")) {
|
||||
bool readOk = readCameraParameters(parser.get<string>("c"), camMatrix, distCoeffs);
|
||||
if(!readOk) {
|
||||
cerr << "Invalid camera file" << endl;
|
||||
throw -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void readDictionatyFromCommandLine(CommandLineParser &parser, aruco::Dictionary& dictionary) {
|
||||
if (parser.has("d")) {
|
||||
int dictionaryId = parser.get<int>("d");
|
||||
dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
|
||||
}
|
||||
else if (parser.has("cd")) {
|
||||
FileStorage fs(parser.get<string>("cd"), FileStorage::READ);
|
||||
bool readOk = dictionary.readDictionary(fs.root());
|
||||
if(!readOk) {
|
||||
cerr << "Invalid dictionary file" << endl;
|
||||
throw -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cerr << "Dictionary not specified" << endl;
|
||||
throw -1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
|
||||
if(argc < 7) {
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! [aruco_detect_board_full_sample]
|
||||
int markersX = parser.get<int>("w");
|
||||
int markersY = parser.get<int>("h");
|
||||
float markerLength = parser.get<float>("l");
|
||||
float markerSeparation = parser.get<float>("s");
|
||||
bool showRejected = parser.has("r");
|
||||
bool refindStrategy = parser.has("rs");
|
||||
int camId = parser.get<int>("ci");
|
||||
|
||||
|
||||
Mat camMatrix, distCoeffs;
|
||||
readCameraParamsFromCommandLine(parser, camMatrix, distCoeffs);
|
||||
|
||||
aruco::DetectorParameters detectorParams;
|
||||
detectorParams.cornerRefinementMethod = aruco::CORNER_REFINE_SUBPIX; // do corner refinement in markers
|
||||
readDetectorParamsFromCommandLine(parser, detectorParams);
|
||||
|
||||
String video;
|
||||
if(parser.has("v")) {
|
||||
video = parser.get<String>("v");
|
||||
}
|
||||
|
||||
if(!parser.check()) {
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
|
||||
readDictionatyFromCommandLine(parser, dictionary);
|
||||
|
||||
aruco::ArucoDetector detector(dictionary, detectorParams);
|
||||
VideoCapture inputVideo;
|
||||
int waitTime;
|
||||
if(!video.empty()) {
|
||||
inputVideo.open(video);
|
||||
waitTime = 0;
|
||||
} else {
|
||||
inputVideo.open(camId);
|
||||
waitTime = 10;
|
||||
}
|
||||
|
||||
float axisLength = 0.5f * ((float)min(markersX, markersY) * (markerLength + markerSeparation) +
|
||||
markerSeparation);
|
||||
|
||||
// Create GridBoard object
|
||||
//! [aruco_create_board]
|
||||
aruco::GridBoard board(Size(markersX, markersY), markerLength, markerSeparation, dictionary);
|
||||
//! [aruco_create_board]
|
||||
|
||||
// Also you could create Board object
|
||||
//vector<vector<Point3f> > objPoints; // array of object points of all the marker corners in the board
|
||||
//vector<int> ids; // vector of the identifiers of the markers in the board
|
||||
//aruco::Board board(objPoints, dictionary, ids);
|
||||
|
||||
double totalTime = 0;
|
||||
int totalIterations = 0;
|
||||
|
||||
while(inputVideo.grab()) {
|
||||
Mat image, imageCopy;
|
||||
inputVideo.retrieve(image);
|
||||
|
||||
double tick = (double)getTickCount();
|
||||
|
||||
vector<int> ids;
|
||||
vector<vector<Point2f>> corners, rejected;
|
||||
Vec3d rvec, tvec;
|
||||
|
||||
//! [aruco_detect_and_refine]
|
||||
|
||||
// Detect markers
|
||||
detector.detectMarkers(image, corners, ids, rejected);
|
||||
|
||||
// Refind strategy to detect more markers
|
||||
if(refindStrategy)
|
||||
detector.refineDetectedMarkers(image, board, corners, ids, rejected, camMatrix,
|
||||
distCoeffs);
|
||||
|
||||
//! [aruco_detect_and_refine]
|
||||
|
||||
// Estimate board pose
|
||||
int markersOfBoardDetected = 0;
|
||||
if(!ids.empty()) {
|
||||
// Get object and image points for the solvePnP function
|
||||
cv::Mat objPoints, imgPoints;
|
||||
board.matchImagePoints(corners, ids, objPoints, imgPoints);
|
||||
|
||||
// Find pose
|
||||
cv::solvePnP(objPoints, imgPoints, camMatrix, distCoeffs, rvec, tvec);
|
||||
|
||||
markersOfBoardDetected = (int)objPoints.total() / 4;
|
||||
}
|
||||
|
||||
double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
|
||||
totalTime += currentTime;
|
||||
totalIterations++;
|
||||
if(totalIterations % 30 == 0) {
|
||||
cout << "Detection Time = " << currentTime * 1000 << " ms "
|
||||
<< "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl;
|
||||
}
|
||||
|
||||
// Draw results
|
||||
image.copyTo(imageCopy);
|
||||
if(!ids.empty()) {
|
||||
aruco::drawDetectedMarkers(imageCopy, corners, ids);
|
||||
}
|
||||
|
||||
if(showRejected && !rejected.empty())
|
||||
aruco::drawDetectedMarkers(imageCopy, rejected, noArray(), Scalar(100, 0, 255));
|
||||
|
||||
if(markersOfBoardDetected > 0)
|
||||
cv::drawFrameAxes(imageCopy, camMatrix, distCoeffs, rvec, tvec, axisLength);
|
||||
|
||||
imshow("out", imageCopy);
|
||||
char key = (char)waitKey(waitTime);
|
||||
if(key == 27) break;
|
||||
//! [aruco_detect_board_full_sample]
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/objdetect/aruco_detector.hpp>
|
||||
#include <iostream>
|
||||
#include "aruco_samples_utility.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
namespace {
|
||||
const char* about = "Basic marker detection";
|
||||
|
||||
//! [aruco_detect_markers_keys]
|
||||
const char* keys =
|
||||
"{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
|
||||
"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,"
|
||||
"DICT_APRILTAG_16h5=17, DICT_APRILTAG_25h9=18, DICT_APRILTAG_36h10=19, DICT_APRILTAG_36h11=20}"
|
||||
"{cd | | Input file with custom dictionary }"
|
||||
"{v | | Input from video or image file, if ommited, input comes from camera }"
|
||||
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
|
||||
"{c | | Camera intrinsic parameters. Needed for camera pose }"
|
||||
"{l | 0.1 | Marker side length (in meters). Needed for correct scale in camera pose }"
|
||||
"{dp | | File of marker detector parameters }"
|
||||
"{r | | show rejected candidates too }"
|
||||
"{refine | | Corner refinement: CORNER_REFINE_NONE=0, CORNER_REFINE_SUBPIX=1,"
|
||||
"CORNER_REFINE_CONTOUR=2, CORNER_REFINE_APRILTAG=3}";
|
||||
}
|
||||
//! [aruco_detect_markers_keys]
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about(about);
|
||||
|
||||
if(argc < 2) {
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool showRejected = parser.has("r");
|
||||
bool estimatePose = parser.has("c");
|
||||
float markerLength = parser.get<float>("l");
|
||||
|
||||
cv::aruco::DetectorParameters detectorParams;
|
||||
if(parser.has("dp")) {
|
||||
cv::FileStorage fs(parser.get<string>("dp"), FileStorage::READ);
|
||||
bool readOk = detectorParams.readDetectorParameters(fs.root());
|
||||
if(!readOk) {
|
||||
cerr << "Invalid detector parameters file" << endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (parser.has("refine")) {
|
||||
// override cornerRefinementMethod read from config file
|
||||
detectorParams.cornerRefinementMethod = parser.get<aruco::CornerRefineMethod>("refine");
|
||||
}
|
||||
std::cout << "Corner refinement method (0: None, 1: Subpixel, 2:contour, 3: AprilTag 2): " << (int)detectorParams.cornerRefinementMethod << std::endl;
|
||||
|
||||
int camId = parser.get<int>("ci");
|
||||
|
||||
String video;
|
||||
if(parser.has("v")) {
|
||||
video = parser.get<String>("v");
|
||||
}
|
||||
|
||||
if(!parser.check()) {
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
|
||||
if (parser.has("d")) {
|
||||
int dictionaryId = parser.get<int>("d");
|
||||
dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
|
||||
}
|
||||
else if (parser.has("cd")) {
|
||||
cv::FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
|
||||
bool readOk = dictionary.readDictionary(fs.root());
|
||||
if(!readOk) {
|
||||
std::cerr << "Invalid dictionary file" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "Dictionary not specified" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! [aruco_pose_estimation1]
|
||||
cv::Mat camMatrix, distCoeffs;
|
||||
if(estimatePose) {
|
||||
// You can read camera parameters from tutorial_camera_params.yml
|
||||
bool readOk = readCameraParameters(parser.get<string>("c"), camMatrix, distCoeffs);
|
||||
if(!readOk) {
|
||||
cerr << "Invalid camera file" << endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//! [aruco_pose_estimation1]
|
||||
//! [aruco_detect_markers]
|
||||
cv::aruco::ArucoDetector detector(dictionary, detectorParams);
|
||||
cv::VideoCapture inputVideo;
|
||||
int waitTime;
|
||||
if(!video.empty()) {
|
||||
inputVideo.open(video);
|
||||
waitTime = 0;
|
||||
} else {
|
||||
inputVideo.open(camId);
|
||||
waitTime = 10;
|
||||
}
|
||||
|
||||
double totalTime = 0;
|
||||
int totalIterations = 0;
|
||||
|
||||
//! [aruco_pose_estimation2]
|
||||
// set coordinate system
|
||||
cv::Mat objPoints(4, 1, CV_32FC3);
|
||||
objPoints.ptr<Vec3f>(0)[0] = Vec3f(-markerLength/2.f, markerLength/2.f, 0);
|
||||
objPoints.ptr<Vec3f>(0)[1] = Vec3f(markerLength/2.f, markerLength/2.f, 0);
|
||||
objPoints.ptr<Vec3f>(0)[2] = Vec3f(markerLength/2.f, -markerLength/2.f, 0);
|
||||
objPoints.ptr<Vec3f>(0)[3] = Vec3f(-markerLength/2.f, -markerLength/2.f, 0);
|
||||
//! [aruco_pose_estimation2]
|
||||
|
||||
while(inputVideo.grab()) {
|
||||
cv::Mat image, imageCopy;
|
||||
inputVideo.retrieve(image);
|
||||
|
||||
double tick = (double)getTickCount();
|
||||
|
||||
//! [aruco_pose_estimation3]
|
||||
vector<int> ids;
|
||||
vector<vector<Point2f> > corners, rejected;
|
||||
|
||||
// detect markers and estimate pose
|
||||
detector.detectMarkers(image, corners, ids, rejected);
|
||||
|
||||
size_t nMarkers = corners.size();
|
||||
vector<Vec3d> rvecs(nMarkers), tvecs(nMarkers);
|
||||
|
||||
if(estimatePose && !ids.empty()) {
|
||||
// Calculate pose for each marker
|
||||
for (size_t i = 0; i < nMarkers; i++) {
|
||||
solvePnP(objPoints, corners.at(i), camMatrix, distCoeffs, rvecs.at(i), tvecs.at(i));
|
||||
}
|
||||
}
|
||||
//! [aruco_pose_estimation3]
|
||||
double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
|
||||
totalTime += currentTime;
|
||||
totalIterations++;
|
||||
if(totalIterations % 30 == 0) {
|
||||
cout << "Detection Time = " << currentTime * 1000 << " ms "
|
||||
<< "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl;
|
||||
}
|
||||
//! [aruco_draw_pose_estimation]
|
||||
// draw results
|
||||
image.copyTo(imageCopy);
|
||||
if(!ids.empty()) {
|
||||
cv::aruco::drawDetectedMarkers(imageCopy, corners, ids);
|
||||
|
||||
if(estimatePose) {
|
||||
for(unsigned int i = 0; i < ids.size(); i++)
|
||||
cv::drawFrameAxes(imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i], markerLength * 1.5f, 2);
|
||||
}
|
||||
}
|
||||
//! [aruco_draw_pose_estimation]
|
||||
|
||||
if(showRejected && !rejected.empty())
|
||||
cv::aruco::drawDetectedMarkers(imageCopy, rejected, noArray(), Scalar(100, 0, 255));
|
||||
|
||||
imshow("out", imageCopy);
|
||||
char key = (char)waitKey(waitTime);
|
||||
if(key == 27) break;
|
||||
}
|
||||
//! [aruco_detect_markers]
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
%YAML:1.0
|
||||
camera_matrix: !!opencv-matrix
|
||||
rows: 3
|
||||
cols: 3
|
||||
dt: d
|
||||
data: [ 628.158, 0., 324.099,
|
||||
0., 628.156, 260.908,
|
||||
0., 0., 1. ]
|
||||
distortion_coefficients: !!opencv-matrix
|
||||
rows: 5
|
||||
cols: 1
|
||||
dt: d
|
||||
data: [ 0.0995485, -0.206384,
|
||||
0.00754589, 0.00336531, 0 ]
|
||||
@@ -0,0 +1,38 @@
|
||||
%YAML:1.0
|
||||
nmarkers: 35
|
||||
markersize: 6
|
||||
marker_0: "101011111011111001001001101100000000"
|
||||
marker_1: "000000000010011001010011111010111000"
|
||||
marker_2: "011001100000001010000101111101001101"
|
||||
marker_3: "001000111111000111011001110000011111"
|
||||
marker_4: "100110110100101111000000111101110011"
|
||||
marker_5: "010101101110111000111010111100010111"
|
||||
marker_6: "101001000110011110101001010100110100"
|
||||
marker_7: "011010100100110000011101110110100010"
|
||||
marker_8: "111110001000101000110001010010111101"
|
||||
marker_9: "011101101100110111001100100001010100"
|
||||
marker_10: "100001100001010001110001011000000111"
|
||||
marker_11: "110010010010011100101111111000001111"
|
||||
marker_12: "110101001001010110011111010110001101"
|
||||
marker_13: "001111000001000100010001101001010001"
|
||||
marker_14: "000000010010101010111110110011010011"
|
||||
marker_15: "110001110111100101110011111100111010"
|
||||
marker_16: "101011001110001010110011111011001110"
|
||||
marker_17: "101110111101110100101101011001010111"
|
||||
marker_18: "000100111000111101010011010101000101"
|
||||
marker_19: "001110001110001101100101110100000011"
|
||||
marker_20: "100101101100010110110110110001100011"
|
||||
marker_21: "010110001001011010000100111000110110"
|
||||
marker_22: "001000000000100100000000010100010010"
|
||||
marker_23: "101001110010100110000111111010010000"
|
||||
marker_24: "111001101010001100011010010001011100"
|
||||
marker_25: "101000010001010000110100111101101001"
|
||||
marker_26: "101010000001010011001010110110000001"
|
||||
marker_27: "100101001000010101001000111101111110"
|
||||
marker_28: "010010100110010011110001110101011100"
|
||||
marker_29: "011001000101100001101111010001001111"
|
||||
marker_30: "000111011100011110001101111011011001"
|
||||
marker_31: "010100001011000100111101110001101010"
|
||||
marker_32: "100101101001101010111111101101110100"
|
||||
marker_33: "101101001010111000000100110111010101"
|
||||
marker_34: "011111010000111011111110110101100101"
|
||||
Reference in New Issue
Block a user