int main(int argc, char* argv[]) { CvSize size640x480 = cvSize(640, 480); // use a 640 x 480 size for all windows, also make sure your webcam is set to 640x480 !! CvCapture* p_capWebcam; // we will assign our web cam video stream to this later . . . IplImage* p_imgOriginal; // pointer to an image structure, this will be the input image from webcam IplImage* p_imgProcessed; // pointer to an image structure, this will be the processed image /* IPL is short for Intel Image Processing Library, this is the structure used in OpenCV 1.x to work with images */ CvMemStorage* p_strStorage; // necessary storage variable to pass into cvHoughCircles() CvSeq* p_seqCircles; // pointer to an OpenCV sequence, will be returned by cvHough Circles() and will contain all circles // call cvGetSeqElem(p_seqCircles, i) will return a 3 element array of the ith circle (see next variable) float* p_fltXYRadius; // pointer to a 3 element array of floats // [0] => x position of detected object // [1] => y position of detected object // [2] => radius of detected object int i; // loop counter char charCheckForEscKey; // char for checking key press (Esc exits program) p_capWebcam = cvCaptureFromCAM(0); // 0 => use 1st webcam, may have to change to a different number if you have multiple cameras if(p_capWebcam == NULL) { // if capture was not successful . . . printf("error: capture is NULL \n"); // error message to standard out . . . getchar(); // getchar() to pause for user see message . . . return(-1); // exit program } // declare 2 windows cvNamedWindow("Original", CV_WINDOW_AUTOSIZE); // original image from webcam cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE); // the processed image we will use for detecting circles p_imgProcessed = cvCreateImage(size640x480, // 640 x 480 pixels (CvSize struct from earlier) IPL_DEPTH_8U, // 8-bit color depth 1); // 1 channel (grayscale), if this was a color image, use 3 while(1) { // for each frame . . . p_imgOriginal = cvQueryFrame(p_capWebcam); // get frame from webcam if(p_imgOriginal == NULL) { // if frame was not captured successfully . . . printf("error: frame is NULL \n"); // error message to std out getchar(); break; } cvInRangeS(p_imgOriginal, // function input CV_RGB(175, 0, 0), // min filtering value (if color is greater than or equal to this) CV_RGB(256,100,100), // max filtering value (if color is less than this) p_imgProcessed); // function output p_strStorage = cvCreateMemStorage(0); // allocate necessary memory storage variable to pass into cvHoughCircles() // smooth the processed image, this will make it easier for the next function to pick out the circles cvSmooth(p_imgProcessed, // function input p_imgProcessed, // function output CV_GAUSSIAN, // use Gaussian filter (average nearby pixels, with closest pixels weighted more) 9, // smoothing filter window width 9); // smoothing filter window height // fill sequential structure with all circles in processed image p_seqCircles = cvHoughCircles(p_imgProcessed, // input image, nothe that this has to be grayscale (no color) p_strStorage, // provide function with memory storage, makes function return a pointer to a CvSeq CV_HOUGH_GRADIENT, // two-pass algorithm for detecting circles, this is the only choice available 2, // size of image / 2 = "accumulator resolution", i.e. accum = res = size of image / 2 p_imgProcessed->height / 4, // min distance in pixels between the centers of the detected circles 100, // high threshold of Canny edge detector, called by cvHoughCircles 50, // low threshold of Canny edge detector, called by cvHoughCircles 10, // min circle radius, in pixels 400); // max circle radius, in pixels for(i=0; i < p_seqCircles->total; i++) { // for each element in sequential circles structure (i.e. for each object detected) p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i); // from the sequential structure, read the ith value into a pointer to a float printf("ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0], // x position of center point of circle p_fltXYRadius[1], // y position of center point of circle p_fltXYRadius[2]); // radius of circle // draw a small green circle at center of detected object cvCircle(p_imgOriginal, // draw on the original image cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), // center point of circle 3, // 3 pixel radius of circle CV_RGB(0,255,0), // draw pure green CV_FILLED); // thickness, fill in the circle // draw a red circle around the detected object cvCircle(p_imgOriginal, // draw on the original image cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), // center point of circle cvRound(p_fltXYRadius[2]), // radius of circle in pixels CV_RGB(255,0,0), // draw pure red 3); // thickness of circle in pixels } // end for cvShowImage("Original", p_imgOriginal); // original image with detectec ball overlay cvShowImage("Processed", p_imgProcessed); // image after processing cvReleaseMemStorage(&p_strStorage); // deallocate necessary storage variable to pass into cvHoughCircles charCheckForEscKey = cvWaitKey(10); // delay (in ms), and get key press, if any if(charCheckForEscKey == 27) break; // if Esc key (ASCII 27) was pressed, jump out of while loop } // end while cvReleaseCapture(&p_capWebcam); // release memory as applicable cvDestroyWindow("Original"); cvDestroyWindow("Processed"); return(0); }
List<Garbage> garbageList(IplImage * src){ cvNamedWindow("output",CV_WINDOW_AUTOSIZE); //frame from video IplImage * src; //object model IplImage * model; if( argc != 2 || (capture=cvCreateFileCapture( argv[1]))== 0 ){ perror("Invalid capture"); return 0; } //image for the histogram-based filter //could be a parameter model=cvLoadImage("../images/colilla-sinBlanco.png",1); CvHistogram * testImageHistogram=getHShistogramFromRGB(model,HIST_H_BINS,HIST_S_BINS); //~ int frameWidth=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH); //~ int frameHeight=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT); //gets a frame for setting image size //CvSize srcSize = cvSize(frameWidth,frameHeight); src=cvQueryFrame(capture); CvSize srcSize = cvGetSize(src); //images for HSV conversion IplImage* hsv = cvCreateImage( srcSize, 8, 3 ); IplImage* h_plane = cvCreateImage( srcSize, 8, 1 ); IplImage* s_plane = cvCreateImage( srcSize, 8, 1 ); IplImage* v_plane = cvCreateImage( srcSize, 8, 1 ); //Image for thresholding IplImage * threshImage=cvCreateImage(srcSize,8,1); //image for equalization IplImage * equalizedImage=cvCreateImage(srcSize,8,1); //image for Morphing operations(Dilate-erode) IplImage * morphImage=cvCreateImage(srcSize,8,1); //image for image smoothing IplImage * smoothImage=cvCreateImage(srcSize,8,1); //image for contour-finding operations IplImage * contourImage=cvCreateImage(srcSize,8,3); int frameCounter=1; int cont_index=0; //convolution kernel for morph operations IplConvKernel* element; CvRect boundingRect; //contours CvSeq ** contours; //Main loop frameCounter++; printf("frame number:%d\n",frameCounter); //convert image to hsv cvCvtColor( src, hsv, CV_BGR2HSV ); cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); //equalize Saturation Channel image cvEqualizeHist(s_plane,equalizedImage); //threshold the equalized Saturation channel image cvThreshold(equalizedImage,threshImage,THRESHOLD_VALUE,255, CV_THRESH_BINARY); //apply morphologic operations element = cvCreateStructuringElementEx( MORPH_KERNEL_SIZE*2+1, MORPH_KERNEL_SIZE*2+1, MORPH_KERNEL_SIZE, MORPH_KERNEL_SIZE, CV_SHAPE_RECT, NULL); cvDilate(threshImage,morphImage,element,MORPH_DILATE_ITER); cvErode(morphImage,morphImage,element,MORPH_ERODE_ITER); //apply smooth gaussian-filter cvSmooth(morphImage,smoothImage,CV_GAUSSIAN,3,0,0,0); //get all contours contours=findContours(smoothImage); cont_index=0; cvCopy(src,contourImage,0); while(contours[cont_index]!=NULL){ CvSeq * aContour=contours[cont_index]; //apply filters if(perimeterFilter(aContour,MINCONTOUR_PERIMETER, MAXCONTOUR_PERIMETER) && //areaFilter(aContour,MINCONTOUR_AREA,MAXCONTOUR_AREA) && rectangularAspectFilter(aContour,CONTOUR_RECTANGULAR_MIN_RATIO, CONTOUR_RECTANGULAR_MAX_RATIO) && boxAreaFilter(aContour,BOXFILTER_TOLERANCE) && histogramMatchingFilter(src,aContour,testImageHistogram, HIST_H_BINS,HIST_S_BINS,HIST_MIN)){ //if passed filters printContour(aContour,3,cvScalar(127,127,0,0), contourImage); //get contour bounding box boundingRect=cvBoundingRect(aContour,0); cvRectangle(contourImage,cvPoint(boundingRect.x,boundingRect.y), cvPoint(boundingRect.x+boundingRect.width, boundingRect.y+boundingRect.height), _GREEN,1,8,0); } cont_index++; } cvShowImage("output",contourImage); cvWaitKey(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; }
void icvPlaceDistortedSample( CvArr* background, int inverse, int maxintensitydev, double maxxangle, double maxyangle, double maxzangle, int inscribe, double maxshiftf, double maxscalef, CvSampleDistortionData* data ) { double quad[4][2]; int r, c; uchar* pimg; uchar* pbg; uchar* palpha; uchar chartmp; int forecolordev; float scale; IplImage* img; IplImage* maskimg; CvMat stub; CvMat* bgimg; CvRect cr; CvRect roi; double xshift, yshift, randscale; icvRandomQuad( data->src->width, data->src->height, quad, maxxangle, maxyangle, maxzangle ); quad[0][0] += (double) data->dx; quad[0][1] += (double) data->dy; quad[1][0] += (double) data->dx; quad[1][1] += (double) data->dy; quad[2][0] += (double) data->dx; quad[2][1] += (double) data->dy; quad[3][0] += (double) data->dx; quad[3][1] += (double) data->dy; cvSet( data->img, cvScalar( data->bgcolor ) ); cvSet( data->maskimg, cvScalar( 0.0 ) ); cvWarpPerspective( data->src, data->img, quad ); cvWarpPerspective( data->mask, data->maskimg, quad ); cvSmooth( data->maskimg, data->maskimg, CV_GAUSSIAN, 3, 3 ); bgimg = cvGetMat( background, &stub ); cr.x = data->dx; cr.y = data->dy; cr.width = data->src->width; cr.height = data->src->height; if( inscribe ) { /* quad's circumscribing rectangle */ cr.x = (int) MIN( quad[0][0], quad[3][0] ); cr.y = (int) MIN( quad[0][1], quad[1][1] ); cr.width = (int) (MAX( quad[1][0], quad[2][0] ) + 0.5F ) - cr.x; cr.height = (int) (MAX( quad[2][1], quad[3][1] ) + 0.5F ) - cr.y; } xshift = maxshiftf * rand() / RAND_MAX; yshift = maxshiftf * rand() / RAND_MAX; cr.x -= (int) ( xshift * cr.width ); cr.y -= (int) ( yshift * cr.height ); cr.width = (int) ((1.0 + maxshiftf) * cr.width ); cr.height = (int) ((1.0 + maxshiftf) * cr.height); randscale = maxscalef * rand() / RAND_MAX; cr.x -= (int) ( 0.5 * randscale * cr.width ); cr.y -= (int) ( 0.5 * randscale * cr.height ); cr.width = (int) ((1.0 + randscale) * cr.width ); cr.height = (int) ((1.0 + randscale) * cr.height); scale = MAX( ((float) cr.width) / bgimg->cols, ((float) cr.height) / bgimg->rows ); roi.x = (int) (-0.5F * (scale * bgimg->cols - cr.width) + cr.x); roi.y = (int) (-0.5F * (scale * bgimg->rows - cr.height) + cr.y); roi.width = (int) (scale * bgimg->cols); roi.height = (int) (scale * bgimg->rows); img = cvCreateImage( cvSize( bgimg->cols, bgimg->rows ), IPL_DEPTH_8U, 1 ); maskimg = cvCreateImage( cvSize( bgimg->cols, bgimg->rows ), IPL_DEPTH_8U, 1 ); cvSetImageROI( data->img, roi ); cvResize( data->img, img ); cvResetImageROI( data->img ); cvSetImageROI( data->maskimg, roi ); cvResize( data->maskimg, maskimg ); cvResetImageROI( data->maskimg ); forecolordev = (int) (maxintensitydev * (2.0 * rand() / RAND_MAX - 1.0)); for( r = 0; r < img->height; r++ ) { for( c = 0; c < img->width; c++ ) { pimg = (uchar*) img->imageData + r * img->widthStep + c; pbg = (uchar*) bgimg->data.ptr + r * bgimg->step + c; palpha = (uchar*) maskimg->imageData + r * maskimg->widthStep + c; chartmp = (uchar) MAX( 0, MIN( 255, forecolordev + (*pimg) ) ); if( inverse ) { chartmp ^= 0xFF; } *pbg = (uchar) (( chartmp*(*palpha )+(255 - (*palpha) )*(*pbg) ) / 255); } } cvReleaseImage( &img ); cvReleaseImage( &maskimg ); }