-
Notifications
You must be signed in to change notification settings - Fork 3
/
TrackerCamshift.cpp
174 lines (123 loc) · 4.53 KB
/
TrackerCamshift.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/video/tracking.hpp>
#include "TrackerCamshift.h"
using namespace std;
using namespace cv;
CAMShiftTracker::CAMShiftTracker() :
hmin (0),
hmax (180),
vmin (25),
vmax (256),
smin (30),
hsize (45)
{
hranges[0]=hmin;
hranges[1]=hmax;
phranges=hranges;
}
bool CAMShiftTracker::init(const cv::Mat& inputFrame, const cv::Rect& inputBox)
{
try {
// initialize the box
box = inputBox;
//Converting the input frame to HSV
cvtColor(inputFrame, hsv, CV_BGR2HSV);
//Mask with the values inside of the range for hsv
//Since we have 3 dimensions on the hsv, for every pixel
//we have to give 3 values to compare
inRange(hsv, cv::Scalar(hmin, smin, MIN(vmin,vmax)), cv::Scalar(hmax, 256, MAX(vmin, vmax)), mask);
int ch[] = {0, 0};
//Geting the hue component of the image
//Creating the image
hue.create(hsv.size(), hsv.depth());
//Coppying the channel hue of hsv to hue
mixChannels(&hsv, 1, &hue, 1, ch, 1);
//Getting the hue values of the template
roi= hue(inputBox);
//Getting the mask values of the template
maskroi=mask(inputBox);
//Computing the color histogram
//Get the histogram for roi, 1 mat only, channel 0, mask to compute only on those pixels
//output value, dimension of the histogram, and the ranges [hmin hmax]
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
//Normalizing the histogram
normalize(hist, hist, 0, 255, CV_MINMAX);
return 1;
} catch(...) {
std::cerr << "Error in MeanSiftTracker::init()." << std::endl;
return 0;
}
}
bool CAMShiftTracker::processFrame(const cv::Mat& newFrame, cv::Rect& resultBox)
{
try {
Mat hsv_newframe;
Mat hue_newframe;
//Converting the new image to HSV format
cvtColor(newFrame, hsv_newframe, CV_BGR2HSV);
//Getting the mask
inRange(hsv_newframe, cv::Scalar(hmin, smin, MIN(vmin,vmax)), cv::Scalar(hmax, 256, MAX(vmin, vmax)), mask_newFrame);
//Coppying the channel hue of hsv to hue
hue_newframe.create(hsv_newframe.size(), hsv_newframe.depth());
int ch[] = {0, 0};
mixChannels(&hsv_newframe, 1, &hue_newframe, 1, ch, 1);
//Getting backproject
calcBackProject(&hue_newframe,1,0,hist,backproj,&phranges);
backproj &= mask_newFrame;
RotatedRect trackBox = CamShift(backproj, box, TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
resultBox=trackBox.boundingRect();
//cout<<trackBox.center.x<<" "<<trackBox.center.y <<endl;
//Mat image;
//cvtColor( backproj, image, CV_GRAY2BGR );
//imshow("back projection",image);
//waitKey(10);
//box=trackBox.boundingRect();
return 1;
} catch(...) {
std::cerr << "Error in MeanSiftTracker::processFrame()." << std::endl;
return 0;
}
}
bool TrackerCamshiftFace::init (const Mat& _frame, const Face& _face, void* _anyData)
{
try {
(void)_anyData;
// get rectangles from face
const std::vector< const cv::Rect* > face_parts = _face.asPtrVector();
if ( face_parts.size() > 3 )
{
cerr << "Error in TrackerCamshiftFace::init() "
<< "- more than 3 face rectangles." << endl;
return 0;
}
// init every rectangles
for (unsigned int part = 0; part != face_parts.size(); ++part )
if (! trackers[part].init(_frame, *face_parts[part]) ) return 0;
return 1;
} catch(...) {
cerr << "Exception in TrackerCamshiftFace::init()." << endl;
return 0;
}
}
bool TrackerCamshiftFace::processFrame (const Mat& _frame, Face& _face)
{
try {
// get rectangles from face
std::vector< cv::Rect* > face_parts = _face.asPtrVector();
if ( face_parts.size() > 3 )
{
cerr << "Error in TrackerCamshiftFace::processFrame() "
<< "- more than 3 face rectangles." << endl;
return 0;
}
// process every rectangles
for (unsigned int part = 0; part != face_parts.size(); ++part )
if (! trackers[part].processFrame(_frame, *face_parts[part]) ) return 0;
return 1;
} catch(...) {
cerr << "Exception in TrackerCamshiftFace::processFrame()." << endl;
return 0;
}
}