forked from lbl1985/noseTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.h
More file actions
246 lines (221 loc) · 8.34 KB
/
Copy pathstate.h
File metadata and controls
246 lines (221 loc) · 8.34 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/** @file
* Rotated rectangle state particle filter +
* 1nd order AR dynamics model ( in fact, next = current + noise )
*
* Use this file as a template of definitions of states and
* state transition model for particle filter
*
* Currently cvparticle.h supports only linear combination of states transition
* model only. you may create another cvParticleTransition to support more complex
* non-linear state transition model. Most of other functions still should be
* available modifications
*
* The MIT License
*
* Copyright (c) 2008, Naotoshi Seo <sonots(at)sonots.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef CV_PARTICLE_ROTRECT_H
#define CV_PARTICLE_ROTRECT_H
#include "opencvx/cvparticle.h"
#include "opencvx/cvdrawrectangle.h"
#include "opencvx/cvcropimageroi.h"
#include "opencvx/cvrect32f.h"
#include <float.h>
//using namespace std;
/********************** Definition of a particle *****************************/
int num_states = 5;
// Definition of meanings of 5 states.
// This kinds of structures is not necessary to be defined,
// but I recommend to define them because it makes clear meanings of states
typedef struct CvParticleState {
double x; // center coord of a rectangle
double y; // center coord of a rectangle
double width; // width of a rectangle
double height; // height of a rectangle
double angle; // rotation around center. degree
} CvParticleState;
// Definition of dynamics model
// new_particle = cvMatMul( dynamics, particle ) + noise
// curr_x =: curr_x + noise
double dynamics[] = {
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1,
};
/********************** Function Prototypes *********************************/
// Functions for CvParticleState structure ( constructor, getter, setter )
inline CvParticleState cvParticleState( double x,
double y,
double width,
double height,
double angle = 0 );
CvParticleState cvParticleStateFromMat( const CvMat* state );
void cvParticleStateToMat( const CvParticleState &state, CvMat* state_mat );
CvParticleState cvParticleStateGet( const CvParticle* p, int p_id );
void cvParticleStateSet( CvParticle* p, int p_id, const CvParticleState &state );
// Particle Filter configuration
void cvParticleStateConfig( CvParticle* p, CvSize imsize, CvParticleState& std );
void cvParticleStateAdditionalBound( CvParticle* p, CvSize imsize );
// Utility Functions
void cvParticleStateDisplay( const CvParticleState& state, IplImage* frame, CvScalar color );
void cvParticleStatePrint( const CvParticleState& state );
/****************** Functions for CvParticleState structure ******************/
// This kinds of state definitions are not necessary,
// but helps readability of codes for sure.
/**
* Constructor
*/
inline CvParticleState cvParticleState( double x,
double y,
double width,
double height,
double angle )
{
CvParticleState state = { x, y, width, height, angle };
return state;
}
/**
* Convert a matrix state representation to a state structure
*
* @param state num_states x 1 matrix
*/
CvParticleState cvParticleStateFromMat( const CvMat* state )
{
CvParticleState s;
s.x = cvmGet( state, 0, 0 );
s.y = cvmGet( state, 1, 0 );
s.width = cvmGet( state, 2, 0 );
s.height = cvmGet( state, 3, 0 );
s.angle = cvmGet( state, 4, 0 );
return s;
}
/**
* Convert a state structure to CvMat
*
* @param state A CvParticleState structure
* @param state_mat num_states x 1 matrix
* @return void
*/
void cvParticleStateToMat( const CvParticleState& state, CvMat* state_mat )
{
cvmSet( state_mat, 0, 0, state.x );
cvmSet( state_mat, 1, 0, state.y );
cvmSet( state_mat, 2, 0, state.width );
cvmSet( state_mat, 3, 0, state.height );
cvmSet( state_mat, 4, 0, state.angle );
}
/**
* Get a state from a particle filter structure
*
* @param p particle filter struct
* @param p_id particle id
*/
CvParticleState cvParticleStateGet( const CvParticle* p, int p_id )
{
CvMat* state, hdr;
state = cvGetCol( p->particles, &hdr, p_id );
return cvParticleStateFromMat( state );
}
/**
* Set a state to a particle filter structure
*
* @param state A CvParticleState structure
* @param p particle filter struct
* @param p_id particle id
* @return void
*/
void cvParticleStateSet( CvParticle* p, int p_id, const CvParticleState& state )
{
CvMat* state_mat, hdr;
state_mat = cvGetCol( p->particles, &hdr, p_id );
cvParticleStateToMat( state, state_mat );
}
/*************************** Particle Filter Configuration *********************************/
/**
* Configuration of Particle filter
*/
void cvParticleStateConfig( CvParticle* p, CvSize imsize, CvParticleState& std )
{
// config dynamics model
CvMat dynamicsmat = cvMat( p->num_states, p->num_states, CV_64FC1, dynamics );
// config random noise standard deviation
CvRNG rng = cvRNG( time( NULL ) );
double stdarr[] = {
std.x,
std.y,
std.width,
std.height,
std.angle
};
CvMat stdmat = cvMat( p->num_states, 1, CV_64FC1, stdarr );
// config minimum and maximum values of states
// lowerbound, upperbound, circular flag (useful for degree)
// lowerbound == upperbound to express no bounding
double boundarr[] = {
0, imsize.width - 1, false,
0, imsize.height - 1, false,
1, imsize.width, false,
1, imsize.height, false,
0, 360, true
};
CvMat boundmat = cvMat( p->num_states, 3, CV_64FC1, boundarr );
cvParticleSetDynamics( p, &dynamicsmat );
cvParticleSetNoise( p, rng, &stdmat );
cvParticleSetBound( p, &boundmat );
}
/**
* @todo
* CvParticle does not support this type of bounding currently
* Call after transition
*/
void cvParticleStateAdditionalBound( CvParticle* p, CvSize imsize )
{
for( int np = 0; np < p->num_particles; np++ )
{
double x = cvmGet( p->particles, 0, np );
double y = cvmGet( p->particles, 1, np );
double width = cvmGet( p->particles, 2, np );
double height = cvmGet( p->particles, 3, np );
width = MIN( width, imsize.width - x ); // another state x is used
height = MIN( height, imsize.height - y ); // another state y is used
cvmSet( p->particles, 2, np, width );
cvmSet( p->particles, 3, np, height );
}
}
/***************************** Utility Functions ****************************************/
void cvParticleStateDisplay( const CvParticleState& state, IplImage* img, CvScalar color )
{
CvBox32f box32f = cvBox32f( state.x, state.y, state.width, state.height, state.angle );
CvRect32f rect32f = cvRect32fFromBox32f( box32f );
cvDrawRectangle( img, rect32f, cvPoint2D32f(0,0), color );
}
void cvParticleStatePrint( const CvParticleState& state )
{
printf( "x :%.2f ", state.x );
printf( "y :%.2f ", state.y );
printf( "width :%.2f ", state.width );
printf( "height :%.2f ", state.height );
printf( "angle :%.2f\n", state.angle );
fflush( stdout );
}
#endif