source: level2/competencies/FaceTracking/src/CamshiftWrapper/camshift_wrapper.cpp @ 201

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