Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/src/camshift.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of Intel Corporation may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
#include "precomp.hpp"
43
44
int cv::meanShift( InputArray _probImage, Rect& window, TermCriteria criteria )
45
{
46
CV_INSTRUMENT_REGION();
47
48
Size size;
49
int cn;
50
Mat mat;
51
UMat umat;
52
bool isUMat = _probImage.isUMat();
53
54
if (isUMat)
55
umat = _probImage.getUMat(), cn = umat.channels(), size = umat.size();
56
else
57
mat = _probImage.getMat(), cn = mat.channels(), size = mat.size();
58
59
Rect cur_rect = window;
60
61
CV_Assert( cn == 1 );
62
63
if( window.height <= 0 || window.width <= 0 )
64
CV_Error( Error::StsBadArg, "Input window has non-positive sizes" );
65
66
window = window & Rect(0, 0, size.width, size.height);
67
68
double eps = (criteria.type & TermCriteria::EPS) ? std::max(criteria.epsilon, 0.) : 1.;
69
eps = cvRound(eps*eps);
70
int i, niters = (criteria.type & TermCriteria::MAX_ITER) ? std::max(criteria.maxCount, 1) : 100;
71
72
for( i = 0; i < niters; i++ )
73
{
74
cur_rect = cur_rect & Rect(0, 0, size.width, size.height);
75
if( cur_rect == Rect() )
76
{
77
cur_rect.x = size.width/2;
78
cur_rect.y = size.height/2;
79
}
80
cur_rect.width = std::max(cur_rect.width, 1);
81
cur_rect.height = std::max(cur_rect.height, 1);
82
83
Moments m = isUMat ? moments(umat(cur_rect)) : moments(mat(cur_rect));
84
85
// Calculating center of mass
86
if( fabs(m.m00) < DBL_EPSILON )
87
break;
88
89
int dx = cvRound( m.m10/m.m00 - window.width*0.5 );
90
int dy = cvRound( m.m01/m.m00 - window.height*0.5 );
91
92
int nx = std::min(std::max(cur_rect.x + dx, 0), size.width - cur_rect.width);
93
int ny = std::min(std::max(cur_rect.y + dy, 0), size.height - cur_rect.height);
94
95
dx = nx - cur_rect.x;
96
dy = ny - cur_rect.y;
97
cur_rect.x = nx;
98
cur_rect.y = ny;
99
100
// Check for coverage centers mass & window
101
if( dx*dx + dy*dy < eps )
102
break;
103
}
104
105
window = cur_rect;
106
return i;
107
}
108
109
110
cv::RotatedRect cv::CamShift( InputArray _probImage, Rect& window,
111
TermCriteria criteria )
112
{
113
CV_INSTRUMENT_REGION();
114
115
const int TOLERANCE = 10;
116
Size size;
117
Mat mat;
118
UMat umat;
119
bool isUMat = _probImage.isUMat();
120
121
if (isUMat)
122
umat = _probImage.getUMat(), size = umat.size();
123
else
124
mat = _probImage.getMat(), size = mat.size();
125
126
meanShift( _probImage, window, criteria );
127
128
window.x -= TOLERANCE;
129
if( window.x < 0 )
130
window.x = 0;
131
132
window.y -= TOLERANCE;
133
if( window.y < 0 )
134
window.y = 0;
135
136
window.width += 2 * TOLERANCE;
137
if( window.x + window.width > size.width )
138
window.width = size.width - window.x;
139
140
window.height += 2 * TOLERANCE;
141
if( window.y + window.height > size.height )
142
window.height = size.height - window.y;
143
144
// Calculating moments in new center mass
145
Moments m = isUMat ? moments(umat(window)) : moments(mat(window));
146
147
double m00 = m.m00, m10 = m.m10, m01 = m.m01;
148
double mu11 = m.mu11, mu20 = m.mu20, mu02 = m.mu02;
149
150
if( fabs(m00) < DBL_EPSILON )
151
return RotatedRect();
152
153
double inv_m00 = 1. / m00;
154
int xc = cvRound( m10 * inv_m00 + window.x );
155
int yc = cvRound( m01 * inv_m00 + window.y );
156
double a = mu20 * inv_m00, b = mu11 * inv_m00, c = mu02 * inv_m00;
157
158
// Calculating width & height
159
double square = std::sqrt( 4 * b * b + (a - c) * (a - c) );
160
161
// Calculating orientation
162
double theta = atan2( 2 * b, a - c + square );
163
164
// Calculating width & length of figure
165
double cs = cos( theta );
166
double sn = sin( theta );
167
168
double rotate_a = cs * cs * mu20 + 2 * cs * sn * mu11 + sn * sn * mu02;
169
double rotate_c = sn * sn * mu20 - 2 * cs * sn * mu11 + cs * cs * mu02;
170
double length = std::sqrt( rotate_a * inv_m00 ) * 4;
171
double width = std::sqrt( rotate_c * inv_m00 ) * 4;
172
173
// In case, when tetta is 0 or 1.57... the Length & Width may be exchanged
174
if( length < width )
175
{
176
std::swap( length, width );
177
std::swap( cs, sn );
178
theta = CV_PI*0.5 - theta;
179
}
180
181
// Saving results
182
int _xc = cvRound( xc );
183
int _yc = cvRound( yc );
184
185
int t0 = cvRound( fabs( length * cs ));
186
int t1 = cvRound( fabs( width * sn ));
187
188
t0 = MAX( t0, t1 ) + 2;
189
window.width = MIN( t0, (size.width - _xc) * 2 );
190
191
t0 = cvRound( fabs( length * sn ));
192
t1 = cvRound( fabs( width * cs ));
193
194
t0 = MAX( t0, t1 ) + 2;
195
window.height = MIN( t0, (size.height - _yc) * 2 );
196
197
window.x = MAX( 0, _xc - window.width / 2 );
198
window.y = MAX( 0, _yc - window.height / 2 );
199
200
window.width = MIN( size.width - window.x, window.width );
201
window.height = MIN( size.height - window.y, window.height );
202
203
RotatedRect box;
204
box.size.height = (float)length;
205
box.size.width = (float)width;
206
box.angle = (float)((CV_PI*0.5+theta)*180./CV_PI);
207
while(box.angle < 0)
208
box.angle += 360;
209
while(box.angle >= 360)
210
box.angle -= 360;
211
if(box.angle >= 180)
212
box.angle -= 180;
213
box.center = Point2f( window.x + window.width*0.5f, window.y + window.height*0.5f);
214
215
return box;
216
}
217
218
/* End of file. */
219
220