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
-
40
1
#include < opencv2/highgui.hpp>
41
2
#include < opencv2/aruco/charuco.hpp>
42
3
#include < vector>
43
4
#include < iostream>
5
+ #include < opencv2/objdetect/aruco_detector.hpp>
6
+ #include < opencv2/objdetect/charuco_detector.hpp>
44
7
#include " aruco_samples_utility.hpp"
45
8
46
9
using namespace std ;
@@ -87,20 +50,20 @@ int main(int argc, char *argv[]) {
87
50
bool autoScale = parser.has (" as" );
88
51
float autoScaleFactor = autoScale ? parser.get <float >(" as" ) : 1 .f ;
89
52
90
- Ptr < aruco::DetectorParameters> detectorParams = makePtr<aruco::DetectorParameters>() ;
53
+ aruco::DetectorParameters detectorParams;
91
54
if (parser.has (" dp" )) {
92
55
FileStorage fs (parser.get <string>(" dp" ), FileStorage::READ);
93
- bool readOk = detectorParams-> readDetectorParameters (fs.root ());
56
+ bool readOk = detectorParams. readDetectorParameters (fs.root ());
94
57
if (!readOk) {
95
58
cerr << " Invalid detector parameters file" << endl;
96
59
return 0 ;
97
60
}
98
61
}
99
62
if (parser.has (" refine" )) {
100
63
// override cornerRefinementMethod read from config file
101
- detectorParams-> cornerRefinementMethod = parser.get <aruco::CornerRefineMethod>(" refine" );
64
+ detectorParams. cornerRefinementMethod = parser.get <aruco::CornerRefineMethod>(" refine" );
102
65
}
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;
104
67
105
68
int camId = parser.get <int >(" ci" );
106
69
String video;
@@ -154,43 +117,65 @@ int main(int argc, char *argv[]) {
154
117
double totalTime = 0 ;
155
118
int totalIterations = 0 ;
156
119
120
+ aruco::CharucoBoard charucoBoard (Size (3 , 3 ), squareLength, markerLength, dictionary);
121
+ aruco::CharucoDetector detector (charucoBoard, aruco::CharucoParameters (), detectorParams);
122
+
157
123
while (inputVideo.grab ()) {
158
124
Mat image, imageCopy;
159
125
inputVideo.retrieve (image);
160
126
161
127
double tick = (double )getTickCount ();
162
128
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;
167
134
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]
177
137
138
+ // ! [diamond_pose_estimation]
178
139
// 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);
180
145
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
+ // ! [diamond_pose_estimation]
155
+ /* //! [diamond_pose_estimation_as_charuco]
156
+ for (size_t i = 0ull; i < N; i++) { // estimate diamond pose as Charuco board
157
+ Mat objPoints_b, imgPoints;
158
+ // The coordinate system of the diamond is placed in the board plane centered in the bottom left corner
159
+ vector<int> charucoIds = {0, 1, 3, 2}; // if CCW order, Z axis pointing in the plane
160
+ // vector<int> charucoIds = {0, 2, 3, 1}; // if CW order, Z axis pointing out the plane
161
+ charucoBoard.matchImagePoints(diamondCorners[i], charucoIds, objPoints_b, imgPoints);
162
+ solvePnP(objPoints_b, imgPoints, camMatrix, distCoeffs, rvecs[i], tvecs[i]);
163
+ }
164
+ //! [diamond_pose_estimation_as_charuco] */
165
+ }
166
+ else {
184
167
// 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;
168
+ for (size_t i = 0 ; i < N ; i++) {
169
+ float sqLenScale = autoScaleFactor * float (diamondIds[i].val [3 ]);
170
+ vector<vector<Point2f> > currentCorners;
171
+ vector<Vec3d> currentRvec, currentTvec;
189
172
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 ]);
173
+ // set coordinate system
174
+ objPoints.ptr <Vec3f>(0 )[0 ] = Vec3f (-sqLenScale/2 .f , sqLenScale/2 .f , 0 );
175
+ objPoints.ptr <Vec3f>(0 )[1 ] = Vec3f (sqLenScale/2 .f , sqLenScale/2 .f , 0 );
176
+ objPoints.ptr <Vec3f>(0 )[2 ] = Vec3f (sqLenScale/2 .f , -sqLenScale/2 .f , 0 );
177
+ objPoints.ptr <Vec3f>(0 )[3 ] = Vec3f (-sqLenScale/2 .f , -sqLenScale/2 .f , 0 );
178
+ solvePnP (objPoints, diamondCorners.at (i), camMatrix, distCoeffs, rvecs.at (i), tvecs.at (i));
194
179
}
195
180
}
196
181
}
@@ -214,20 +199,21 @@ int main(int argc, char *argv[]) {
214
199
if (showRejected && rejectedMarkers.size () > 0 )
215
200
aruco::drawDetectedMarkers (imageCopy, rejectedMarkers, noArray (), Scalar (100 , 0 , 255 ));
216
201
202
+ // ! [draw_diamonds]
217
203
if (diamondIds.size () > 0 ) {
218
204
aruco::drawDetectedDiamonds (imageCopy, diamondCorners, diamondIds);
205
+ // ! [draw_diamonds]
219
206
207
+ // ! [draw_diamond_pose_estimation]
220
208
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 );
209
+ for (size_t i = 0u ; i < diamondIds.size (); i++)
210
+ cv::drawFrameAxes (imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i], squareLength*1 .1f );
224
211
}
212
+ // ! [draw_diamond_pose_estimation]
225
213
}
226
-
227
214
imshow (" out" , imageCopy);
228
215
char key = (char )waitKey (waitTime);
229
216
if (key == 27 ) break ;
230
217
}
231
-
232
218
return 0 ;
233
219
}
0 commit comments