-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathac_withoutedges_yuv.cpp
More file actions
executable file
·288 lines (242 loc) · 8.32 KB
/
ac_withoutedges_yuv.cpp
File metadata and controls
executable file
·288 lines (242 loc) · 8.32 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/****************************************************************************
**
** Copyright (C) 2010-2012 Fabien Bessy.
** Contact: fabien.bessy@gmail.com
**
** This file is part of project Ofeli.
**
** http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
** You may use this file under the terms of the CeCILL license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Fabien Bessy and its Subsidiary(-ies) nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
**
****************************************************************************/
// comment/uncomment below, respectively,
// if you want to test the algorithm with a RGB interleaved/planar image data buffer
//#define RGB_PLANAR_IMAGE_DATA_BUFFER
// function affected by the define : void RGB_from_buffer(int offset)
#include "ac_withoutedges_yuv.hpp"
namespace ofeli
{
ACwithoutEdgesYUV::ACwithoutEdgesYUV(const unsigned char* img_rgb_data1, int img_width1, int img_height1) :
ActiveContour(img_rgb_data1, img_width1, img_height1,
true, 0.65, 0.65, 0.0, 0.0, true, 5, 2.0, 30, 3),
lambda_out(1), lambda_in(1), alpha(1), beta(10), gamma(10)
{
// a virtual function call in a constructor
// but the virtual function table is solved at this level of the class hierarchy
initialize_for_each_frame();
}
ACwithoutEdgesYUV::ACwithoutEdgesYUV(const unsigned char* img_rgb_data1, int img_width1, int img_height1,
bool hasEllipse1, double init_width1, double init_height1, double center_x1, double center_y1,
bool hasCycle2_1, int kernel_length1, double sigma1, int Na1, int Ns1,
int lambda_out1, int lambda_in1, int alpha1, int beta1, int gamma1) :
ActiveContour(img_rgb_data1, img_width1, img_height1,
hasEllipse1, init_width1, init_height1, center_x1, center_y1,
hasCycle2_1, kernel_length1, sigma1, Na1, Ns1),
lambda_out(lambda_out1), lambda_in(lambda_in1), alpha(alpha1), beta(beta1), gamma(gamma1)
{
// a virtual function call in a constructor
// but the virtual function table is solved at this level of the class hierarchy
initialize_for_each_frame();
}
ACwithoutEdgesYUV::ACwithoutEdgesYUV(const unsigned char* img_rgb_data1, int img_width1, int img_height1,
const char* phi_init1,
bool hasCycle2_1, int kernel_length1, double sigma1, int Na1, int Ns1,
int lambda_out1, int lambda_in1, int alpha1, int beta1, int gamma1) :
ActiveContour(img_rgb_data1, img_width1, img_height1,
phi_init1,
hasCycle2_1, kernel_length1, sigma1, Na1, Ns1),
lambda_out(lambda_out1), lambda_in(lambda_in1), alpha(alpha1), beta(beta1), gamma(gamma1)
{
// a virtual function call in a constructor
// but the virtual function table is solved at this level of the class hierarchy
initialize_for_each_frame();
}
ACwithoutEdgesYUV::ACwithoutEdgesYUV(const ACwithoutEdgesYUV& ac) :
ActiveContour(ac),
lambda_out(ac.lambda_out), lambda_in(ac.lambda_in), alpha(ac.alpha), beta(ac.beta), gamma(ac.gamma),
CoutR(ac.CoutR), CoutG(ac.CoutG), CoutB(ac.CoutB),
CinR(ac.CinR), CinG(ac.CinG), CinB(ac.CinB),
sum_out_R(ac.sum_out_R), sum_in_R(ac.sum_in_R), sum_out_G(ac.sum_out_G),
sum_in_G(ac.sum_in_G), sum_out_B(ac.sum_out_B), sum_in_B(ac.sum_in_B),
n_out(ac.n_out), n_in(ac.n_in), R(ac.R), G(ac.G), B(ac.B)
{
for( int index = 0; index < 3; index++ )
{
CoutYUV[index] = ac.CoutYUV[index];
CinYUV[index] = ac.CinYUV[index];
}
}
void ACwithoutEdgesYUV::initialize_for_each_frame()
{
int it = get_iteration();
ActiveContour::initialize_for_each_frame();
// condition for video tracking :
// If the active contour moves enough ( >120 ), variables sums, n_in, n_out
// are too different and are recalculated.
// If the active contour does not evolve ( < 5 because a last cycle 2 is performed )
// in the case of uniform images (or during the object initialization),
// variables are (re)calculated for convergence.
// Otherwise, variables are not calculated.
if( it > 120 || it < 5 )
{
initialize_sums();
}
calculate_means();
return;
}
void ACwithoutEdgesYUV::initialize_sums()
{
sum_in_R = 0;
sum_out_R = 0;
sum_in_G = 0;
sum_out_G = 0;
sum_in_B = 0;
sum_out_B = 0;
n_in = 0;
n_out = 0;
for( int offset = 0; offset < img_size; offset++ )
{
RGB_from_buffer(offset);
if( phi[offset] > 0 )
{
sum_out_R += R;
sum_out_G += G;
sum_out_B += B;
n_out++;
}
else
{
sum_in_R += R;
sum_in_G += G;
sum_in_B += B;
n_in++;
}
}
return;
}
void ACwithoutEdgesYUV::calculate_means()
{
if( n_out != 0 )
{
CoutR = sum_out_R/n_out;
CoutG = sum_out_G/n_out;
CoutB = sum_out_B/n_out;
}
if( n_in != 0 )
{
CinR = sum_in_R/n_in;
CinG = sum_in_G/n_in;
CinB = sum_in_B/n_in;
}
calculate_YUV(CoutR, CoutG, CoutB, CoutYUV);
calculate_YUV(CinR, CinG, CinB, CinYUV);
return;
}
int ACwithoutEdgesYUV::compute_external_speed_Fd(int offset)
{
RGB_from_buffer(offset);
int YUV[3];
calculate_YUV(R, G, B, YUV);
return lambda_out*( alpha*square(YUV[0]-CoutYUV[0])
+ beta*square(YUV[1]-CoutYUV[1])
+ gamma*square(YUV[2]-CoutYUV[2]) )
- lambda_in*( alpha*square(YUV[0]-CinYUV[0])
+ beta*square(YUV[1]-CinYUV[1])
+ gamma*square(YUV[2]-CinYUV[2]) );
}
void ACwithoutEdgesYUV::updates_for_means_in1()
{
sum_out_R -= R;
sum_out_G -= G;
sum_out_B -= B;
n_out--;
sum_in_R += R;
sum_in_G += G;
sum_in_B += B;
n_in++;
return;
}
void ACwithoutEdgesYUV::updates_for_means_out1()
{
sum_in_R -= R;
sum_in_G -= G;
sum_in_B -= B;
n_in--;
sum_out_R += R;
sum_out_G += G;
sum_out_B += B;
n_out++;
return;
}
void ACwithoutEdgesYUV::updates_for_means_in2(int offset)
{
RGB_from_buffer(offset);
updates_for_means_in1();
return;
}
void ACwithoutEdgesYUV::updates_for_means_out2(int offset)
{
RGB_from_buffer(offset);
updates_for_means_out1();
return;
}
int ACwithoutEdgesYUV::get_CoutR() const
{
return CoutR;
}
int ACwithoutEdgesYUV::get_CoutG() const
{
return CoutG;
}
int ACwithoutEdgesYUV::get_CoutB() const
{
return CoutB;
}
int ACwithoutEdgesYUV::get_CinR() const
{
return CinR;
}
int ACwithoutEdgesYUV::get_CinG() const
{
return CinG;
}
int ACwithoutEdgesYUV::get_CinB() const
{
return CinB;
}
const int* ACwithoutEdgesYUV::get_CoutYUV() const
{
return CoutYUV;
}
const int* ACwithoutEdgesYUV::get_CinYUV() const
{
return CinYUV;
}
}