#include<cv.h>
#include<iostream>
#include<highgui.h>
#include<stdio.h>
#define alpha 1
#define x1 0
#define y1 0
using namespace std;
int main()
{
CvCapture *capture;
IplImage* pImg = NULL;
IplImage* pImg2 = NULL;
IplImage* grey = NULL;
IplImage* pImgcr = NULL;
IplImage* logoImg = NULL;
IplImage* logoImg1 = NULL;
IplImage* logoImg2 = NULL;
IplImage* logoImg3 = NULL;
IplImage* logoImg4 = NULL;
int score=0;
int scorex;
int scorey;
//cout<<"1";
while(true){
capture=cvCreateCameraCapture( 0 );
pImg=cvQueryFrame(capture);
logoImg=cvLoadImage("006.jpg");
logoImg1=cvLoadImage("002.jpg");
logoImg2=cvLoadImage("004.jpg");
logoImg3=cvLoadImage("008.jpg");
logoImg4=cvLoadImage("exit.jpg");
float mouseX=0;
float mouseY=0;
float redPixelNumber=0;
float redPixelX=0;
float redPixelY=0;
grey = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U, 1);
pImgcr = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U, 3);
cvCvtColor(pImg, pImgcr, CV_BGR2YCrCb);
cvInRangeS(pImgcr, cvScalar(0,137,77), cvScalar(256,177,127),grey);
for (int y=0; y<grey->height; y++) {
uchar* ptr1=(uchar*) (grey->imageData +y*grey->widthStep);
//uchar* ptr1=(uchar*) (pImg->imageData +y*pImg->widthStep);
for (int x=0; x<grey->width; x++) {
if(ptr1[x]==255){
redPixelNumber++;
redPixelX+=x;
redPixelY+=y;
}
}
if (redPixelNumber>0) {
mouseX=redPixelX/redPixelNumber;
mouseY=redPixelY/redPixelNumber;
}
}
int mousex1=mouseX-60;
int mousey1=mouseY-60;
int scorex=mouseX;
int scorey=mouseY;
if( (logoImg->height + mousey1) < pImg->height) {
for (int y=0; y<logoImg->height; y++) {
uchar* ptr1=(uchar*) (logoImg->imageData +y*logoImg->widthStep);
uchar* ptr2=(uchar*) (pImg->imageData +((mousey1+y))*pImg->widthStep);
for (int x=0; x<logoImg->width; x++) {
if(ptr1[3*(x)]<255&& ptr1[3*(x)+1]<255&& ptr1[3*(x)+2]<255){
ptr2[3*(x+mousex1)]=ptr1[3*(x)];
ptr2[3*(x+mousex1)+1]=ptr1[3*(x)+1];
ptr2[3*(x+mousex1)+2]=ptr1[3*(x)+2];
}
}
}
}
if(scorex <pImg->width/3&&scorey <pImg->height/2){
score+=10;
cout<<"伊凡萬科死了"<<" "<<"得分"<<score<<endl;
cvWaitKey(100);
//Sleep(100);
}
if(scorex >pImg->width/3&&scorex <(pImg->width/3)*2&&scorey<pImg->height/2){
score+=30;
cout<<"滿大人死了"<<" "<<"得分"<<score<<endl;
cvWaitKey(100);
//Sleep(100);
}
if(scorex>(pImg->width/3)*2&&scorey <pImg->height/2){
score+=50;
cout<<"火焰人死了"<<" "<<"得分"<<score<<endl;
cvWaitKey(100);
//Sleep(100);
}
//---------------------------------------------------------------------------------
if( (logoImg1->height + y1) < pImg->height) {
for (int y=0; y<logoImg1->height; y++) {
uchar* ptr1=(uchar*) (logoImg1->imageData +y*logoImg1->widthStep);
uchar* ptr2=(uchar*) (pImg->imageData+150 +(75+y)*pImg->widthStep);
for (int x=0; x<logoImg1->width; x++) {
if(ptr1[3*(x)]<250&& ptr1[3*(x)+1]<250&& ptr1[3*(x)+2]<250){
ptr2[3*(x+x1)]=ptr1[3*(x+x1)];
ptr2[3*(x+x1)+1]=ptr1[3*(x+x1)+1];
ptr2[3*(x+x1)+2]=ptr1[3*(x+x1)+2];
}
}
}
}
//---------------------------------------------------------------------------------
if( (logoImg2->height + y1) < pImg->height) {
for (int y=0; y<logoImg2->height; y++) {
uchar* ptr1=(uchar*) (logoImg2->imageData +y*logoImg2->widthStep);
uchar* ptr2=(uchar*) (pImg->imageData +810+(y+75)*pImg->widthStep);
for (int x=0;x<logoImg2->width; x++) {
if(ptr1[3*(x)]<250&& ptr1[3*(x)+1]<250&& ptr1[3*(x)+2]<250){
ptr2[3*(x+x1)]=ptr1[3*(x+x1)];
ptr2[3*(x+x1)+1]=ptr1[3*(x+x1)+1];
ptr2[3*(x+x1)+2]=ptr1[3*(x+x1)+2];
}
}
}
}
//---------------------------------------------------------------------------------
if( (logoImg3->height + y1) < pImg->height) {
for (int y=0; y<logoImg3->height; y++) {
uchar* ptr1=(uchar*) (logoImg3->imageData +y*logoImg3->widthStep);
uchar* ptr2=(uchar*) (pImg->imageData +1377+(y+75)*pImg->widthStep);
for (int x=0;x<logoImg3->width; x++) {
if(ptr1[3*(x)]<250&& ptr1[3*(x)+1]<250&& ptr1[3*(x)+2]<250){
ptr2[3*(x+x1)]=ptr1[3*(x+x1)];
ptr2[3*(x+x1)+1]=ptr1[3*(x+x1)+1];
ptr2[3*(x+x1)+2]=ptr1[3*(x+x1)+2];
}
}
}
}
//---------------------------------------------------------------------------------
if( (logoImg4->height + y1) < pImg->height) {
for (int y=0; y<logoImg4->height; y++) {
uchar* ptr1=(uchar*) (logoImg4->imageData +y*logoImg4->widthStep);
uchar* ptr2=(uchar*) (pImg->imageData +1620+(y+380)*pImg->widthStep);
for (int x=0;x<logoImg4->width; x++) {
if(ptr1[3*(x)]<250&& ptr1[3*(x)+1]<250&& ptr1[3*(x)+2]<250){
ptr2[3*(x)]=ptr1[3*(x+x1)];
ptr2[3*(x)+1]=ptr1[3*(x+x1)+1];
ptr2[3*(x)+2]=ptr1[3*(x+x1)+2];
}
}
}
}
//---------------------------------------------------------------------------------
// mousex1
// mousey1
//cvLine(pImg,cvPoint(mouseX-10,mouseY),cvPoint(mouseX+10,mouseY),CV_RGB(255,0,0),12,8,0);
//cvLine(pImg,cvPoint(mouseX,mouseY-10),cvPoint(mouseX,mouseY+10),CV_RGB(255,0,0),12,8,0);
cvLine(pImg,cvPoint(pImg->width/3,0),cvPoint(pImg->width/3,pImg->height),CV_RGB(255,0,0),12,8,0);
cvLine(pImg,cvPoint((pImg->width/3)*2,0),cvPoint((pImg->width/3)*2,pImg->height),CV_RGB(255,0,0),12,8,0);
cvShowImage("webcam",pImg);
char key = cvWaitKey(33);
// if(key == 27)break;
//if(key == 27)break;
if(scorex >(pImg->width/5)*4&&scorey>(pImg->height/5)*4)break;
}
}
沒有留言:
張貼留言