#include "stdafx.h"

#include <cv.h>
#include <highgui.h>

//This function threshold the HSV image and create a binary image
IplImage* GetThresholdedImage(IplImage* imgHSV){        
       IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
       cvInRangeS(imgHSV, cvScalar(170,160,60), cvScalar(180,256,256), imgThresh); 
       return imgThresh;
} 
 
int main(){
      CvCapture* capture =0;       

      capture = cvCaptureFromCAM(0);
      if(!capture){
            printf("Capture failure\n");
            return -1;
      }
      
      IplImage* frame1=0;
      cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);      
      cvNamedWindow("Ball", CV_WINDOW_AUTOSIZE);


      //iterate through each frames of the video      
      while(true){

            frame1 = cvQueryFrame(capture);            
            if(!frame1) break;

            frame1=cvCloneImage(frame1); 
            cvSmooth(frame1, frame1, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel

            IplImage* imgHSV = cvCreateImage(cvGetSize(frame1), IPL_DEPTH_8U, 3); 
            cvCvtColor(frame1, 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
            
            cvShowImage("Ball", imgThresh);            
            cvShowImage("Video", frame1);
            
            //Clean up used images
            cvReleaseImage(&imgHSV);
            cvReleaseImage(&imgThresh);            
            cvReleaseImage(&frame1);

            //Wait 50mS
            int c = cvWaitKey(10);
            //If 'ESC' is pressed, break the loop
            if((char)c==27 ) break;      
      }

      cvDestroyAllWindows() ;
      cvReleaseCapture(&capture);     

      return 0;
}