1 | // camshift_wrapper.c - by Robin Hewitt, 2007 |
---|
2 | // http://www.cognotics.com/opencv/downloads/camshift_wrapper |
---|
3 | // This is free software. See License.txt, in the download |
---|
4 | // package, for details. |
---|
5 | // |
---|
6 | |
---|
7 | #include "cv.h" |
---|
8 | #include <stdio.h> |
---|
9 | #include "camshift_wrapper.h" |
---|
10 | |
---|
11 | |
---|
12 | // Parameters |
---|
13 | int nHistBins = 30; // number of histogram bins |
---|
14 | float rangesArr[] = {0,180}; // histogram range |
---|
15 | int vmin = 65, vmax = 100, smin = 55; // limits for calculating hue |
---|
16 | |
---|
17 | |
---|
18 | // File-level variables |
---|
19 | IplImage * pHSVImg = 0; // the input image converted to HSV color mode |
---|
20 | IplImage * pHueImg = 0; // the Hue channel of the HSV image |
---|
21 | IplImage * pMask = 0; // this image is used for masking pixels |
---|
22 | IplImage * pProbImg = 0; // the face probability estimates for each pixel |
---|
23 | CvHistogram * pHist = 0; // histogram of hue in the original face image |
---|
24 | |
---|
25 | CvRect prevFaceRect; // location of face in previous frame |
---|
26 | CvBox2D faceBox; // current face-location estimate |
---|
27 | |
---|
28 | |
---|
29 | int nFrames = 0; |
---|
30 | |
---|
31 | |
---|
32 | // Declarations for internal functions |
---|
33 | void updateHueImage(const IplImage * pImg); |
---|
34 | |
---|
35 | |
---|
36 | ////////////////////////////////// |
---|
37 | // createTracker() |
---|
38 | // |
---|
39 | int createTracker(const IplImage * pImg) |
---|
40 | { |
---|
41 | // Allocate the main data structures ahead of time |
---|
42 | float * pRanges = rangesArr; |
---|
43 | pHSVImg = cvCreateImage( cvGetSize(pImg), 8, 3 ); |
---|
44 | pHueImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); |
---|
45 | pMask = cvCreateImage( cvGetSize(pImg), 8, 1 ); |
---|
46 | pProbImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); |
---|
47 | |
---|
48 | pHist = cvCreateHist( 1, &nHistBins, CV_HIST_ARRAY, &pRanges, 1 ); |
---|
49 | |
---|
50 | return 1; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | ////////////////////////////////// |
---|
55 | // releaseTracker() |
---|
56 | // |
---|
57 | void releaseTracker() |
---|
58 | { |
---|
59 | // Release all tracker resources |
---|
60 | cvReleaseImage( &pHSVImg ); |
---|
61 | cvReleaseImage( &pHueImg ); |
---|
62 | cvReleaseImage( &pMask ); |
---|
63 | cvReleaseImage( &pProbImg ); |
---|
64 | |
---|
65 | cvReleaseHist( &pHist ); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | ////////////////////////////////// |
---|
70 | // startTracking() |
---|
71 | // |
---|
72 | void startTracking(IplImage * pImg, CvRect * pFaceRect) |
---|
73 | { |
---|
74 | float maxVal = 0.f; |
---|
75 | |
---|
76 | // Make sure internal data structures have been allocated |
---|
77 | if( !pHist ) createTracker(pImg); |
---|
78 | |
---|
79 | // Create a new hue image |
---|
80 | updateHueImage(pImg); |
---|
81 | |
---|
82 | // Create a histogram representation for the face |
---|
83 | cvSetImageROI( pHueImg, *pFaceRect ); |
---|
84 | cvSetImageROI( pMask, *pFaceRect ); |
---|
85 | cvCalcHist( &pHueImg, pHist, 0, pMask ); |
---|
86 | cvGetMinMaxHistValue( pHist, 0, &maxVal, 0, 0 ); |
---|
87 | cvConvertScale( pHist->bins, pHist->bins, maxVal? 255.0/maxVal : 0, 0 ); |
---|
88 | cvResetImageROI( pHueImg ); |
---|
89 | cvResetImageROI( pMask ); |
---|
90 | |
---|
91 | // Store the previous face location |
---|
92 | prevFaceRect = *pFaceRect; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | ////////////////////////////////// |
---|
97 | // track() |
---|
98 | // |
---|
99 | CvRect track(IplImage * pImg) |
---|
100 | { |
---|
101 | CvConnectedComp components; |
---|
102 | |
---|
103 | // Create a new hue image |
---|
104 | updateHueImage(pImg); |
---|
105 | |
---|
106 | // Create a probability image based on the face histogram |
---|
107 | cvCalcBackProject( &pHueImg, pProbImg, pHist ); |
---|
108 | cvAnd( pProbImg, pMask, pProbImg, 0 ); |
---|
109 | |
---|
110 | // Use CamShift to find the center of the new face probability |
---|
111 | cvCamShift( pProbImg, prevFaceRect, |
---|
112 | cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ), |
---|
113 | &components, &faceBox ); |
---|
114 | |
---|
115 | // Update face location and angle |
---|
116 | prevFaceRect = components.rect; |
---|
117 | faceBox.angle = -faceBox.angle; |
---|
118 | //cvEllipseBox(pImg, faceBox, |
---|
119 | // CV_RGB(255,0,0), 3, CV_AA, 0 ); |
---|
120 | return prevFaceRect; |
---|
121 | //return faceBox; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | ////////////////////////////////// |
---|
126 | // updateHueImage() |
---|
127 | // |
---|
128 | void updateHueImage(const IplImage * pImg) |
---|
129 | { |
---|
130 | // Convert to HSV color model |
---|
131 | cvCvtColor( pImg, pHSVImg, CV_BGR2HSV ); |
---|
132 | |
---|
133 | // Mask out-of-range values |
---|
134 | cvInRangeS( pHSVImg, cvScalar(0, smin, MIN(vmin,vmax), 0), |
---|
135 | cvScalar(180, 256, MAX(vmin,vmax) ,0), pMask ); |
---|
136 | |
---|
137 | // Extract the hue channel |
---|
138 | cvSplit( pHSVImg, pHueImg, 0, 0, 0 ); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | ////////////////////////////////// |
---|
143 | // setVmin() |
---|
144 | // |
---|
145 | void setVmin(int _vmin) |
---|
146 | { vmin = _vmin; } |
---|
147 | |
---|
148 | |
---|
149 | ////////////////////////////////// |
---|
150 | // setSmin() |
---|
151 | // |
---|
152 | void setSmin(int _smin) |
---|
153 | { smin = _smin; } |
---|
154 | |
---|
155 | |
---|