-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_pong.cpp
More file actions
139 lines (111 loc) · 3.53 KB
/
main_pong.cpp
File metadata and controls
139 lines (111 loc) · 3.53 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
/**
* @file main.cpp
* @brief Main file for motion-controlled games.
*/
#include <math.h>
#include <unistd.h>
#include "cv.h"
#include "highgui.h"
#include "uchar_array.c"
#include "calibration.c"
#include "threshold.c"
#include "detection.c"
#include "pong.c"
int min(int i1, int i2) {
return i1 > i2 ? i2 : i1;
}
int max(int i1, int i2) {
return i1 <= i2 ? i2 : i1;
}
void generate_movement_frame(IplImage *debug_frame, const IplImage *prev_frame, const IplImage *frame);
int main(int argc, char **argv) {
CvCapture *capture = 0;
IplImage *frame = 0;
IplImage *prev_frame = 0;
IplImage *result = 0;
IplImage *arm = 0;
CvScalar red = cvScalar(255, 0, 0);
capture = cvCaptureFromCAM(0);
hands_t *hands = init_hands();
bool is_down = false;
if (!capture) {
perror("Error when reading stream");
exit(EXIT_FAILURE);
}
calibration_t *c = (calibration_t *) malloc(sizeof(calibration_t));
calibrate(capture, c);
cvNamedWindow("Arm Detection", 1);
cvNamedWindow("BW Matte", 1);
object_list_t *objects = init_game();
int is_alive = 1;
clock_t last_frame = clock();
while (cvWaitKey(10) != 'q') {
frame = cvQueryFrame(capture);
if (frame) {
if (prev_frame) {
cvReleaseImage(&prev_frame);
prev_frame = cvCloneImage(frame);
cvCvtColor(frame, frame, CV_BGR2HSV);
cvReleaseImage(&arm);
arm = get_arm(frame, c);
cv::Mat image1 = cv::cvarrToMat(arm, false);
cv::medianBlur(image1, image1, 11);
arm = cvCreateImage(cvSize(image1.cols, image1.rows), 8, arm->nChannels);
IplImage ipltemp = image1;
cvCopy(&ipltemp, arm);
cvCvtColor(frame, frame, CV_HSV2BGR);
detect_hands(arm, hands);
int half = frame->height / 2;
char c = 0;
c = getch();
if (is_alive && clock() - last_frame >= 10 * 1000) {
last_frame = clock();
render_game(objects, max(0, min((hands->right_y - 100) / 2 , HEIGHT - 20)), max(0, min((hands->left_y - 100) / 2, HEIGHT - 20)));
}
if (c == 'r' || c == 'R') {
is_alive = 1;
free_object_list(objects);
objects = init_game();
}
if (game_end(objects)) {
is_alive = 0;
}
cvCircle(frame, cvPoint(hands->left_x, hands->left_y), 10, red, 15);
cvCircle(frame, cvPoint(hands->right_x, hands->right_y), 10, red, 15);
cvShowImage("BW Matte", arm);
} else {
arm = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
}
cvReleaseImage(&prev_frame);
prev_frame = cvCloneImage(frame);
cvFlip(frame, frame, 1);
cvShowImage("Arm Detection", frame);
}
}
sleep(5);
endwin();
printf("\nYou died!\n");
for_all(objects, print_object);
cvDestroyWindow("Arm Detection");
cvDestroyWindow("BW Matte");
cvReleaseCapture(&capture);
cvReleaseImage(&frame);
cvReleaseImage(&prev_frame);
cvReleaseImage(&result);
cvReleaseImage(&arm);
free_object_list(objects);
free(hands);
free(c);
return EXIT_SUCCESS;
}
void generate_movement_frame(IplImage *debug_frame, const IplImage *prev_frame, const IplImage *frame) {
for (int y = 0; y < debug_frame->height; y++) {
for (int x = 0; x < debug_frame->width * debug_frame->nChannels; x++) {
if (prev_frame->imageData[y * frame->widthStep + x] - frame->imageData[y * frame->widthStep + x] != 0) {
debug_frame->imageData[y * debug_frame->widthStep + x] = 255;
} else {
debug_frame->imageData[y * debug_frame->widthStep + x] = 0;
}
}
}
}