-
Notifications
You must be signed in to change notification settings - Fork 0
/
augmentedReality.cpp
365 lines (321 loc) · 15.9 KB
/
augmentedReality.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// augmentedReality.cpp
// Project4
// These codes are about calibrating camera to obtain intrinsic parameters, projecting objects into the scene
// and overlaying images and videos onto ArUco markers.
// Created by Thean Cheat Lim on 3/16/23.
//
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include "helper.hpp"
using namespace cv;
using namespace std;
int MIN_CALIBRATION_CNT = 5;
char CAM_DISTORTION_YML [] = "cameraDistortion.yml"; // file to store camera intrinsic params
int main(int argc, char *argv[]) {
VideoCapture *capdev;
// open the video device
capdev = new VideoCapture(0);
if( !capdev->isOpened() ) {
printf("Unable to open video device\n");
return(-1);
}
// get some properties of the image
Size refS((int) capdev->get(CAP_PROP_FRAME_WIDTH ),
(int) capdev->get(CAP_PROP_FRAME_HEIGHT));
int fps = capdev->get(CAP_PROP_FPS);
printf("Expected size: %d %d\n", refS.width, refS.height);
cout << "Frames per second :" << fps;
namedWindow("Video", 1); // identifies a window
Mat frame;
char persistKey = 'n';
// Read in Camera Matrix and Distortion Coeff if exists
Mat cameraMatrix, distortionCoefficients;
FileStorage camDistortionYml(CAM_DISTORTION_YML, FileStorage::READ);
if(camDistortionYml.isOpened()){
camDistortionYml["camera_matrix"] >> cameraMatrix;
camDistortionYml["distortion_coefficients"] >> distortionCoefficients;
}
camDistortionYml.release();
// Define the number of inner corners of the chessboard
int boardWidth = 9;
int boardHeight = 6;
Size boardSize(boardWidth, boardHeight);
// Task 2: Select Calibration Images
vector<Point2f> calibrationCorners;
Mat calibrationFrame;
vector<vector<Vec3f>> pointList;
vector<vector<Point2f>> cornerList;
// Fixed Point set
vector<Vec3f> chessboardFixedPointSet;
for (int j = 0; j < boardHeight; j++){
for (int i = 0; i<boardWidth; i++){
Vec3f temp(i, -j, 0);
chessboardFixedPointSet.push_back(temp);
}
}
// Extension Assets -- videos and images
vector<Mat> images;
vector<VideoCapture> videos;
bool readImagesVideos = false;
Mat vidFrame;
vector<int>frameCounters;
// Loop forever to read in video frame
for(;;) {
*capdev >> frame; // get a new frame from the camera, treat as a stream
if(frame.empty() ) {
printf("frame is empty\n");
break;
}
if(persistKey =='d'){
// Task 1: Detect and extract Chessboard corners
// Create an empty vector of Point2f to store the detected corners
vector<Point2f> corners;
// Find the chessboard corners
bool found = findChessboardCorners(frame, boardSize, corners);
if (found){
// Improve the detected corners' accuracy
Mat grayScaleFrame;
cvtColor(frame, grayScaleFrame, COLOR_BGR2GRAY);
int winSize = 11; // Default
cornerSubPix(grayScaleFrame, corners, Size(winSize, winSize), Size(-1, -1),
TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.0001));
// Draw the detected corners on the image
drawChessboardCorners(frame, boardSize, corners, found);
// Print out the number of detected corners and the coordinates of the first corner
printf("Number of corners detected: %lu\n", corners.size());
printf("Coordinates of the first corner: (%f, %f)\n", corners[0].x, corners[0].y);
// Make a copy
calibrationCorners = corners;
frame.copyTo(calibrationFrame);
} else {
printf("No chessboard is found\n");
}
}
if(persistKey =='p'){
// Task 4: Calculate Current Position of the Camera
if (cameraMatrix.empty()) printf("Press d and then s to calibrate the camera first.");
else {
vector<Point2f> corners;
bool found = findChessboardCorners(frame, boardSize, corners);
if (found){
Mat rvec, tvec;
cornerSubPixSolvePnP(frame, rvec, tvec, chessboardFixedPointSet, corners, cameraMatrix, distortionCoefficients);
// Print rotation and translation data
cout << "rotation vector: " << rvec << endl;
cout << "translation vector: " << tvec << endl;
}
}
}
if(persistKey =='x'){
// Task 5: Project Outside Corners or 3D Axes
if (cameraMatrix.empty()) printf("Press d and then s to calibrate the camera first.");
else {
vector<Point2f> corners;
bool found = findChessboardCorners(frame, boardSize, corners);
if (found){
Mat rvec, tvec;
cornerSubPixSolvePnP(frame, rvec, tvec, chessboardFixedPointSet, corners, cameraMatrix, distortionCoefficients);
vector<Point3f> objectPoints;
objectPoints.push_back(Point3f(0, 0, 0)); // origin
objectPoints.push_back(Point3f(1, 0, 0)); // x
objectPoints.push_back(Point3f(0, -1, 0)); // y
objectPoints.push_back(Point3f(0, 0, 1)); // z
vector<Point2f> imagePoints;
projectPoints(objectPoints, rvec, tvec, cameraMatrix, distortionCoefficients, imagePoints);
// Draw the axis on the image
arrowedLine(frame, imagePoints[0], imagePoints[1], Scalar(0, 0, 255), 2);
arrowedLine(frame, imagePoints[0], imagePoints[2], Scalar(0, 255, 0), 2);
arrowedLine(frame, imagePoints[0], imagePoints[3], Scalar(255, 0, 0), 2);
}
}
}
if(persistKey =='o'){
// Task 6: Create a Virtual Object
if (cameraMatrix.empty()) printf("Press d and then s to calibrate the camera first.");
else {
vector<Point2f> corners;
bool found = findChessboardCorners(frame, boardSize, corners);
if (found){
Mat rvec, tvec;
cornerSubPixSolvePnP(frame, rvec, tvec, chessboardFixedPointSet, corners, cameraMatrix, distortionCoefficients);
vector<Point3f> objectPoints;
vector<Point2f> imagePoints;
constructPyramidRoof(objectPoints);
projectPoints(objectPoints, rvec, tvec, cameraMatrix, distortionCoefficients, imagePoints);
drawPyramidRoof(frame, imagePoints);
}
}
}
if(persistKey =='t'){
// Extension: - Torus on chessboard
if (cameraMatrix.empty()) printf("Press d and then s to calibrate the camera first.");
else {
vector<Point2f> corners;
bool found = findChessboardCorners(frame, boardSize, corners);
if (found){
Mat rvec, tvec;
cornerSubPixSolvePnP(frame, rvec, tvec, chessboardFixedPointSet, corners, cameraMatrix, distortionCoefficients);
vector<Point3f> objectPoints;
vector<Point2f> imagePoints;
vector<int> indices;
vector<Point> polygon;
constructTorus(objectPoints, indices, polygon);
projectPoints(objectPoints, rvec, tvec, cameraMatrix, distortionCoefficients, imagePoints);
drawTorus(frame, imagePoints, indices, polygon);
}
}
}
if(persistKey =='h'){ // Harris corner
// Task 7: Detect Robust Features - Harris Corner
/*https:docs.opencv.org/3.4/d4/d7d/tutorial_harris_detector.html*/
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY);
Mat corners, cornersScaled;
int blockSize = 3;
int apertureSize = 3;
double k = 0.06;
double threshold = 128;
cornerHarris(gray, corners, blockSize, apertureSize, k);
normalize(corners, corners, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
// Draw circles at the corner
Scalar color = Scalar(114,128,250);
int radius = 5;
int thickness = 5;
for( int i = 0; i < corners.rows; i++){
float *uptr = corners.ptr<float>(i);
for( int j = 0; j < corners.cols; j++){
if((int)uptr[j]>threshold){
circle(frame, Point(j,i), radius, color, thickness);
}
}
}
}
if(persistKey =='f'){ // FAST algorithm
// Task 7 and Extension1: Detect Robust Features - Harris Corner
/*https:blog.francium.tech/feature-detection-and-matching-with-opencv-5fd2394a590*/
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY);
// Create FAST feature detector and compute keypoints
Ptr<FeatureDetector> detector = FastFeatureDetector::create(50);
vector<KeyPoint> keypoints;
detector->detect(gray, keypoints);
// Draw keypoints on the image
Scalar color = Scalar(114,128,250);
drawKeypoints(frame, keypoints, frame, color);
}
if(persistKey =='v' or persistKey =='a'){
// Extension 3: ArUco + display video/images
/* https:docs.opencv.org/4.7.0/d5/dae/tutorial_aruco_detection.html*/
/* https:www.youtube.com/watch?v=wB4BRWNuJM4&list=PLJ958Ls6nowUnzTXcdBBmO96NG5AWTq_N&index=4*/
if (not readImagesVideos) {
readImagesVideosFromDir(images, videos, frameCounters);
readImagesVideos = true;
}
vector<int> markerIds;
vector<vector<Point2f>> markerCorners, rejectedCandidates;
aruco::DetectorParameters detectorParams = aruco::DetectorParameters();
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
aruco::ArucoDetector detector(dictionary, detectorParams);
detector.detectMarkers(frame, markerCorners, markerIds, rejectedCandidates);
if (persistKey =='v' and markerIds.size() == 0){
// Reset video frame
frameCounters.clear();
for (int i = 0; i<videos.size(); i++){
videos[i].set(CAP_PROP_POS_FRAMES, 0);
frameCounters.push_back(0);
}
} else {
for (int i = 0; i<markerIds.size(); i++){
Mat curImg;
int index=0;
if (persistKey =='v') {
if (markerIds[i]>videos.size()){
cout<<markerIds[i]<<" "<<videos.size()<<endl;
continue; //ignore this box ->no overlay
}
index = markerIds[i]-1;
VideoCapture vid = videos[index];
if (frameCounters[i]==vid.get(CAP_PROP_FRAME_COUNT)){
// Reset video frame
vid.set(CAP_PROP_POS_FRAMES, 0);
frameCounters[i] = 0;
}
vid.read(curImg);
} else if (persistKey=='a'){
index = markerIds[i]%images.size();
curImg = images[index];
}
if (curImg.rows==0) continue;
int rows = curImg.rows;
int cols = curImg.cols;
vector<Point2f> srcPoints;
srcPoints.push_back(Point2f(0, 0));
srcPoints.push_back(Point2f(cols, 0));
srcPoints.push_back(Point2f(cols, rows));
srcPoints.push_back(Point2f(0, rows));
Mat H = findHomography(srcPoints, markerCorners.at(i));
Mat warpedImage;
warpPerspective(curImg, warpedImage, H, frame.size());
Mat mask = Mat::zeros(frame.rows, frame.cols, CV_8UC1);
vector<Point2f> thisCorner2F = markerCorners.at(i);
vector<Point> thisCornerCasted;
for (size_t i = 0; i < thisCorner2F.size(); i++) {
thisCornerCasted.push_back(Point((int)thisCorner2F[i].x, (int)thisCorner2F[i].y));
}
fillConvexPoly(mask, thisCornerCasted, 255);
bitwise_and(warpedImage,warpedImage,frame, mask = mask);
if (persistKey=='v')frameCounters[index]++;
}
}
}
imshow("Video", frame);
// see if there is a waiting keystroke
char key = waitKey(10);
if( key == 'q') break;
if(key =='s'){
if (calibrationCorners.empty()) printf("No chessboard corners were detected. Press 'd' first.\n");
else {
// Task 2: Select Calibration Images
// Save corners and real word point sets
cornerList.push_back(calibrationCorners);
pointList.push_back(chessboardFixedPointSet);
// Save the calibration image
string calibrationFrameFn = "calibration" + std::to_string(cornerList.size()) + ".png";
imwrite(calibrationFrameFn, calibrationFrame);
if (cornerList.size()>=MIN_CALIBRATION_CNT){
// Task 3: Calibrate the Camera
// Initialize camera_matrix
cameraMatrix = Mat::eye(3, 3, CV_64F);
cameraMatrix.at<double>(0, 2) = frame.cols/2.0;
cameraMatrix.at<double>(1, 2) = frame.rows/2.0;
// distortion coefficient matrix. Initialize with zero
distortionCoefficients = Mat::zeros(8, 1, CV_64F);
vector<Mat> rvecs, tvecs;
printf("Camera Matrix and Distortion coefficients BEFORE calibration: \n");
cout << cameraMatrix << endl;
cout << distortionCoefficients << endl;
double reprojection_error = calibrateCamera(pointList, cornerList, frame.size(), cameraMatrix, distortionCoefficients, rvecs, tvecs, CALIB_FIX_ASPECT_RATIO);
printf("Camera Matrix and Distortion coefficients AFTER calibration: \n");
cout << cameraMatrix << endl;
cout << distortionCoefficients << endl;
printf("Reprojection Error: %f\n", reprojection_error);
// Save Camera Matrix and Distortion coefficients
FileStorage cameraDistortion(CAM_DISTORTION_YML, FileStorage::WRITE);
cameraDistortion << "camera_matrix" << cameraMatrix;
cameraDistortion << "distortion_coefficients" << distortionCoefficients;
cameraDistortion.release();
}
}
// back to previous key
key = persistKey;
}
// Persist key
if (strchr("nsqdpaofhxvt", key)){ /*https:stackoverflow.com/a/19548575/19481647*/
persistKey = key;
}
}
delete capdev;
return(0);
}