updated on Fri Jan 13 20:02:10 UTC 2012
[aur-mirror.git] / ar-breakout / gestlib.h
blob84f1588af12ad804246dd856c34a1315eb6e2419
1 #include <cv.h>
2 #include <cvaux.h>
3 #include <highgui.h>
5 cv::Mat currentFrame; //Stores the current frame read from capture device
7 class camera
9 private:
10 cv::VideoCapture cameraDevice;
11 public:
12 camera(int deviceNumber=0)
14 cameraDevice.open(0); //Open specified camera device
15 cameraDevice.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
16 cameraDevice.set(CV_CAP_PROP_FRAME_WIDTH, 320);
19 camera(std::string filename)
21 cameraDevice.open(filename); //Open video file
24 void read()
26 cameraDevice >> currentFrame;
27 cv::flip(currentFrame, currentFrame, 1);
30 ~camera()
32 cameraDevice.release();
33 currentFrame.release();
38 class tracker
40 private:
41 cv::Mat foreground;
42 cv::Mat tempFrame, hsvFrame, grayFrame;
43 cv::Scalar skin_hsv_min, skin_hsv_max;
44 CvMemStorage *storage;
45 CvSeq *conts;
46 CvSeq Hand;
49 public:
50 tracker()
52 //Set range for skin color
53 skin_hsv_min = cvScalar(0, 20, 50, 0);
54 skin_hsv_max = cvScalar(30, 200, 255, 0);
55 storage = cvCreateMemStorage(0);
56 conts = 0;
59 void getForeground()
61 //Convert to hsv color for skin detection
62 cv::cvtColor(currentFrame, hsvFrame, CV_BGR2HSV);
63 cv::cvtColor(currentFrame, grayFrame, CV_BGR2GRAY);
65 //Detect skin colored pixels in image
66 cv::inRange(hsvFrame, skin_hsv_min, skin_hsv_max, foreground);
69 //Noise Removal
70 cv::dilate(foreground, foreground, cv::Mat());
71 cv::erode(foreground, foreground, cv::Mat());
73 foreground.copyTo(tempFrame);
75 cvFindContours(&(foreground.operator IplImage()), storage, &conts, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
77 currentFrame.copyTo(foreground);
80 cv::Mat read()
82 return foreground;
85 int collide(int x, int y)
87 return pointIsInsideContour((CvContour*)&Hand, x, y);;
90 void findHand()
92 CvSeq *cont = conts;
93 double area, tmp;
94 area = 0;
96 while(cont!=NULL) //Find contour with largest area
98 tmp = cvContourArea(cont);
99 if(tmp > area)
101 area = tmp;
102 Hand = *cont;
103 currentFrame.copyTo(foreground);
106 cont = cont->h_next;
108 cvDrawContours(&(foreground.operator IplImage()), &Hand, CV_RGB(255,0,0), CV_RGB(0,255,0), 0, 10, CV_AA, cvPoint(0,0));
109 return;
112 ~tracker()
114 foreground.release();
117 protected:
118 char pointIsInsideContour(CvContour *contour, int x, int y)
120 char found_left=0, found_top=0, found_right=0, found_bottom=0;
121 int count, i;
122 CvPoint *contourPoint;
124 if(!contour)
125 return 0;
127 count = contour->total;
129 for(i=0;i<count;i++)
131 contourPoint = (CvPoint *)CV_GET_SEQ_ELEM(CvPoint,contour,i); //Get i'th point in the contour
133 if(contourPoint->x == x)
135 if(contourPoint->y < y)
136 found_top = 1;
137 else
138 found_bottom = 1;
140 if(contourPoint->y == y)
142 if(contourPoint->x < x)
143 found_left = 1;
144 else
145 found_right = 1;
149 return found_left && found_top && found_right && found_bottom; //Returns true if four points around (x,y) are in the contour;