diff --git a/src/vision/vision.cpp b/src/vision/vision.cpp index 1d436de..43de1aa 100644 --- a/src/vision/vision.cpp +++ b/src/vision/vision.cpp @@ -1,11 +1,11 @@ #include "cv.h" #include "highgui.h" -IplImage* GetThresholdedImage(IplImage* img) -{ +IplImage* GetThresholdedImage(IplImage* img){ // Convert the image into an HSV image IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3); cvCvtColor(img, imgHSV, CV_BGR2HSV); IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1); + // Set yellow to white and all other colors to black //cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed); cvInRangeS(imgHSV, cvScalar(10, 100, 100), cvScalar(40, 255, 255), imgThreshed); cvReleaseImage(&imgHSV); @@ -19,20 +19,13 @@ int main() capture = cvCaptureFromCAM(0); // Couldn't get a device? Throw an error and quit - if(!capture) - { + if(!capture){ printf("Could not initialize capturing..."); return -1; } - // The two windows we'll be using cvNamedWindow("video"); cvNamedWindow("thresh"); - // This image holds the "scribble" data... - // the tracked positions of the ball - IplImage* imgScribble = NULL; - // An infinite loop - while(true) - { + while(true){ // Will hold a frame captured from the camera IplImage* frame = 0; frame = cvQueryFrame(capture); @@ -40,56 +33,18 @@ int main() if(!frame) break; // If this is the first frame, we need to initialize it - if(imgScribble == NULL) - { - imgScribble = cvCreateImage(cvGetSize(frame), 8, 3); - } // Holds the yellow thresholded image (yellow = white, rest = black) IplImage* imgYellowThresh = GetThresholdedImage(frame); - // Calculate the moments to estimate the position of the ball - CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments)); - cvMoments(imgYellowThresh, moments, 1); - // The actual moment values - double moment10 = cvGetSpatialMoment(moments, 1, 0); - double moment01 = cvGetSpatialMoment(moments, 0, 1); - - double area = cvGetCentralMoment(moments, 0, 0); - // Holding the last and current ball positions - static int posX = 0; - - static int posY = 0; - - int lastX = posX; - - int lastY = posY; - - posX = moment10/area; - posY = moment01/area; - // Print it out for debugging purposes - printf("position (%d,%d)", posX, posY); - // We want to draw a line only if its a valid position - if(lastX>0 && lastY>0 && posX>0 && posY>0) - { - // Draw a yellow line from the previous point to the current point - cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,255,255), 5); - - } - // Add the scribbling image and the frame... - cvAdd(frame, imgScribble, frame); cvShowImage("thresh", imgYellowThresh); cvShowImage("video", frame); - // Wait for a keypress + // Quit on keypress int c = cvWaitKey(10); - if(c!=-1) - { - // If pressed, break out of the loop + if(c!=-1){ break; } - // Release the thresholded image+moments... we need no memory leaks.. please cvReleaseImage(&imgYellowThresh); - delete moments; } // We're done using the camera. Other applications can now use it cvReleaseCapture(&capture);