5 cv::Mat currentFrame
; //Stores the current frame read from capture device
10 cv::VideoCapture cameraDevice
;
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
26 cameraDevice
>> currentFrame
;
27 cv::flip(currentFrame
, currentFrame
, 1);
32 cameraDevice
.release();
33 currentFrame
.release();
42 cv::Mat tempFrame
, hsvFrame
, grayFrame
;
43 cv::Scalar skin_hsv_min
, skin_hsv_max
;
44 CvMemStorage
*storage
;
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);
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
);
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
);
85 int collide(int x
, int y
)
87 return pointIsInsideContour((CvContour
*)&Hand
, x
, y
);;
96 while(cont
!=NULL
) //Find contour with largest area
98 tmp
= cvContourArea(cont
);
103 currentFrame
.copyTo(foreground
);
108 cvDrawContours(&(foreground
.operator IplImage()), &Hand
, CV_RGB(255,0,0), CV_RGB(0,255,0), 0, 10, CV_AA
, cvPoint(0,0));
114 foreground
.release();
118 char pointIsInsideContour(CvContour
*contour
, int x
, int y
)
120 char found_left
=0, found_top
=0, found_right
=0, found_bottom
=0;
122 CvPoint
*contourPoint
;
127 count
= contour
->total
;
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
)
140 if(contourPoint
->y
== y
)
142 if(contourPoint
->x
< x
)
149 return found_left
&& found_top
&& found_right
&& found_bottom
; //Returns true if four points around (x,y) are in the contour;