1- /*
2- By downloading, copying, installing or using the software you agree to this
3- license. If you do not agree to this license, do not download, install,
4- copy or use the software.
5-
6- License Agreement
7- For Open Source Computer Vision Library
8- (3-clause BSD License)
9-
10- Copyright (C) 2013, OpenCV Foundation, all rights reserved.
11- Third party copyrights are property of their respective owners.
12-
13- Redistribution and use in source and binary forms, with or without modification,
14- are permitted provided that the following conditions are met:
15-
16- * Redistributions of source code must retain the above copyright notice,
17- this list of conditions and the following disclaimer.
18-
19- * Redistributions in binary form must reproduce the above copyright notice,
20- this list of conditions and the following disclaimer in the documentation
21- and/or other materials provided with the distribution.
22-
23- * Neither the names of the copyright holders nor the names of the contributors
24- may be used to endorse or promote products derived from this software
25- without specific prior written permission.
26-
27- This software is provided by the copyright holders and contributors "as is" and
28- any express or implied warranties, including, but not limited to, the implied
29- warranties of merchantability and fitness for a particular purpose are
30- disclaimed. In no event shall copyright holders or contributors be liable for
31- any direct, indirect, incidental, special, exemplary, or consequential damages
32- (including, but not limited to, procurement of substitute goods or services;
33- loss of use, data, or profits; or business interruption) however caused
34- and on any theory of liability, whether in contract, strict liability,
35- or tort (including negligence or otherwise) arising in any way out of
36- the use of this software, even if advised of the possibility of such damage.
37- */
38-
39-
401#include < opencv2/highgui.hpp>
412#include < opencv2/aruco/charuco.hpp>
423#include < vector>
434#include < iostream>
5+ #include < opencv2/objdetect/aruco_detector.hpp>
6+ #include < opencv2/objdetect/charuco_detector.hpp>
447#include " aruco_samples_utility.hpp"
458
469using namespace std ;
@@ -87,20 +50,20 @@ int main(int argc, char *argv[]) {
8750 bool autoScale = parser.has (" as" );
8851 float autoScaleFactor = autoScale ? parser.get <float >(" as" ) : 1 .f ;
8952
90- Ptr< aruco::DetectorParameters> detectorParams = makePtr<aruco::DetectorParameters>() ;
53+ aruco::DetectorParameters detectorParams;
9154 if (parser.has (" dp" )) {
9255 FileStorage fs (parser.get <string>(" dp" ), FileStorage::READ);
93- bool readOk = detectorParams-> readDetectorParameters (fs.root ());
56+ bool readOk = detectorParams. readDetectorParameters (fs.root ());
9457 if (!readOk) {
9558 cerr << " Invalid detector parameters file" << endl;
9659 return 0 ;
9760 }
9861 }
9962 if (parser.has (" refine" )) {
10063 // override cornerRefinementMethod read from config file
101- detectorParams-> cornerRefinementMethod = parser.get <aruco::CornerRefineMethod>(" refine" );
64+ detectorParams. cornerRefinementMethod = parser.get <aruco::CornerRefineMethod>(" refine" );
10265 }
103- std::cout << " Corner refinement method (0: None, 1: Subpixel, 2:contour, 3: AprilTag 2): " << (int )detectorParams-> cornerRefinementMethod << std::endl;
66+ std::cout << " Corner refinement method (0: None, 1: Subpixel, 2:contour, 3: AprilTag 2): " << (int )detectorParams. cornerRefinementMethod << std::endl;
10467
10568 int camId = parser.get <int >(" ci" );
10669 String video;
@@ -154,43 +117,55 @@ int main(int argc, char *argv[]) {
154117 double totalTime = 0 ;
155118 int totalIterations = 0 ;
156119
120+ aruco::CharucoBoard charucoBoard (Size (3 , 3 ), squareLength, markerLength, dictionary);
121+ aruco::CharucoDetector detector (charucoBoard, aruco::CharucoParameters (), detectorParams);
122+
157123 while (inputVideo.grab ()) {
158124 Mat image, imageCopy;
159125 inputVideo.retrieve (image);
160126
161127 double tick = (double )getTickCount ();
162128
163- vector< int > markerIds;
164- vector< Vec4i > diamondIds;
165- vector< vector< Point2f > > markerCorners, rejectedMarkers, diamondCorners;
166- vector< Vec3d > rvecs, tvecs;
129+ // ! [detect_diamonds]
130+ vector<int > markerIds;
131+ vector<Vec4i> diamondIds;
132+ vector<vector<Point2f> > markerCorners, rejectedMarkers, diamondCorners;
133+ vector<Vec3d> rvecs, tvecs;
167134
168- // detect markers
169- aruco::detectMarkers (image, makePtr<aruco::Dictionary>(dictionary), markerCorners, markerIds, detectorParams,
170- rejectedMarkers);
171-
172- // detect diamonds
173- if (markerIds.size () > 0 )
174- aruco::detectCharucoDiamond (image, markerCorners, markerIds,
175- squareLength / markerLength, diamondCorners, diamondIds,
176- camMatrix, distCoeffs);
135+ detector.detectDiamonds (image, diamondCorners, diamondIds, markerCorners, markerIds);
136+ // ! [detect_diamonds]
177137
138+ // ! [diamond_pose_estimation]
178139 // estimate diamond pose
179- if (estimatePose && diamondIds.size () > 0 ) {
140+ size_t N = diamondIds.size ();
141+ if (estimatePose && N > 0 ) {
142+ cv::Mat objPoints (4 , 1 , CV_32FC3);
143+ rvecs.resize (N);
144+ tvecs.resize (N);
180145 if (!autoScale) {
181- aruco::estimatePoseSingleMarkers (diamondCorners, squareLength, camMatrix,
182- distCoeffs, rvecs, tvecs);
183- } else {
146+ // set coordinate system
147+ objPoints.ptr <Vec3f>(0 )[0 ] = Vec3f (-squareLength/2 .f , squareLength/2 .f , 0 );
148+ objPoints.ptr <Vec3f>(0 )[1 ] = Vec3f (squareLength/2 .f , squareLength/2 .f , 0 );
149+ objPoints.ptr <Vec3f>(0 )[2 ] = Vec3f (squareLength/2 .f , -squareLength/2 .f , 0 );
150+ objPoints.ptr <Vec3f>(0 )[3 ] = Vec3f (-squareLength/2 .f , -squareLength/2 .f , 0 );
151+ // Calculate pose for each marker
152+ for (size_t i = 0ull ; i < N; i++)
153+ solvePnP (objPoints, diamondCorners.at (i), camMatrix, distCoeffs, rvecs.at (i), tvecs.at (i));
154+ }
155+ // ! [diamond_pose_estimation]
156+ else {
184157 // if autoscale, extract square size from last diamond id
185- for (unsigned int i = 0 ; i < diamondCorners. size () ; i++) {
186- float autoSquareLength = autoScaleFactor * float (diamondIds[i].val [3 ]);
187- vector< vector< Point2f > > currentCorners;
188- vector< Vec3d > currentRvec, currentTvec;
158+ for (size_t i = 0 ; i < N ; i++) {
159+ float sqLenScale = autoScaleFactor * float (diamondIds[i].val [3 ]);
160+ vector<vector<Point2f> > currentCorners;
161+ vector<Vec3d> currentRvec, currentTvec;
189162 currentCorners.push_back (diamondCorners[i]);
190- aruco::estimatePoseSingleMarkers (currentCorners, autoSquareLength, camMatrix,
191- distCoeffs, currentRvec, currentTvec);
192- rvecs.push_back (currentRvec[0 ]);
193- tvecs.push_back (currentTvec[0 ]);
163+ // set coordinate system
164+ objPoints.ptr <Vec3f>(0 )[0 ] = Vec3f (-sqLenScale/2 .f , sqLenScale/2 .f , 0 );
165+ objPoints.ptr <Vec3f>(0 )[1 ] = Vec3f (sqLenScale/2 .f , sqLenScale/2 .f , 0 );
166+ objPoints.ptr <Vec3f>(0 )[2 ] = Vec3f (sqLenScale/2 .f , -sqLenScale/2 .f , 0 );
167+ objPoints.ptr <Vec3f>(0 )[3 ] = Vec3f (-sqLenScale/2 .f , -sqLenScale/2 .f , 0 );
168+ solvePnP (objPoints, diamondCorners.at (i), camMatrix, distCoeffs, rvecs.at (i), tvecs.at (i));
194169 }
195170 }
196171 }
@@ -214,20 +189,22 @@ int main(int argc, char *argv[]) {
214189 if (showRejected && rejectedMarkers.size () > 0 )
215190 aruco::drawDetectedMarkers (imageCopy, rejectedMarkers, noArray (), Scalar (100 , 0 , 255 ));
216191
192+ // ! [draw_diamonds]
217193 if (diamondIds.size () > 0 ) {
218194 aruco::drawDetectedDiamonds (imageCopy, diamondCorners, diamondIds);
195+ // ! [draw_diamonds]
219196
197+ // ! [draw_diamond_pose_estimation]
220198 if (estimatePose) {
221- for (unsigned int i = 0 ; i < diamondIds.size (); i++)
222- cv::drawFrameAxes (imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i],
223- squareLength * 1 .1f );
199+ for (size_t i = 0u ; i < diamondIds.size (); i++)
200+ cv::drawFrameAxes (imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i], squareLength*1.1 );
224201 }
202+ // ! [draw_diamond_pose_estimation]
225203 }
226-
204+ imwrite ( " detecteddiamonds.jpg " , imageCopy);
227205 imshow (" out" , imageCopy);
228206 char key = (char )waitKey (waitTime);
229207 if (key == 27 ) break ;
230208 }
231-
232209 return 0 ;
233210}
0 commit comments