-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.cpp
479 lines (355 loc) · 11.9 KB
/
main.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
Copyright (C) 2013, Gurpinder Singh Sandhu, all rights reserved.
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.
* The name of the copyright holders may not 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 Gurpinder Singh Sandhu 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.*/
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <math.h>
#include <conio.h>
cv::Mat kernel;
float kernel_sum = 0.0;
int count=0;
float pi1 = 3.142;
int total_iter = 0;
int frame_count = 0;
int model = 0;
float alpha = 0.99;
int no_of_bins = 16;
int range = 256;
int bin_width = cvRound(float(range)/float(no_of_bins));
cv::Rect box,next_box;
bool drawing_box = false;
bool selected = false;
void reduce_color_space(cv::Mat &input_image,cv::Mat &output_image,int div_factor);
void create_kernel(cv:: Mat &kernel);
cv::Mat kernel_on_patch(cv::Mat &input_image,int *patch_centre,cv::Mat input_kernel);
cv::Mat index_function(cv::Mat &input_image,int no_of_bins);
cv::Mat create_target_model(cv::Mat &input_image,int *patch_centre,cv::Mat &input_kernel,int no_of_bins);
cv::Mat detect_object(cv::Mat &input_image,cv::Rect &box);
cv::Mat assign_weight(cv::Mat &input_image,cv::Mat &target_model,cv::Mat &target_candidate,cv::Rect &box);
int check_bin_for_pixel(int pixel, int no_of_bins, int range);
float calc_bhattacharya(cv::Mat &target_model,cv::Mat &target_candidate);
void create_mouse_callback(int event,int x,int y,int flag,void* param);
void main()
{
cv::Mat orig_img,temp_img,target_model,target_candidate,weight;
int curr_pos[2] = {0,0};
int next_pos[2] = {0,0};
cv::VideoCapture start_capture;
start_capture = cv::VideoCapture("tracking_video.avi");
for(int i = 0; i<5; i++)
start_capture.read(orig_img);
//imshow("test",orig_img);
int patch_centre[2] = {0,0};
int patch_centre1[2] = {0,0};
cv::namedWindow("original image");
temp_img = orig_img.clone();
cv::setMouseCallback("original image",create_mouse_callback,(void*) &temp_img);
cv::imshow("original image",orig_img);
while(selected == false)
{
cv::Mat temp;
temp_img.copyTo(temp);
if( drawing_box )
cv::rectangle( temp, box,cv::Scalar(0),2);
cv::imshow("original image", temp );
if( cv::waitKey( 15 )==27 )
break;
}
if(box.width%2==0)
box.width++;
if(box.height%2==0)
box.height++;
//cv::imshow("gp",orig_img);
cv::waitKey(0);
target_model = detect_object(orig_img,box);
cv::waitKey(0);
while(1)
{
count++;
if(!start_capture.read(orig_img))
break;
//start_capture.read(orig_img);
//start_capture.read(orig_img);
frame_count++;
/*if(frame_count == 10)
{
frame_count = 0;
target_model = target_candidate.clone();
}*/
for(int k=0;k<20;k++)
{
target_candidate = detect_object(orig_img,box);
weight = assign_weight(orig_img,target_model,target_candidate,box);
float num_x = 0.0;
float den = 0.0;
float num_y = 0.0;
float centre = static_cast<float>((weight.rows-1)/2.0);
double mult = 0.0;
float norm_i = 0.0;
float norm_j = 0.0;
next_box.x = box.x;
next_box.y = box.y;
next_box.width = box.width;
next_box.height = box.height;
for(int i=0;i<weight.rows;i++)
{
for(int j=0;j<weight.cols;j++)
{
norm_i = static_cast<float>(i-centre)/centre;
norm_j = static_cast<float>(j-centre)/centre;
mult = pow(norm_i,2)+pow(norm_j,2)>1.0?0.0:1.0;
num_x += static_cast<float>(alpha*norm_j*weight.at<float>(i,j)*mult);
num_y += static_cast<float>(alpha*norm_i*weight.at<float>(i,j)*mult);
den += static_cast<float>(weight.at<float>(i,j)*mult);
}
}
next_box.x += static_cast<int>((num_x/den)*centre);
next_box.y += static_cast<int>((num_y/den)*centre);
//std::cout << "\n" << k;
if(abs(next_box.x-box.x)<1 && abs(next_box.y-box.y)<1)
{
//std::cout <<"\n \n success\n" << k;
total_iter += k;
break;
}
else
{
box.x = next_box.x;
box.y = next_box.y;
}
if(box.x + box.width >= orig_img.cols || box.x <= 0 || box.y + box.height >= orig_img.rows || box.y <= 0)
{
_getch();
}
}
float dist = 0.0;
dist = calc_bhattacharya(target_model,target_candidate);
//std::cout << '\n' << "Bhattacharya Distance : " << dist;
if(dist < 0.6 && frame_count > 10)
{
//target_model = target_candidate.clone();
frame_count = 0;
//std::cout << "gp";
}
cv::rectangle(orig_img,box,cv::Scalar(0));
cv::imshow("Tracking",orig_img);
cv::waitKey(5);
}
cv::waitKey(0);
}
void reduce_color_space(cv::Mat &input_image,cv::Mat &output_image,int div_factor=64)
{
output_image = input_image.clone();
int no_rows = input_image.rows;
int no_cols = input_image.cols*input_image.channels();
if(input_image.isContinuous())
{
no_cols = no_rows*no_cols;
no_rows = 1;
}
int n = static_cast<int>(log(static_cast<double>(div_factor))/log(2.0));
uchar mask = 0xFF<<n;
for(int i=0;i<no_rows;i++)
{
uchar* data = output_image.ptr<uchar>(i);
for(int j=0;j<no_cols;j++)
{
*data++ = *data&mask + div_factor/2;
}
}
}
void create_kernel(cv::Mat &kernel)
{
int ck_no_rows = kernel.rows;
int ck_no_cols = kernel.cols;
float ck_centre[2] = {float((ck_no_cols-1)/2), float((ck_no_rows-1)/2)};
kernel_sum = 0.0;
float parameter_cd = 0.1*pi1*ck_no_rows*ck_no_rows;
std::cout << '\n' << parameter_cd;
for(int i=0;i<ck_no_rows;i++)
{
for(int j=0;j<ck_no_cols;j++)
{
float x = (abs(i-ck_centre[0]));
float y = (abs(j-ck_centre[1]));
float n = static_cast<float>(parameter_cd*(1.0-((x*x+y*y)/(ck_centre[0]*ck_centre[0]))));
float m = n<0?0:n;
kernel.at<float>(i,j) = m;
}
}
//cv::normalize(kernel, kernel,1.0,0.0);
for(int i=0;i<ck_no_rows;i++)
{
for(int j=0;j<ck_no_cols;j++)
{
kernel_sum += kernel.at<float>(i,j);
}
}
}
cv::Mat create_target_model(cv::Mat &input_image, int *patch_centre,cv::Mat &input_kernel,int no_of_bins)
{
//const int size[3] = {no_of_bins,1};
cv::Mat target_model(3,no_of_bins,CV_32F,cv::Scalar(1e-10));
int no_of_channels = input_image.channels();
int ctm_no_cols = input_kernel.cols;
int ctm_no_rows = input_kernel.rows;
cv::Vec3f curr_pixel_value;
cv::Vec3f bin_value;
int x_img = patch_centre[0]-(ctm_no_cols-1)/2;
int y_img = patch_centre[1]-(ctm_no_rows-1)/2;
//for(int i=0;i<target_model.rows;i++)
// target_model.at<float>(i,0) = 1e-10;
/*std::cout << '\n';
for(int i=0;i<bgr_planes[0].rows;i++)
for(int j=0;j<bgr_planes[0].cols;j++)
{
std::cout << static_cast<int>(bgr_planes[0].at<uchar>(i,j)) << '\t';
}
*/
x_img = patch_centre[0]-(ctm_no_cols-1)/2;
for(int i=0;i<ctm_no_rows;i++)
{
y_img = patch_centre[1]-(ctm_no_rows-1)/2;
for(int j=0;j<ctm_no_rows;j++)
{
curr_pixel_value = input_image.at<cv::Vec3b>(x_img,y_img);
bin_value[0] = (curr_pixel_value[0]/bin_width);
bin_value[1] = (curr_pixel_value[1]/bin_width);
bin_value[2] = (curr_pixel_value[2]/bin_width);
target_model.at<float>(0,bin_value[0]) += input_kernel.at<float>(i,j)/(kernel_sum);
target_model.at<float>(1,bin_value[1]) += input_kernel.at<float>(i,j)/(kernel_sum);
target_model.at<float>(2,bin_value[2]) += input_kernel.at<float>(i,j)/(kernel_sum);
y_img++;
}
x_img++;
}
return target_model;
}
void create_mouse_callback(int event,int x,int y,int flag,void* param)
{
cv::Mat *image = (cv::Mat*) param;
switch( event ){
case CV_EVENT_MOUSEMOVE:
if( drawing_box ){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = cv::Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
cv::rectangle(*image,box,cv::Scalar(0),2);
selected = true;
break;
}
}
cv::Mat detect_object(cv::Mat &input_image,cv::Rect &box)
{
int kernel_size = box.height>box.width?box.width:box.height;
cv::Mat kernel_for_now(kernel_size,kernel_size,CV_32F,cv::Scalar(0));
create_kernel(kernel_for_now);
int patch_centre[2] = {box.y+box.height/2,box.x+box.width/2};
cv::Mat target_model = create_target_model(input_image,patch_centre,kernel_for_now,16);
return target_model;
}
cv::Mat assign_weight(cv::Mat &input_image,cv::Mat &target_model,cv::Mat &target_candidate,cv::Rect &box)
{
int aw_no_of_rows = box.height>box.width?box.width:box.height;
int aw_no_of_cols = box.height>box.width?box.width:box.height;
cv::Mat weight(aw_no_of_rows,aw_no_of_cols,CV_32F,cv::Scalar(1.0000));
int bin_width = cvRound(float(range)/float(no_of_bins));
std::vector<cv::Mat> bgr_planes;
split(input_image, bgr_planes);
//for(int i = 0; i < weight.rows; i++)
// for(int j = 0; j < weight.cols; j++)
// {
// weight.at<float>(i,j) = 1.0000;
// }
int i_img = box.y;
int j_img = box.x;
int curr_pixel = 0;
int bin_value = 0;
for(int k = 0; k < 3; k++)
{
i_img = box.y;
for(int i=0;i<aw_no_of_rows;i++)
{
j_img = box.x;
for(int j=0;j<aw_no_of_cols;j++)
{
curr_pixel = static_cast<int>(bgr_planes[k].at<uchar>(i_img,j_img));
//bin_value = check_bin_for_pixel(curr_pixel,no_of_bins,range);
bin_value = curr_pixel/bin_width;
weight.at<float>(i,j) *= static_cast<float>((sqrt(target_model.at<float>(k, bin_value)/target_candidate.at<float>(k, bin_value))));
j_img++;
}
i_img++;
}
}
return weight;
}
int check_bin_for_pixel(int pixel, int no_of_bins, int range)
{
int bin_value = 0;
int bin_width = cvRound(float(range)/float(no_of_bins));
if(pixel >= range)
{
return no_of_bins+1;
}
for(int i = 0; i < no_of_bins; i++)
{
if(pixel >= bin_width*i && pixel < bin_width*(i+1))
{
bin_value = i;
break;
}
}
return bin_value;
}
float calc_bhattacharya(cv::Mat &target_model,cv::Mat &target_candidate)
{
float p_bar = 0.0;
float dist = 0.0;
for(int i = 0; i < target_model.rows; i++)
{
for(int j=0; j < target_model.cols; j++)
p_bar += sqrt((target_candidate.at<float>(i,j))*(target_model.at<float>(i,j)));
}
dist = sqrt(1-p_bar);
return dist;
}