int main() { CvCapture* capture =0; capture = cvCaptureFromCAM(0); if(!capture) { printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black cvNamedWindow("Video"); cvNamedWindow("Ball"); //iterate through each frames of the video while(true) { frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel //track the possition of the ball trackObject(imgThresh); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); cvShowImage("Ball", imgThresh); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgThresh); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; } cvDestroyAllWindows(); cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main( int argc, char** argv ) { CvSeq* comp = 0; CvRect window, eye; int key, nc, found; int stage = STAGE_INIT; key=0; startpos_x = 0; startpos_y = 0; search_window_x=0,search_window_y=0; /* Initialize Capture from webcam * Here '0' in cvCaptureCAM indicates the Index of the camera to be used. */ capture = cvCaptureFromCAM(0); if (!capture) exit_nicely("Webcam Not found!"); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 300); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 250); // Grabs and returns a frame from the camera. frame = cvQueryFrame(capture); if (!frame) exit_nicely("Cannot query frame!"); // Create a window named 'Video' with window size Normal. cvNamedWindow("video",CV_WINDOW_NORMAL); /* * Creates Windows Handler for keeping the frame window * always on top of other windows. */ HWND win_handle = FindWindow(0, "video"); if (!win_handle) { printf("Failed FindWindow\n"); } SetWindowPos(win_handle, HWND_TOPMOST, 0, 0, 0, 0, 1); ShowWindow(win_handle, SW_SHOW); // Create a callback to mousehandler when mouse is clicked on frame. cvSetMouseCallback( "video", mouseHandler,NULL ); // cvCreateImage is used to create & allocate image data nose_template = cvCreateImage( cvSize( NOSE_TPL_WIDTH, NOSE_TPL_HEIGHT ),frame->depth, frame->nChannels ); nose_template_match = cvCreateImage( cvSize( BOUNDARY_WINDOW_WIDTH - NOSE_TPL_WIDTH + 1, BOUNDARY_WINDOW_HEIGHT - NOSE_TPL_HEIGHT + 1 ),IPL_DEPTH_32F, 1 ); // Initialize Memory for storing the frames storage = cvCreateMemStorage(0); if (!storage) exit_nicely("Cannot allocate memory storage!"); /* Allocates and Fills the structure IplConvKernel , which can be used as a structuring element in the morphological operations */ kernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL); gray = cvCreateImage(cvGetSize(frame), 8, 1); prev = cvCreateImage(cvGetSize(frame), 8, 1); diff = cvCreateImage(cvGetSize(frame), 8, 1); eye_template = cvCreateImage(cvSize(EYE_TPL_WIDTH, EYE_TPL_HEIGHT), 8, 1); // Show if any error occurs during allocation if (!kernel || !gray || !prev || !diff || !eye_template) exit_nicely("System error."); gray->origin = frame->origin; prev->origin = frame->origin; diff->origin = frame->origin; // Loop until ESC(27) key is pressed while( key != 27 ) { // Get a frame from CAM frame = cvQueryFrame( capture ); /* Always check if frame exists */ if( !frame ) break; // Flip the frame vertically cvFlip( frame, frame, 1 ); frame->origin = 0; // Eye blink detection & tracking if (stage == STAGE_INIT) window = cvRect(0, 0, frame->width, frame->height); // Convert original image to thresholded(grayscale) image for efficient detection*/ cvCvtColor(frame, gray, CV_BGR2GRAY); // Find connected components in the image nc = get_connected_components(gray, prev, window, &comp); // Check if eyes are detected and start tracking by setting Region of Interest(ROI) if (stage == STAGE_INIT && is_eye_pair(comp, nc, &eye)) { cvSetImageROI(gray, eye); cvCopy(gray, eye_template, NULL); cvResetImageROI(gray); // Start tracking eyes for blink stage = STAGE_TRACKING; } // Here the tracking will start & will check for eye blink if (stage == STAGE_TRACKING) { // Try to locate the eye found = locate_eye(gray, eye_template, &window, &eye); // If eye is not found here or 'r' is pressed, restart the eye detection module if (!found || key == 'r') stage = STAGE_INIT; DRAW_RECTS(frame, diff, window, eye); // Check if there was an eye blink if (is_blink(comp, nc, window, eye)) { //DRAW_RECTS(frame, diff, window, eye); printf("Eye Blinked!"); // Perform mouse left button click on blink mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0,0,0,0); } } prev = (IplImage*)cvClone(gray); /* Perform nose tracking if template is available Here tracking will start & continues till selected templated is within specified threshold limit */ if( is_tracking ) trackObject(); // Display the frame in window cvShowImage( "video", frame ); // Check for a key press key = cvWaitKey( 10 ); } // Exit without any error exit_nicely(NULL); }
int main(){ CvCapture* capture =0; CvSize dim; FILE *fp; fp = fopen("debug.txt","r"); if(fp) { fscanf(fp, "%d", &debug); fclose(fp); } capture = cvCaptureFromCAM(0); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 160); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 120); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video dim = cvGetSize(frame); imgTracking=cvCreateImage(dim,IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black halfWidth = dim.width/2; halfHeight = dim.height/2; if(debug>0) { cvNamedWindow("Video"); cvNamedWindow("Ball"); } //iterate through each frames of the video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel //track the possition of the ball trackObject(imgThresh); if(debug>1) { // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); } if(debug>0) { cvShowImage("Ball", imgThresh); cvShowImage("Video", frame); } //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgThresh); cvReleaseImage(&frame); //Wait X mS int c = cvWaitKey(delay); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; } cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
void ishara::processFrameAndUpdateGUI() { int pos_x1 = 0; int pos_y1 = 0; int pos_x2 = 0; int pos_y2 = 0; hMin1 = ui->sliderHMin1->value(); hMax1 = ui->sliderHMax1->value(); sMin1 = ui->sliderSMin1->value(); sMax1 = ui->sliderSMax1->value(); vMin1 = ui->sliderVMin1->value(); vMax1 = ui->sliderVMax1->value(); hMin2 = ui->sliderHMin2->value(); hMax2 = ui->sliderHMax2->value(); sMin2 = ui->sliderSMin2->value(); sMax2 = ui->sliderSMax2->value(); vMin2 = ui->sliderVMin2->value(); vMax2 = ui->sliderVMax2->value(); smoothFac = ui->sliderSmoothFac->value(); pinchR = ui->sliderPinchR->value(); rightClickDealy = ui->sliderRCRC->value(); capture.read(src); /* * flip the initial image and convert to RGB as opencv would read images in BGR format */ cv::flip(src, frame, 1); cv::cvtColor(frame, frame, CV_BGR2RGB); /* * converting the image to HSV */ cv::cvtColor(frame, imgHSV, CV_BGR2HSV); /* * identifying the color markers used in the index finger */ cv::inRange(imgHSV, cv::Scalar(hMin1, sMin1, vMin1), cv::Scalar(hMax1, sMax1, vMax1), imgThresh1); openingOperation(&imgThresh1); marker1 = trackObject(&imgThresh1, &pos_x1, &pos_y1); /* * identifying the color markers used in the index thumb */ cv::inRange(imgHSV, cv::Scalar(hMin2, sMin2, vMin2), cv::Scalar(hMax2, sMax2, vMax2), imgThresh2); openingOperation(&imgThresh2); marker2 = trackObject(&imgThresh2, &pos_x2, &pos_y2); mcorInit_X = pos_x1; mcorInit_Y = pos_y1; if(marker1 && startEmulation) { Display *display = XOpenDisplay(0); if(!display) { ui->statusBar->showMessage("Error opening display!"); return; } Window root = DefaultRootWindow(display); if(!root) { ui->statusBar->showMessage("Root window not found!"); return; } /* * map and move mouse pointer on screen */ mouseMap(); if(motionEnable == 0) { pre_x = msPoint_X; pre_y = msPoint_Y; motionEnable = 1; } else { dx = msPoint_X - pre_x; dy = msPoint_Y - pre_y; mcorFinal_X += dx * (xScreenWidth/frame.cols); mcorFinal_Y += dy * (xScreenHeight/frame.rows); if(mcorFinal_X > xScreenWidth) mcorFinal_X = xScreenWidth; if(mcorFinal_Y > xScreenHeight) mcorFinal_Y = xScreenHeight; pre_x = msPoint_X; pre_y = msPoint_Y; } XTestFakeMotionEvent(display, DefaultScreen(display), mcorFinal_X, mcorFinal_Y, 0); if(marker2) { if(tmpX == mcorFinal_X && tmpY == mcorFinal_Y) { ++waitCountRC; } else { tmpX = 0; tmpY = 0; waitCountRC = 0; } /* * right click */ if(ui->chkEnableRightClick->isChecked() == true) { if(waitCountRC == rightClickDealy && (ifScrollUp != 1 && ifScrollDwn != 1) && pinch != 1) { XTestFakeButtonEvent(display, 3, 1, 1); XTestFakeButtonEvent(display, 3, 0, 1); tmpX = 0; tmpY = 0; waitCountRC = 0; } } tmpX = mcorFinal_X; tmpY = mcorFinal_Y; preClick(&pos_x2, &pos_y2); /* * scroll */ if(ui->chkEnableScroll->isChecked() == true) { scrollInit(&pos_x2, &pos_y2); if(ifScrollUp == 1) { XTestFakeButtonEvent(display, 4, 1, 10); XTestFakeButtonEvent(display, 4, 0, 10); } if(ifScrollDwn == 1) { XTestFakeButtonEvent(display, 5, 1, 10); XTestFakeButtonEvent(display, 5, 0, 10); } } /* * left click */ if(ui->chkEnableLeftClick->isChecked() == true) { cv::circle(frame, cv::Point(mcorInit_X, mcorInit_Y), pinchR, cv::Scalar(255, 0, 0), 2); if(pinch == 1) { if(btnPress == 1) { XTestFakeButtonEvent(display, 1, 1, 1); btnPress = 0; btnRel = 1; } } else{ if(btnRel == 1) { XTestFakeButtonEvent(display, 1, 0, 1); btnRel = 0; } else btnPress = 1; } } } XFlush(display); XCloseDisplay(display); } else { motionEnable = 0; } /* * display images */ QImage qImgDisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); ui->lblDisplayClean->setPixmap(imgDisplay.fromImage(qImgDisplay).scaledToHeight(480, Qt::FastTransformation)); QImage qImgDisplayThresh1((uchar*)imgThresh1.data, imgThresh1.cols, imgThresh1.rows, imgThresh1.step, QImage::Format_Indexed8); ui->lblConfigureLeft->setPixmap(imgDisplay.fromImage(qImgDisplayThresh1).scaledToHeight(236, Qt::FastTransformation)); QImage qImgDisplayThresh2((uchar*)imgThresh2.data, imgThresh2.cols, imgThresh2.rows, imgThresh2.step, QImage::Format_Indexed8); ui->lblConfigureRight->setPixmap(imgDisplay.fromImage(qImgDisplayThresh2).scaledToHeight(236, Qt::FastTransformation)); }
void ishara::processFrameAndUpdateGUI() { cv::Mat imgThresh1; cv::Mat imgThresh2; int pos_x1 = 0; int pos_y1 = 0; int pos_x2 = 0; int pos_y2 = 0; hMin1 = ui->sliderHMin1->value(); hMax1 = ui->sliderHMax1->value(); sMin1 = ui->sliderSMin1->value(); sMax1 = ui->sliderSMax1->value(); vMin1 = ui->sliderVMin1->value(); vMax1 = ui->sliderVMax1->value(); hMin2 = ui->sliderHMin2->value(); hMax2 = ui->sliderHMax2->value(); sMin2 = ui->sliderSMin2->value(); sMax2 = ui->sliderSMax2->value(); vMin2 = ui->sliderVMin2->value(); vMax2 = ui->sliderVMax2->value(); smoothFac = ui->sliderSmoothFac->value(); pinchR = ui->sliderPinchR->value(); rightClkCount = ui->sliderRCRC->value(); capture.read(src); /** * Flip the initial image and convert to RGB as opencv would read images in BGR format. */ cv::flip(src, frame, 1); cv::cvtColor(frame, frame, CV_BGR2RGB); /** * Converting the image to HSV and applying Gaussian Blur filter to reduce some noise. */ cv::cvtColor(frame, imgHSV, CV_BGR2HSV); cv::GaussianBlur(imgHSV, imgHSV, cv::Size(11, 11), 0); /** * Identifying the color markers used in the index finger. */ cv::inRange(imgHSV, cv::Scalar(hMin1, sMin1, vMin1), cv::Scalar(hMax1, sMax1, vMax1), imgThresh1); trackObject(&imgThresh1, &pos_x1, &pos_y1); /** * Identifying the color markers used in the index thumb. */ cv::inRange(imgHSV, cv::Scalar(hMin2, sMin2, vMin2), cv::Scalar(hMax2, sMax2, vMax2), imgThresh2); trackObject(&imgThresh2, &pos_x2, &pos_y2); mcorInit_X = pos_x1; mcorInit_Y = pos_y1; if(mcorInit_X + mcorInit_Y) { if(startEmulation == 1) { Display *display = XOpenDisplay(0); if(!display) { ui->statusBar->showMessage("Error opening display!"); return; } Window root = DefaultRootWindow(display); if(!root) { ui->statusBar->showMessage("Root window not found!"); return; } /** * Map and move mouse pointer on screen. */ getxScreenSize(); mouseMap(); XTestFakeMotionEvent(display, DefaultScreen(display), mcorFinal_X, mcorFinal_Y, 0); if(pos_x2 + pos_y2) { if(tmpX == mcorFinal_X && tmpY == mcorFinal_Y) ++waitCount; else { tmpX = 0; tmpY = 0; waitCount = 0; } /** * Right click. */ if(ui->chkEnableRightClick->isChecked() == true) { if(waitCount == rightClkCount && (ifScrollUp != 1 && ifScrollDwn != 1) && pinch != 1) { XTestFakeButtonEvent(display, 3, 1, 1); XTestFakeButtonEvent(display, 3, 0, 1); tmpX = 0; tmpY = 0; waitCount = 0; } } tmpX = mcorFinal_X; tmpY = mcorFinal_Y; preClick(&pos_x2, &pos_y2); /** * Scroll. */ if(ui->chkEnableScroll->isChecked() == true) { scrollInit(&pos_x2, &pos_y2); if(ifScrollUp == 1) { XTestFakeButtonEvent(display, 4, 1, 10); XTestFakeButtonEvent(display, 4, 0, 10); } if(ifScrollDwn == 1) { XTestFakeButtonEvent(display, 5, 1, 10); XTestFakeButtonEvent(display, 5, 0, 10); } } /** * Left click. */ if(ui->chkEnableLeftClick->isChecked() == true) { cv::circle(frame, cv::Point(mcorInit_X, mcorInit_Y), pinchR, cv::Scalar(0, 0, 255)); if(pinch == 1) { if(btnPress == 1) { XTestFakeButtonEvent(display, 1, 1, 1); btnPress = 0; btnRel = 1; } } else{ if(btnRel == 1) { XTestFakeButtonEvent(display, 1, 0, 1); btnRel = 0; } else btnPress = 1; } } } XFlush(display); XCloseDisplay(display); } } /** * Display images. */ QImage qImgDisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); ui->lblDisplayClean->setPixmap(imgDisplay.fromImage(qImgDisplay).scaledToHeight(480, Qt::FastTransformation)); QImage qImgDisplayThresh1((uchar*)imgThresh1.data, imgThresh1.cols, imgThresh1.rows, imgThresh1.step, QImage::Format_Indexed8); ui->lblConfigureLeft->setPixmap(imgDisplay.fromImage(qImgDisplayThresh1).scaledToHeight(236, Qt::FastTransformation)); QImage qImgDisplayThresh2((uchar*)imgThresh2.data, imgThresh2.cols, imgThresh2.rows, imgThresh2.step, QImage::Format_Indexed8); ui->lblConfigureRight->setPixmap(imgDisplay.fromImage(qImgDisplayThresh2).scaledToHeight(236, Qt::FastTransformation)); /** * Clean up the crap. */ imgThresh1.release(); imgThresh2.release(); }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(0,255,0)); float ballX = RENDERWIDTH / 4; float ballY = RENDERHEIGHT/2 ; ball.setPosition(ballX, ballY); //ball properties sf::CircleShape balll(7, 25); balll.setFillColor(sf::Color(255,0,0)); float balllX = RENDERWIDTH*3 / 4; float balllY = RENDERHEIGHT/2; balll.setPosition(balllX, balllY); while(pong.isOpen()) { sf::Event event; while(pong.pollEvent(event)) { if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) pong.close(); } CvCapture* capture =0; capture = cvCaptureFromCAM(0); IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1); cvInRangeS(imgHSV, cvScalar(0, 134, 107), cvScalar(41, 243, 149), imgThresh); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel trackObject(imgThresh, 1); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); IplImage* imgThresh2 =cvCreateImage(cvGetSize(imgHSV2),IPL_DEPTH_8U, 1); cvInRangeS(imgHSV, cvScalar(24, 0, 224), cvScalar(46, 89, 256), imgThresh); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel trackObject(imgThresh2, 2); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); cvDestroyAllWindows() ; cvReleaseCapture(&capture); ball.setPosition((ballX-lastX1)/2, (ballY-lastY1)/2); balll.setPosition((balllX-lastX2)/2, (balllY-lastY2)/2); //render updates pong.clear(); pong.draw(ball); pong.draw(balll); pong.display(); } return 0; }
int main(){ CvCapture* capture =0; capture = cvCaptureFromCAM(0); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //membuat gambar yang kosong dan menjalankan imgTracking yang memiliki ukuran CvWindow yang sama dari yang asli imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //menyembunyikan warna yang tidak terdeteksi menjadi hitam //cvCreateTrackbar("Low Threshold", "Video2", &slideposisirendah, maxrendahtreshold, onLowThresholdSlide); // Create the high threshold slider //cvCreateTrackbar("High Threshold", "Video2", &slideposisitinggi, maxtinggitreshold, onHighThresholdSlide); settingWindows(); cvNamedWindow("Konfigurasi"); cvNamedWindow("Video"); cvNamedWindow("Video2",CV_WINDOW_AUTOSIZE); //mengulangi setiap frame dari video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3);//menghaluskan gambar yg asli menggunakan kernel Gaussian IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //merubah format warna RGB ke Grayscale IplImage* imgThresh = GetThresholdedImage(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3);//menghaluskan gambar biner menggunakan kernel gaussian //membuat posisi jalur objek trackObject(imgThresh); //Menambahkan jalur gambar dan frame cvAdd(frame, imgTracking, frame); cvShowImage("Video", imgThresh); cvShowImage("Video2", frame); cvReleaseImage(&imgHSV); cvReleaseImage(&imgThresh); cvReleaseImage(&frame); int c = cvWaitKey(10); if((char)c==27 ) break; } cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
/* main code */ int main(int argc, char** argv ){ CvCapture *capture; int i, key; //*struct tm *newtime; time_t second,milsecond; char rightfilename[100], leftfilename[100]; // ブーストされた分類器のカスケードを読み込む // cascade = (CvHaarClassifierCascade *) cvLoad (cascade_name, 0, 0, 0); righteye_cascade = (CvHaarClassifierCascade *) cvLoad (righteye_cascade_name, 0, 0, 0); lefteye_cascade = (CvHaarClassifierCascade *) cvLoad (lefteye_cascade_name, 0, 0, 0); /* initialize camera */ capture = cvCaptureFromCAM( 0 ); /* always check */ if( !capture ) return 1; /* get video properties, needed by template image */ frame = cvQueryFrame( capture ); if ( !frame ) return 1; /* create template image */ tpl = cvCreateImage( cvSize( TPL_WIDTH, TPL_HEIGHT ), frame->depth, frame->nChannels ); /* create image for template matching result */ tm = cvCreateImage( cvSize( WINDOW_WIDTH - TPL_WIDTH + 1, WINDOW_HEIGHT - TPL_HEIGHT + 1 ), IPL_DEPTH_32F, 1 ); //eyezone eyezone1 = cvCreateImage(cvSize(50,50), IPL_DEPTH_8U, 1); minieyezone1 = cvCreateImage(cvSize(16,16), IPL_DEPTH_8U, 1); output1 = cvCreateImage(cvSize(512,512), IPL_DEPTH_8U, 1); eyezone2 = cvCreateImage(cvSize(50,50), IPL_DEPTH_8U, 1); minieyezone2 = cvCreateImage(cvSize(16,16), IPL_DEPTH_8U, 1); output2 = cvCreateImage(cvSize(512,512), IPL_DEPTH_8U, 1); /* create a window and install mouse handler */ cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); cvSetMouseCallback( "video", mouseHandler, NULL ); cvNamedWindow("output1", CV_WINDOW_AUTOSIZE); cvNamedWindow("output2", CV_WINDOW_AUTOSIZE); gray = cvCreateImage (cvGetSize (frame), IPL_DEPTH_8U, 1); righteye_storage = cvCreateMemStorage (0); lefteye_storage = cvCreateMemStorage (0); CvPoint righteye_center, lefteye_center; // eye candidate CvRect righteye_cand1, righteye_cand2, lefteye_cand1, lefteye_cand2, right, left; int eye_candidate_num = 0; while( key != 'q' ) { eye_candidate_num = 0; /* get a frame */ frame = cvQueryFrame( capture ); /* always check */ if( !frame ) break; /* 'fix' frame */ /* cvFlip( frame, frame, -1 ); */ frame->origin = 0; /* perform tracking if template is available */ if( is_tracking ) trackObject(); cvClearMemStorage (righteye_storage); cvClearMemStorage (lefteye_storage); cvCvtColor (frame, gray, CV_BGR2GRAY); cvEqualizeHist (gray, gray); righteye = cvHaarDetectObjects (gray, righteye_cascade, righteye_storage, 1.11, 4, 0, cvSize (40, 40), cvSize(40,40)); lefteye = cvHaarDetectObjects (gray, lefteye_cascade, lefteye_storage, 1.11, 4, 0, cvSize (40, 40), cvSize(40,40)); //右目を円で描画 for (i = 0; i < (righteye ? righteye->total : 0); i++) { CvRect *r = (CvRect *) cvGetSeqElem (righteye, i); CvPoint center; int radius; center.x = cvRound (r->x + r->width * 0.5); center.y = cvRound (r->y + r->height * 0.5); radius = cvRound ((r->width + r->height) * 0.25); cvCircle (frame, center, radius, colors[i % 8], 3, 8, 0); //右目候補 if(i == 0){ righteye_cand1 = *r; } if(i == 1){ righteye_cand2 = *r; } } //左目を死角で描画 for (i = 0; i < (lefteye ? lefteye->total : 0); i++) { CvRect *r = (CvRect *) cvGetSeqElem (lefteye, i); CvPoint apex1, apex2; apex1 = cvPoint(r->x, r->y); apex2.x = cvRound(r->x + r->width); apex2.y = cvRound(r->y + r->height); cvRectangle (frame,apex1, apex2, colors[i % 8], 3, 8, 0); //左目候補 if(i == 0){ lefteye_cand1 = *r; } if(i == 1){ lefteye_cand2 = *r; } } //候補しぼり if(righteye->total >= 1){ if(righteye->total >= 2){ if(righteye_cand1.x <= righteye_cand2.x){ right = righteye_cand1; righteye_center.x = cvRound(right.x + right.width*0.5); righteye_center.y = cvRound(right.y + right.height*0.5); } else{ right = righteye_cand2; righteye_center.x = cvRound(right.x + right.width*0.5); righteye_center.y = cvRound(right.y + right.height*0.5); } } else{ right = righteye_cand1; righteye_center.x = cvRound(right.x + right.width*0.5); righteye_center.y = cvRound(right.y + right.height*0.5); } eyezone1 = cvCreateImage(cvSize(right.width, right.height), IPL_DEPTH_8U, 1); cvGetRectSubPix(gray, eyezone1, cvPointTo32f(righteye_center)); cvEqualizeHist(eyezone1, eyezone1); cvResize(eyezone1, minieyezone1, CV_INTER_LINEAR); cvResize(minieyezone1, output1, CV_INTER_NN); } if(lefteye->total >= 1){ if(lefteye->total >= 2){ if(lefteye_cand1.x >= lefteye_cand2.x){ left = lefteye_cand1; lefteye_center.x = cvRound(left.x + left.width*0.5); lefteye_center.y = cvRound(left.y + left.height*0.5); } else{ left = lefteye_cand2; lefteye_center.x = cvRound(left.x + left.width*0.5); lefteye_center.y = cvRound(right.y + left.height*0.5); } } else{ left = lefteye_cand1; lefteye_center.x = cvRound(left.x + left.width*0.5); lefteye_center.y = cvRound(left.y + left.height*0.5); } eyezone2 = cvCreateImage(cvSize(left.width, left.height), IPL_DEPTH_8U, 1); cvGetRectSubPix(gray, eyezone2, cvPointTo32f(lefteye_center)); cvEqualizeHist(eyezone2, eyezone2); cvResize(eyezone2, minieyezone2, CV_INTER_LINEAR); cvResize(minieyezone2, output2, CV_INTER_NN); } printf("righteye width = %d, height = %d\n", right.width, right.height); printf("lefteye width = %d, height = %d\n", left.width, left.height); // printf("righteye x = %d\n", right.x); // printf("lefteye x = %d\n", left.x); /* display frame */ cvShowImage( "video", frame); //cvShowImage( "eyezone1", eyezone1); //cvShowImage( "eyezone2", eyezone2); cvShowImage( "output1", output1); cvShowImage( "output2", output2); //ファイル出力,時間計測 time(&second); milsecond = clock(); // printf("時間[sec] = %ld\n", second); printf("経過時間[usec] = %ld\n", milsecond); //sprintf(filename, "%ld.bmp",second); //printf("sprintf = %s\n", filename); //cvSaveImage(filename, frame,0); if(key == 'n'){ sprintf(rightfilename, "n_right%ld.bmp", milsecond); sprintf(leftfilename, "n_left%ld.bmp", milsecond); printf("fileoutput %s, %s\n", rightfilename, leftfilename); cvSaveImage(rightfilename, minieyezone1, 0); cvSaveImage(leftfilename, minieyezone2, 0); } if(key == 'h'){ sprintf(rightfilename, "h_right%ld.bmp", milsecond); sprintf(leftfilename, "h_left%ld.bmp", milsecond); printf("fileoutput %s, %s\n", rightfilename, leftfilename); cvSaveImage(rightfilename, minieyezone1, 0); cvSaveImage(leftfilename, minieyezone2, 0); } if(key == 'j'){ sprintf(rightfilename, "j_right%ld.bmp", milsecond); sprintf(leftfilename, "j_left%ld.bmp", milsecond); printf("fileoutput %s, %s\n", rightfilename, leftfilename); cvSaveImage(rightfilename, minieyezone1, 0); cvSaveImage(leftfilename, minieyezone2, 0); } if(key == 'k'){ sprintf(rightfilename, "k_right%ld.bmp", milsecond); sprintf(leftfilename, "k_left%ld.bmp", milsecond); printf("fileoutput %s, %s\n", rightfilename, leftfilename); cvSaveImage(rightfilename, minieyezone1, 0); cvSaveImage(leftfilename, minieyezone2, 0); } if(key == 'l'){ sprintf(rightfilename, "l_right%ld.bmp", milsecond); sprintf(leftfilename, "l_left%ld.bmp", milsecond); printf("fileoutput %s, %s\n", rightfilename, leftfilename); cvSaveImage(rightfilename, minieyezone1, 0); cvSaveImage(leftfilename, minieyezone2, 0); } /* exit if user press 'q' */ key = cvWaitKey( 1 ); } /* free memory */ cvDestroyWindow( "video" ); cvDestroyWindow( "output1"); cvDestroyWindow( "output2"); cvReleaseCapture( &capture ); cvReleaseImage( &tpl ); cvReleaseImage( &tm ); cvReleaseImage( &gray); cvReleaseImage( &eyezone1); cvReleaseImage( &eyezone2); cvReleaseImage( &minieyezone1); cvReleaseImage( &minieyezone2); cvReleaseImage( &output1); cvReleaseImage( &output2); return 0; }
int main(){ ////////////////////GAME PROPERTIES/////////////////////////////////// //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "Tungu"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("multimedia/audio/background.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("multimedia/audio/bounce.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("multimedia/audio/point.wav"); sf::Sound point; point.setBuffer(buffer2); //ncp properties sf::RectangleShape ncp(sf::Vector2f(5, RENDERHEIGHT / 1.6)); ncp.setFillColor(sf::Color(50, 50, 50)); ncp.setPosition(RENDERWIDTH / 2, RENDERHEIGHT / 2 - (RENDERHEIGHT / 1.6) / 2 ); //player 1 properties int p1Len = 100; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 100; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(10, 25); ball.setFillColor(sf::Color(255, 255, 255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 15; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; //score-timer text sf::Font font; font.loadFromFile("fonts/LiberationSerif-Regular.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); sf::Text timer1("", font, 50); timer1.setPosition(0 , 5 * RENDERHEIGHT / 6); sf::Text timer2("", font, 50); timer2.setPosition(RENDERWIDTH / 2 - timer2.getLocalBounds().width, 5 * RENDERHEIGHT / 6); int time1 = 0; int time2 = 0; //gameover sf::Text gameover("GAME OVER", font, 120); gameover.setColor(sf::Color::Red); gameover.setPosition(0, RENDERHEIGHT / 3); ///////////////CAMERA FUNTIONS////////////////////// CvCapture* capture =0; capture = cvCaptureFromCAM(0); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black //cvNamedWindow("Player1"); ///cvNamedWindow("Player2"); //iterate through each frames of the video while(player1Score + player2Score != 7){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel /////////////////// Player 1 //////////////////// IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV, 85, 143, 40, 116, 256, 159); //guante cyan cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); /////////////////// Player 2 //////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV);//Change the color format from BGR to HSV IplImage* imgThresh2 = GetThresholdedImage(imgHSV2, 26, 145, 31, 73, 256, 111); //guante verde cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); //cvShowImage("Player1", imgThresh); //cvShowImage("Player2", imgThresh2); cvMoveWindow("Video", 1800, 0); cvResizeWindow("Video", 100, 240); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; /////////////////////////////Scores/////////////////////////////////// //score for player one winning if(player1Score == player2Score + 1) { timer1.setString(convertInt(time1 += 2)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 2) { timer1.setString(convertInt(time1 += 4)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 3) { timer1.setString(convertInt(time1 += 8)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 4) { timer1.setString(convertInt(time1 += 16)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 5) { timer1.setString(convertInt(time1 += 32)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 6) { timer1.setString(convertInt(time1 += 64)); timer2.setString(convertInt(time2 += 1)); } //score on equals if(player1Score == player2Score) { timer1.setString(convertInt(time1 += 1)); timer2.setString(convertInt(time2 += 1)); } //score for player two winning if(player2Score == player1Score + 1) { timer2.setString(convertInt(time2 += 2)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 2) { timer2.setString(convertInt(time2 += 4)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 3) { timer2.setString(convertInt(time2 += 8)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 4) { timer2.setString(convertInt(time2 += 16)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 5) { timer2.setString(convertInt(time2 += 32)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 6) { timer2.setString(convertInt(time2 += 64)); timer1.setString(convertInt(time1 += 1)); } //////////////////////////////Game//////////////////////////////////// //player 1 movement muy lento if(player100==6) player1.move(0, -(vel-3)); else if(player100==-6) player1.move(0, vel-3); //player 1 movement LENTO if(player100==1) player1.move(0, -vel); else if(player100==-1) player1.move(0, vel); //player 1 movement MEDIO if(player100==2) player1.move(0, -(vel+10)); else if(player100==-2) player1.move(0, vel+10); //player 1 movement RAPIDO if(player100==3) player1.move(0, -(vel+20)); else if(player100==-3) player1.move(0, vel+20); //player 1 movement muy rapido if(player100==4) player1.move(0, -(vel+25)); else if(player100==-4) player1.move(0, vel+25); //player 1 movement Extreme if(player100==5) player1.move(0, -(vel+30)); else if(player100==-5) player1.move(0, vel+30); //MOVIMIENTO GOLPE PLAYER1 if(player100==0) player1.move(0,0); if (player1.getPosition().x <= 10){ if(player100==22) player1.move(10, 0); } //player 2 movement muy LENTO if(player200==6) player2.move(0, -(vel-3)); else if(player200==-6) player2.move(0, vel-3); //player 2 movement LENTO if(player200==1) player2.move(0, -vel); else if(player200==-1) player2.move(0, vel); //player 2 movement MEDIO if(player200==2) player2.move(0, -(vel+10)); else if(player200==-2) player2.move(0, vel+10); //player 2 movement RAPIDO if(player200==3) player2.move(0, -(vel+20)); else if(player200==-3) player2.move(0, vel+20); //player 2 movement muy rapido if(player200==4) player2.move(0, -(vel+25)); else if(player200==-4) player2.move(0, vel+25); //player 2 movement Extreme if(player200==5) player2.move(0, -(vel+30)); else if(player200==-5) player2.move(0, vel+30); if(player200==0) player2.move(0,0); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-22) player2.move(-10, 0); } //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter){ ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x){ if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 5) * -1; ball.setFillColor(sf::Color(255,0,0)); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 3) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else{ ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.2; ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x){ if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y){ if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 5) * -1; ball.setFillColor(sf::Color(255,0,0)); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 3) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else{ ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.draw(ncp); pong.display(); while(player1Score + player2Score == 7){ if(player1Score > player2Score) timer1.setString(convertInt(time1 += 500)); if(player1Score < player2Score) timer2.setString(convertInt(time2 += 500)); pong.clear(sf::Color::Black); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(gameover); pong.display(); counter(3); break; } /////////////////////Finish Game///////////////////////////////// } std::stringstream ss; ss.str (timer1.getString()); std::string scorePlayer1 = ss.str(); ss.str (timer2.getString()); std::string scorePlayer2 = ss.str(); std::cout << "Final Score:" << '\n'; std::cout << "Player1: " + scorePlayer1 << '\n'; std::cout << "Player2: " + scorePlayer2 << '\n'; std::ofstream myfile ("highscores.txt", std::ofstream::in | std::ofstream::out | std::ofstream::app); if (myfile.is_open()) { myfile << scorePlayer1 << std::endl; myfile << scorePlayer2 << std::endl; myfile.close(); } else std::cout << "Unable to open file"; cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main( int argc, char** argv ) # { # CvCapture *capture; # int key; # # /* initialize camera */ # capture = cvCaptureFromCAM( 0 ); # # /* always check */ # if( !capture ) return 1; # # /* get video properties, needed by template image */ # frame = cvQueryFrame( capture ); # if ( !frame ) return 1; # # /* create template image */ # tpl = cvCreateImage( cvSize( TPL_WIDTH, TPL_HEIGHT ), # frame->depth, frame->nChannels ); # # /* create image for template matching result */ # tm = cvCreateImage( cvSize( WINDOW_WIDTH - TPL_WIDTH + 1, # WINDOW_HEIGHT - TPL_HEIGHT + 1 ), # IPL_DEPTH_32F, 1 ); # # /* create a window and install mouse handler */ # cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); # cvSetMouseCallback( "video", mouseHandler, NULL ); # # while( key != 'q' ) { # /* get a frame */ # frame = cvQueryFrame( capture ); # # /* always check */ # if( !frame ) break; # # /* 'fix' frame */ # cvFlip( frame, frame, -1 ); # frame->origin = 0; # # /* perform tracking if template is available */ # if( is_tracking ) trackObject(); # # /* display frame */ # cvShowImage( "video", frame ); # # /* exit if user press 'q' */ # key = cvWaitKey( 1 ); # } # # /* free memory */ # cvDestroyWindow( "video" ); # cvReleaseCapture( &capture ); # cvReleaseImage( &tpl ); # cvReleaseImage( &tm ); # # return 0; # }
int main(){ ////////////////////GAME PROPERTIES/////////////////////////////////// //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); ///////////////FINISH PROPERTIES////////////////////////////////////////////////////// CvCapture* capture =0; capture = cvCaptureFromCAM(1); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black cvNamedWindow("Video"); cvNamedWindow("Ball"); //iterate through each frames of the video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV, 94, 169, 127, 143, 251, 229); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); IplImage* imgThresh2 = GetThresholdedImage(imgHSV2, 22, 64, 152, 50, 134, 256); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); cvShowImage("Ball", imgThresh); cvShowImage("Ball2", imgThresh2); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; //////////////////////////////Game//////////////////////////////////// //player 1 movement if(player100==1){ player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if(player100==0){ player1.move(0,0);} if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); if(player200==0){ player2.move(0,0);} //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); /////////////////////Finish Game///////////////////////////////// } cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); //, sf::Style::Fullscreen pong.setMouseCursorVisible(false); pong.setFramerateLimit(24); /* //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); */ //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); //game loop //while(pong.isOpen()) /////////////////////Declaraciones//////////////////////////////// IplImage* frame=0; IplImage* imgTracking; CvCapture* capture =0; IplImage* imgThresh; IplImage* imgThresh2; sf::Event event; int player100=0; int player200=0; capture = cvCaptureFromCAM(1); for(int i=0;i<1000;++i) { if(!capture){ printf("Capture failure\n"); return -1; } frame = cvRetrieveFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV imgThresh = GetThresholdedImage(imgHSV, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV); delete(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); imgThresh2 = GetThresholdedImage(imgHSV2, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV2); delete(imgHSV2); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); /* //player 1 movement if( player100 == 1 ) { player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } }//fin IF //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); }//fin if elseh } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 10) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); }//fin else }//fin IF */ //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); }//fin for cvReleaseCapture(&capture); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); cvReleaseImage(&imgTracking); //cvReleaseImage(&imgTemp); revisar este puntero delete(moments); return 0; }