-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (96 loc) · 4.27 KB
/
Copy pathmain.cpp
File metadata and controls
141 lines (96 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <opencv2/aruco.hpp>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace aruco;
void drawCube(Mat frame, vector <Point2f> corners, const Vec3d& rvec, const Vec3d& tvec,
const Mat& camMatrix, const Mat& distCoeffs);
static bool readDetectorParameters(const string& filename, aruco::DetectorParameters ¶ms) {
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;
}
int main() {
DetectorParameters params;
readDetectorParameters("../detector_params.yml", params);
Dictionary dict = getPredefinedDictionary(PREDEFINED_DICTIONARY_NAME(10));
double c[] = {1567.7598663928327, 0, 959.50000000000000, 0, 1569.8260146860121, 539.50000000000000, 0, 0,1};
Mat camMatrix = {
Size(3, 3),
CV_64F,
c
};
double d[] = {1.7746202293461785e-2, -6.7204665235718858e-2, 0, 0, 3.5079887214058908e-1};
Mat distCoeffs = {Size(5, 1),
CV_64F,
d};
vector<int> ids;
vector<vector<Point2f>> corners, rejected;
vector<Vec3d> rvecs, tvecs;
const Ptr<Dictionary> dic = &dict;
const Ptr<DetectorParameters>par = ¶ms;
VideoCapture cap("../1.mp4");
VideoWriter output("../result.avi", VideoWriter::fourcc('M','J','P','G'), 30, Size(1280, 720));
while (true){
Mat frame;
cap >> frame;
if(frame.empty()){
break;
}
// imshow("Frame", frame);
detectMarkers(frame, dic, corners, ids, par, rejected, camMatrix, distCoeffs);
if(!ids.empty()){
estimatePoseSingleMarkers(corners, 1, camMatrix, distCoeffs, rvecs, tvecs);
for(auto i = 0; i < ids.size(); i++){
drawCube(frame, corners[i], rvecs[i], tvecs[i], camMatrix, distCoeffs);
}
}
resize(frame, frame, Size(1280, 720));
imshow("Frame", frame);
output.write(frame);
waitKey(10);
}
}
void drawCube(Mat frame, vector <Point2f> corners, const Vec3d& rvec, const Vec3d& tvec,
const Mat& camMatrix, const Mat& distCoeffs){
vector<Point3d> source = {
{-0.5, 0.5, 1},
{0.5, 0.5, 1},
{0.5, -0.5, 1},
{-0.5, -0.5, 1},
};
vector <Point2d> upperCorners;
projectPoints(source, rvec, tvec, camMatrix, distCoeffs, upperCorners);
for (auto i:{0, 1, 2, 3}) {
line(frame, upperCorners[i], corners[i] , Scalar(255, 12, 12), 3);
}
for (auto i: {0, 1, 2}){
line(frame, upperCorners[i], upperCorners[i + 1], Scalar(0, 0, 255), 3);
line(frame, corners[i], corners[i + 1], Scalar(0, 156, 0), 3);
}
line(frame, upperCorners[3], upperCorners[0], Scalar(0, 0, 255), 3);
line(frame, corners[3], corners[0], Scalar(0, 156, 0), 3);
}