-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.cpp
488 lines (407 loc) · 18.7 KB
/
project.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
#define TH1 10
#define alpha 0.7
float refBlue=0;
float refGreen=0;
float refRed=0;
float refCounter=0;
float refBlue2=0;
float refGreen2=0;
float refRed2=0;
float refCounter2=0;
int refX=0;
int refY=0;
int xyCounter=0;
int refX2=0;
int refY2=0;
int xyCounter2=0;
CvPoint CircleCenter;
int Radius;
CvScalar Color;
int Thickness;
int Shift;
int preprocessTime=60;
double Scale;
int FontFace;
double HorizontalScale;
double VerticalScale;
double Shear;
int LineType;
CvPoint TextPosition1,TextPosition2,TextPosition3;
int main()
{
float x1;
float y1;
float x2;
float y2;
CvCapture *capture;
IplImage *frame;
IplImage *frame3;
IplImage *frame6;
IplImage* logoImg = NULL;
IplImage *src = 0;
IplImage* logoImg2 = NULL;
CvSize logoImg_cvsize;
float scale = 0.1;
uchar* ptr3;
uchar* ptr1;
uchar* ptr4;
int ballX;
int ballY;
int ballVX;
int ballVY;
int point;
int point2;
int midCircleCenterX;
int midCircleCenterY;
int rightCircleCenterX;
int rightCircleCenterY;
int leftCircleCenterX;
int leftCircleCenterY;
bool isTouch=false;
bool isTouch2=false;
bool isWin;
int state=0;
CvPoint FromPoint1,ToPoint1;
CvPoint FromPoint2,ToPoint2;
CvScalar Color;
CvSize dst_cvsize; //ヘ夹紇钩へ
logoImg2=cvLoadImage("wm2.jpg");
src=cvLoadImage("wm.jpg");
logoImg_cvsize.width= src->width * scale;
logoImg_cvsize.height = src->height * scale;
logoImg = cvCreateImage( logoImg_cvsize, src->depth, src->nChannels); //承ミヘ夹紇钩
cvResize(src, logoImg, CV_INTER_LINEAR); //罽ㄓ方紇钩ヘ夹紇钩
//cvSaveImage("0o.jpg", logoImg);//纗紇钩
cvFlip(logoImg, 0, 0);
while(true){
if(state==0){
int c2=30;
capture=cvCreateCameraCapture( 0 );
//cvNamedWindow("Webcam",1);
cvNamedWindow("Webcam3",1);
frame = cvQueryFrame(capture);
CvSize ImageSize1 = cvSize(1000,700);
frame3 = cvCreateImage(ImageSize1,IPL_DEPTH_8U,3);
srand (time(0));
ballX=rand() % 800+100;
ballY=rand() % 600+50;
ballVX=rand() % 10 -5;
(ballVX==0)?ballVX++:ballVX=ballVX;
ballVY=rand() % 10 -5;
point=0;
point2=0;
midCircleCenterX=frame3->width/2;
midCircleCenterY=frame3->height/2;
rightCircleCenterX=frame3->width;
rightCircleCenterY=frame3->height/2;
leftCircleCenterX=0;
leftCircleCenterY=frame3->height/2;
isTouch=false;
isTouch2=false;
isWin=false;
x1=0;
y1=0;
x2=0;
y2=0;
while (c2 > 0) {
frame = cvQueryFrame(capture);
frame6=cvCloneImage(frame);
cvWaitKey(20);
c2--;
//cvShowImage("Webcam", frame);
cvShowImage("Webcam3", frame6);
}
preprocessTime=60;
while (preprocessTime > 0) {
frame = cvQueryFrame(capture);
cvFlip(frame, 0, 1);
frame6=cvCloneImage(frame);
cvRectangle(frame6, cvPoint(200,200), cvPoint(238,240), cvScalar(0,0,255), 2);
for (int y=205; y<235; y++) {
uchar* ptr2=(uchar*) (frame6->imageData +y*frame6->widthStep);
//x=570 y=460
for (int x=205; x<235; x++) {
refBlue+=ptr2[3*x];
refGreen+=ptr2[3*x+1];
refRed+=ptr2[3*x+2];
refCounter++;
}
}
cvRectangle(frame6, cvPoint(400,200), cvPoint(438,240), cvScalar(0,0,255), 2);
for (int y=205; y<235; y++) {
uchar* ptr2=(uchar*) (frame6->imageData +y*frame6->widthStep);
for (int x=405; x<435; x++) {
refBlue2+=ptr2[3*x];
refGreen2+=ptr2[3*x+1];
refRed2+=ptr2[3*x+2];
refCounter2++;
}
}
cvWaitKey(100);
preprocessTime--;
//cvShowImage("Webcam", frame);
cvShowImage("Webcam3", frame6);
}
cvDestroyWindow("Webcam3");
state=1;
refBlue=refBlue/refCounter;
refGreen=refGreen/refCounter;
refRed=refRed/refCounter;
refBlue2=refBlue2/refCounter;
refGreen2=refGreen2/refCounter;
refRed2=refRed2/refCounter;
// printf(" refBlue = %.2f \n refGreen = %.2f \n refRed = %.2f\n", refBlue,refGreen, refRed);
//printf(" refBlue2 = %.2f \n refGreen2 = %.2f \n refRed2 = %.2f\n", refBlue2,refGreen2, refRed2);
//printf("%d %d",frame->width,frame->height);
//ystem("pause");
}
if(state==1){
while(1)
{
frame = cvQueryFrame(capture);
cvFlip(frame, 0, 1);
for (int y=0; y<frame3->height; y++) {
ptr3=(uchar*) (frame3->imageData +y*frame3->widthStep);
for (int x=0; x<frame3->width; x++) {
ptr3[3*x]=0;
ptr3[3*x+1]=0;
ptr3[3*x+2]=0;
}
}
cvRectangle(frame3, cvPoint(4,4), cvPoint(frame3->width-4,frame3->height-4), cvScalar(255,255,255), 5);
FromPoint1 = cvPoint(frame3->width/2,0);
ToPoint1 = cvPoint(frame3->width/2,frame3->height);
Color=CV_RGB(255,255,255);
Thickness=5;
Shift=0;
cvLine(frame3,FromPoint1,ToPoint1,Color,Thickness,CV_AA,Shift);
CircleCenter=cvPoint(midCircleCenterX,midCircleCenterY);
Radius=100;
cvCircle(frame3,CircleCenter,Radius,Color,Thickness,CV_AA,Shift);
CircleCenter=cvPoint(rightCircleCenterX,rightCircleCenterY);
cvCircle(frame3,CircleCenter,Radius,Color,Thickness,CV_AA,Shift);
CircleCenter=cvPoint(leftCircleCenterX,leftCircleCenterY);
cvCircle(frame3,CircleCenter,Radius,Color,Thickness,CV_AA,Shift);
//draw the ice ball
ballX+=ballVX;
ballY+=ballVY;
Color=CV_RGB(255,255,0);
CircleCenter=cvPoint(ballX,ballY);
cvCircle(frame3,CircleCenter,20,Color,8,CV_AA,Shift);
if(ballX-10<0){
ballVX*=-1;
ballX=10;
//cout << ballX << "," << ballY <<"," <<frame3->width<<","<<ballVX <<endl;
//system("pause");
}
if(ballX+10>frame3->width){
ballVX*=-1;
ballX=frame3->width-10;
}
if(ballY-10<0){
ballVY*=-1;
ballY=10;
}
if(ballY+10>frame3->height){
ballVY*=-1;
ballY=frame3->height-10;
}
// touch the circle
int rtCirDistance=(int)sqrt((rightCircleCenterX-ballX)*(rightCircleCenterX-ballX)+(rightCircleCenterY-ballY)*(rightCircleCenterY-ballY));
if(rtCirDistance-20<Radius&&isTouch==false){
point++;
isTouch=true;
}
if(rtCirDistance-20>Radius&&isTouch==true){
isTouch=false;
}
int lfCirDistance=(int)sqrt((leftCircleCenterX-ballX)*(leftCircleCenterX-ballX)+(leftCircleCenterY-ballY)*(leftCircleCenterY-ballY));
if(lfCirDistance-20<Radius&&isTouch2==false){
point2++;
isTouch2=true;
}
if(lfCirDistance-20>Radius&&isTouch2==true){
isTouch2=false;
}
//text area
Scale=3;
Thickness=3;
CvFont Font1=cvFont(Scale,Thickness);
Color=CV_RGB(155,255,255);
FontFace=CV_FONT_HERSHEY_SIMPLEX;
HorizontalScale=1;
VerticalScale=1;
Shear=2;
LineType=CV_AA;
TextPosition2=cvPoint(25,50);
TextPosition3=cvPoint(frame3->width-150,50);
cvInitFont(&Font1,FontFace,HorizontalScale,VerticalScale,Shear,Thickness,LineType);
char myBuf[60]={'P','o','i','n','t',':'};
sprintf(myBuf+6*sizeof(char),"%d",point);
cvPutText(frame3,myBuf,TextPosition2,&Font1,Color);
sprintf(myBuf+6*sizeof(char),"%d",point2);
cvPutText(frame3,myBuf,TextPosition3,&Font1,Color);
refX=0;
refY=0;
refX2=0;
refY2=0;
xyCounter=0;
xyCounter2=0;
for (int y=0; y<frame->height; y++) {
ptr1=(uchar*) (frame->imageData +y*frame->widthStep);
for (int x=0; x<frame->width; x++) {
float D= (ptr1[3*x] - refBlue) * (ptr1[3*x] - refBlue) +
(ptr1[3*x+1] - refGreen) * (ptr1[3*x+1] -refGreen) +
(ptr1[3*x+2] - refRed) * (ptr1[3*x+2] - refRed);
if (sqrt(D) < TH1) {
xyCounter++;
refX+=x;
refY+=y;
}
}
}
for (int y=0; y<frame->height; y++) {
ptr1=(uchar*) (frame->imageData +y*frame->widthStep);
for (int x=0; x<frame->width; x++) {
float D2= (ptr1[3*x] - refBlue2) * (ptr1[3*x] - refBlue2) +
(ptr1[3*x+1] - refGreen2) * (ptr1[3*x+1] -refGreen2) +
(ptr1[3*x+2] - refRed2) * (ptr1[3*x+2] - refRed2);
if (sqrt(D2) < TH1) {
xyCounter2++;
refX2+=x;
refY2+=y;
}
}
}
if (xyCounter > 0) {
refX = refX/xyCounter;
refY = refY/xyCounter;
if(x1+50>ballX-20&&x1-50<ballX+20&&y1+50>ballY-20&&y1-50<ballY+20){
ballVX=refX/10;
ballVY=refY/10;
}
}
else {
refX=-1;
refY=-1;
}
if (xyCounter2 > 0) {
refX2 = refX2/xyCounter2;
refY2 = refY2/xyCounter2;
if(x2+50>ballX-20&&x2-50<ballX+20&&y2+50>ballY-10&&y2-50<ballY+10){
ballVX=refX2/10;
ballVY=refY2/10;
}
}
else {
refX2=-1;
refY2=-1;
}
// Start to move a Logo using a refColor
if (refX >=0 && refY >=0) {
x1=refX;
x1*=1.2;
y1=refY*1.2;
if(x1>frame3->width/2-100)
x1=frame3->width/2-100;
if(y1>frame3->height-10)
y1=frame3->height-10;
}
if (refX2 >=0 && refY2 >=0) {
x2=refX2;
x2=x2*1.2+500;
y2=refY2*1.2;
if(x2>frame3->width-100)
x2=frame3->width-100;
if(y2>frame3->height-10)
y2=frame3->height-10;
}
//printf(" * %f %f %d %d %d %d \n", x2, y1,refX, refY,refX2, refY2);
if( (logoImg->height + y1) > frame3->height)
y1 = frame3->height - logoImg->height - 1;
for (int y=0; y<logoImg->height; y++) {
ptr1=(uchar*) (logoImg->imageData +y*logoImg->widthStep);
ptr3=(uchar*) (frame3->imageData +(y+(int)y1)*frame3->widthStep);
for (int x=0; x<logoImg->width; x++) {
ptr3[3*(x+(int)x1)]=alpha*ptr1[3*(x)] + (1-alpha)*ptr3[3*(x+(int)x1)];
ptr3[3*(x+(int)x1)+1]=alpha*ptr1[3*(x)+1] + (1-alpha)*ptr3[3*(x+(int)x1)+1];
ptr3[3*(x+(int)x1)+2]=alpha*ptr1[3*(x)+2] + (1-alpha)*ptr3[3*(x+(int)x1)+2];
}
}
if( (logoImg2->height + y2) > frame3->height)
y2 = frame3->height - logoImg2->height - 1;
for (int y=0; y<logoImg2->height; y++) {
ptr1=(uchar*) (logoImg2->imageData +y*logoImg2->widthStep);
ptr3=(uchar*) (frame3->imageData +(y+(int)y2)*frame3->widthStep);
for (int x=0; x<logoImg2->width; x++) {
ptr3[3*(x+(int)x2)]=alpha*ptr1[3*(x)] + (1-alpha)*ptr3[3*(x+(int)x2)];
ptr3[3*(x+(int)x2)+1]=alpha*ptr1[3*(x)+1] + (1-alpha)*ptr3[3*(x+(int)x2)+1];
ptr3[3*(x+(int)x2)+2]=alpha*ptr1[3*(x)+2] + (1-alpha)*ptr3[3*(x+(int)x2)+2];
}
}
// End to move a Logo using a refColor
cvShowImage("Webcam", frame);
//isBreak
cvShowImage("Webcam3", frame3);
cvWaitKey(20);
//point2=10;
if(point>=10||point2>=10){
state=2;
isWin=true;
break;
}
}
}
if(state==2){
//cout << point <<" "<< point2 <<endl;
if(point>=10&&isWin){
Scale=100;
Thickness=3;
CvFont Font1=cvFont(Scale,Thickness);
Color=CV_RGB(255,0,0);
FontFace=CV_FONT_HERSHEY_SIMPLEX;
HorizontalScale=3;
VerticalScale=3;
Shear=2;
LineType=CV_AA;
TextPosition2=cvPoint(frame3->width/2-200,frame3->height/2);
cvInitFont(&Font1,FontFace,HorizontalScale,VerticalScale,Shear,Thickness,LineType);
cvPutText(frame3,"Left Win",TextPosition2,&Font1,Color);
cvShowImage("Webcam3", frame3);
cvWaitKey (15);
}
else if(point2>=10&&isWin){
Scale=100;
Thickness=3;
CvFont Font1=cvFont(Scale,Thickness);
Color=CV_RGB(0,255,0);
FontFace=CV_FONT_HERSHEY_SIMPLEX;
HorizontalScale=4;
VerticalScale=4;
Shear=2;
LineType=CV_AA;
TextPosition2=cvPoint(frame3->width/2-250,frame3->height/2);
cvInitFont(&Font1,FontFace,HorizontalScale,VerticalScale,Shear,Thickness,LineType);
cvPutText(frame3,"Right Win",TextPosition2,&Font1,Color);
cvShowImage("Webcam3", frame3);
cvWaitKey (15);
}
isWin=false;
if(cvWaitKey (15)==13)
state=0;
else
state=2;
}
cvWaitKey(20);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Webcam3");
}