c++ - Unable to detect ArUco markers with OpenCV 3.1.0 -
i trying code simple c++ routine first write predefined dictionary of aruco markers (e.g. 4x4_100) folder , detect aruco markers in specific image selected folder using opencv 3.1 , visual studio 2017. have compiled opencv-contrib libraries required use aruco markers. routine builds without error, having trouble detecting markers after supplying correct arguments (e.g. image, dictionary, etc.) in-built "aruco::detectmarkers" function. please me understand what`s wrong approach? below minimal working example , test image attached here "4x4marker_40.jpg":
#include "opencv2\core.hpp" #include "opencv2\imgproc.hpp" #include "opencv2\imgcodecs.hpp" #include "opencv2\aruco.hpp" #include "opencv2\highgui.hpp" #include <sstream> #include <fstream> #include <iostream> using namespace cv; using namespace std; // function write aruco markers void createarucomarkers() { // define variable store output markers mat outputmarker; // choose predefined dictionary of markers ptr< aruco::dictionary> markerdictionary = aruco::getpredefineddictionary(aruco::predefined_dictionary_name::dict_4x4_50); // write each of markers '.jpg' image file (int = 0; < 50; i++) { aruco::drawmarker(markerdictionary, i, 500, outputmarker, 1); ostringstream convert; string imagename = "4x4marker_"; convert << imagename << << ".jpg"; imwrite(convert.str(), outputmarker); } } // main body of routine int main(int argv, char** argc) { createarucomarkers(); // read specific image mat frame = imread("4x4marker_40.jpg", cv_load_image_unchanged); // define variables store output of marker detection vector<int> markerids; vector<vector<point2f>> markercorners, rejectedcandidates; // define dictionary type variable marker detection ptr<aruco::dictionary> markerdictionary = aruco::getpredefineddictionary(aruco::predefined_dictionary_name::dict_4x4_50); // detect markers aruco::detectmarkers(frame, markerdictionary, markercorners, markerids); // display image namedwindow("webcam", cv_window_autosize); imshow("webcam", frame); // draw detected markers on displayed image aruco::drawdetectedmarkers(frame, markercorners, markerids); cout << "\nmarker id is:\t"<<markerids.size(); waitkey(); }
there few problems in code:
- you displaying image
imshowbefore callingdrawdetectedmarkersyou'll never see detected marker. - you displaying size of
markeridsvector instead of value contained within it. - (this main problem) marker has no white space around it's impossible detect.
one suggestion: use forward slashes, not backslashes in #include statements. forward slashes work everywhere, backslashes work on windows.
this worked on machine. note loaded image color image make easier see results of drawdetectedmarkers.
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/aruco.hpp> #include <opencv2/highgui.hpp> #include <sstream> #include <fstream> #include <iostream> using namespace cv; using namespace std; // function write aruco markers void createarucomarkers() { // create image hold marker plus surrounding white space mat outputimage(700, 700, cv_8uc1); // fill image white outputimage = scalar(255); // define roi write marker rect markerrect(100, 100, 500, 500); mat outputmarker(outputimage, markerrect); // choose predefined dictionary of markers ptr< aruco::dictionary> markerdictionary = aruco::getpredefineddictionary(aruco::predefined_dictionary_name::dict_4x4_50); // write each of markers '.jpg' image file (int = 0; < 50; i++) { //draw marker roi aruco::drawmarker(markerdictionary, i, 500, outputmarker, 1); ostringstream convert; string imagename = "4x4marker_"; convert << imagename << << ".jpg"; // note writing outputimage, not outputmarker imwrite(convert.str(), outputimage); } } // main body of routine int main(int argv, char** argc) { createarucomarkers(); // read specific image mat frame = imread("4x4marker_40.jpg", cv_load_image_color); // define variables store output of marker detection vector<int> markerids; vector<vector<point2f>> markercorners, rejectedcandidates; // define dictionary type variable marker detection ptr<aruco::dictionary> markerdictionary = aruco::getpredefineddictionary(aruco::predefined_dictionary_name::dict_4x4_50); // detect markers aruco::detectmarkers(frame, markerdictionary, markercorners, markerids); // display image namedwindow("webcam", cv_window_autosize); // draw detected markers on displayed image aruco::drawdetectedmarkers(frame, markercorners, markerids); // show image detected marker imshow("webcam", frame); // if marker identified, show id if (markerids.size() > 0) { cout << "\nmarker id is:\t" << markerids[0] << endl; } waitkey(0); }
Comments
Post a Comment