forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVIBE.cpp
More file actions
293 lines (278 loc) · 6.65 KB
/
VIBE.cpp
File metadata and controls
293 lines (278 loc) · 6.65 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
289
290
291
292
293
#include "VIBE.h"
void initRnd(unsigned int size)
{
unsigned int seed = time(0);
srand(seed);
rndSize=size;
rnd=(unsigned int*)calloc(rndSize, sizeof(unsigned int));
for(unsigned int i=0; i < rndSize; i++)
{
rnd[i]=rand();
}
rndPos=0;
}
unsigned int freeRnd()
{
free(rnd);
rndSize=0;
return 0;
}
vibeModel *libvibeModelNew()
{
vibeModel *model = (vibeModel*)calloc(1, sizeof(vibeModel));
if(model)
{
model->numberOfSamples = 20;
model->matchingThreshold = 20;
model->matchingNumber = 2;
model->updateFactor = 16;
initRnd(65536);
}
return model;
}
unsigned char getRandPixel(const unsigned char *image_data, const unsigned int width, const unsigned int height, const unsigned int stride, const unsigned int x, const unsigned int y)
{
unsigned int neighborRange=1;
int dx;
int dy;
dx = (x-neighborRange) + rnd[rndPos=(rndPos+1)%rndSize]%(2*neighborRange);
dy = (y-neighborRange) + rnd[rndPos=(rndPos+1)%rndSize]%(2*neighborRange);
if((dx<0)||(dx>=width))
{
if(dx<0)
{
dx = rnd[rndPos=(rndPos+1)%rndSize]%(x+neighborRange);
}
else
{
dx = (x-neighborRange) + rnd[rndPos=(rndPos+1)%rndSize]%(width - x + neighborRange-1);
}
}
if((dy<0)||(dy>=height))
{
if(dy<0)
{
dy = rnd[rndPos=(rndPos+1)%rndSize]%(y+neighborRange);
}
else
{
dy = (y-neighborRange) + rnd[rndPos=(rndPos+1)%rndSize]%(height - y + neighborRange-1);
}
}
return image_data[dx+dy*stride];
}
int libvibeModelInit(vibeModel *model, const unsigned char *image_data, const unsigned int width, const unsigned int height, const unsigned int stride)
{
if (!model || !image_data || !width || !height || !stride || (stride<width)) return 1;
// Store frame size
model->width = width;
model->height = height;
model->stride = stride;
// Create a model for each pixel
model->pixels = 0;
model->pixels = (pixel*)calloc(model->width*model->height, sizeof(pixel));
if (!model->pixels) return 1;
// Allocate memory for each model N samples
for (unsigned int i=0; i < model->width*model->height; i++)
{
model->pixels[i].numberOfSamples=model->numberOfSamples;
model->pixels[i].sizeOfSample = 1;
model->pixels[i].samples = 0;
model->pixels[i].samples = (unsigned char*)calloc(model->numberOfSamples,sizeof(unsigned char));
if (!model->pixels[i].samples) return 1;
}
// Fill the model
// We need to fill samples.
// One of samples written with pixel value,
// others filled with random neighbour pixels values.
unsigned int n=0;
for (unsigned int j=0; j < model->height; j++)
{
for (unsigned int i=0; i < model->width; i++)
{
model->pixels[n].samples[0] = image_data[i+j*stride];
for (unsigned int k=1; k < model->numberOfSamples; k++)
model->pixels[n].samples[k] = getRandPixel(image_data, width, height, stride, i, j);
n++;
}
}
return 0;
}
int libvibeModelUpdate(vibeModel *model, const unsigned char *image_data, unsigned char *segmentation_map)
{
int ad = model->stride - model->width;
if (!model || !image_data || !segmentation_map) return 1;
if (model->stride < model->width) return 1;
unsigned int n=0;
for (int j=0; j < model->height; j++)
{
for (int i=0; i < model->width; i++)
{
/****************************************************************/
/**********************Compare pixels****************************/
/****************************************************************/
bool flag=false;
unsigned int matchingCounter=0;
// Compare with every sample
for(unsigned int t=0; t<model->pixels[n].numberOfSamples; t++)
{
if (abs((int)image_data[n]-(int)model->pixels[n].samples[t]) < model->matchingThreshold)
{
// If the difference less than threshold value for number of samples MatchingNumber,
// then assume, that there is no difference with background
matchingCounter++;
if (matchingCounter >= model->matchingNumber)
{
flag=true;
break;
}
}
}
/****************************************************************/
/****************************************************************/
/****************************************************************/
if(flag)
{
// matches background - update modet for this point
segmentation_map[n] = 0;
/****************************************************************/
/**********************Model update**************************/
/****************************************************************/
if(rnd[rndPos=(rndPos+1)%rndSize]%model->updateFactor)
{
model->pixels[i+model->width*j].samples[rnd[rndPos=(rndPos+1)%rndSize]%model->numberOfSamples]=image_data[n];
unsigned int m = (model->stride * j + i);
switch((rnd[rndPos=(rndPos+1)%rndSize])%8)
{
case 0:
if ((model->width - 1) <= i)
{
}
else
{
m++;
}
break;
case 1:
if ((model->width - 1) <= i)
{
}
else
{
m++;
}
if ((model->height - 1) <= j)
{
}
else
{
m += model->stride;
}
break;
case 2:
if ((model->height - 1) <= j)
{
}
else
{
m += model->stride;
}
break;
case 3:
if (i <= 0)
{
}
else
{
m--;
}
if ((model->height - 1) <= j)
{
}
else
{
m += model->stride;
}
break;
case 4:
if (i <= 0)
{
}
else
{
m--;
}
break;
case 5:
if (i <= 0)
{
}
else
{
m--;
}
if (j <= 0)
{
}
else
{
m -= model->stride;
}
break;
case 6:
if (j <= 0)
{
}
else
{
m -= model->stride;
}
break;
case 7:
if ((model->width - 1) <= i)
{
}
else
{
m++;
}
if (j <= 0)
{
}
else
{
m -= model->stride;
}
break;
default:
puts("You should not see this message!!!");
break;
}
model->pixels[m].samples[rnd[rndPos=(rndPos+1)%rndSize]%model->numberOfSamples]=image_data[n];
/****************************************************************/
/****************************************************************/
/****************************************************************/
}
}
else
{
// background difference
segmentation_map[n] = 255;
}
n++;
}
if (model->stride > model->width)
n+=ad;
}
return 0;
}
int libvibeModelFree(vibeModel *model)
{
for(unsigned int i=0; i<model->width*model->height; i++)
{
free(model->pixels[i].samples);
}
free(model->pixels);
freeRnd();
return 0;
}