6 void KCF_Tracker::init(cv::Mat &img, const cv::Rect & bbox)
8 //check boundary, enforce min size
9 double x1 = bbox.x, x2 = bbox.x + bbox.width, y1 = bbox.y, y2 = bbox.y + bbox.height;
11 if (x2 > img.cols-1) x2 = img.cols - 1;
13 if (y2 > img.rows-1) y2 = img.rows - 1;
15 if (x2-x1 < 2*p_cell_size) {
16 double diff = (2*p_cell_size -x2+x1)/2.;
17 if (x1 - diff >= 0 && x2 + diff < img.cols){
20 } else if (x1 - 2*diff >= 0) {
26 if (y2-y1 < 2*p_cell_size) {
27 double diff = (2*p_cell_size -y2+y1)/2.;
28 if (y1 - diff >= 0 && y2 + diff < img.rows){
31 } else if (y1 - 2*diff >= 0) {
40 p_pose.cx = x1 + p_pose.w/2.;
41 p_pose.cy = y1 + p_pose.h/2.;
43 cv::Mat input_gray, input_rgb = img.clone();
44 if (img.channels() == 3){
45 cv::cvtColor(img, input_gray, CV_BGR2GRAY);
46 input_gray.convertTo(input_gray, CV_32FC1);
48 img.convertTo(input_gray, CV_32FC1);
50 // don't need too large image
51 if (p_pose.w * p_pose.h > 100.*100.) {
52 std::cout << "resizing image by factor of 2" << std::endl;
53 p_resize_image = true;
55 cv::resize(input_gray, input_gray, cv::Size(0,0), 0.5, 0.5, cv::INTER_AREA);
56 cv::resize(input_rgb, input_rgb, cv::Size(0,0), 0.5, 0.5, cv::INTER_AREA);
59 //compute win size + fit to fhog cell size
60 p_windows_size[0] = round(p_pose.w * (1. + p_padding) / p_cell_size) * p_cell_size;
61 p_windows_size[1] = round(p_pose.h * (1. + p_padding) / p_cell_size) * p_cell_size;
65 for (int i = -p_num_scales/2; i <= p_num_scales/2; ++i)
66 p_scales.push_back(std::pow(p_scale_step, i));
68 p_scales.push_back(1.);
72 double min_size_ratio = std::max(5.*p_cell_size/p_windows_size[0], 5.*p_cell_size/p_windows_size[1]);
73 double max_size_ratio = std::min(floor((img.cols + p_windows_size[0]/3)/p_cell_size)*p_cell_size/p_windows_size[0], floor((img.rows + p_windows_size[1]/3)/p_cell_size)*p_cell_size/p_windows_size[1]);
74 p_min_max_scale[0] = std::pow(p_scale_step, std::ceil(std::log(min_size_ratio) / log(p_scale_step)));
75 p_min_max_scale[1] = std::pow(p_scale_step, std::floor(std::log(max_size_ratio) / log(p_scale_step)));
77 std::cout << "init: img size " << img.cols << " " << img.rows << std::endl;
78 std::cout << "init: win size. " << p_windows_size[0] << " " << p_windows_size[1] << std::endl;
79 std::cout << "init: min max scales factors: " << p_min_max_scale[0] << " " << p_min_max_scale[1] << std::endl;
81 p_output_sigma = std::sqrt(p_pose.w*p_pose.h) * p_output_sigma_factor / static_cast<double>(p_cell_size);
83 //window weights, i.e. labels
84 p_yf = fft2(gaussian_shaped_labels(p_output_sigma, p_windows_size[0]/p_cell_size, p_windows_size[1]/p_cell_size));
85 p_cos_window = cosine_window_function(p_yf.cols, p_yf.rows);
87 //obtain a sub-window for training initial model
88 std::vector<cv::Mat> path_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1]);
89 p_model_xf = fft2(path_feat, p_cos_window);
91 if (m_use_linearkernel) {
92 ComplexMat xfconj = p_model_xf.conj();
93 p_model_alphaf_num = xfconj.mul(p_yf);
94 p_model_alphaf_den = (p_model_xf * xfconj).sum_over_channels();
96 //Kernel Ridge Regression, calculate alphas (in Fourier domain)
97 ComplexMat kf = gaussian_correlation(p_model_xf, p_model_xf, p_kernel_sigma, true);
98 p_model_alphaf_num = p_yf * kf;
99 p_model_alphaf_den = kf * (kf + p_lambda);
100 p_model_alphaf = p_model_alphaf_num / p_model_alphaf_den;
101 // p_model_alphaf = p_yf / (kf + p_lambda); //equation for fast training
105 void KCF_Tracker::setTrackerPose(BBox_c &bbox, cv::Mat & img)
107 init(img, bbox.get_rect());
110 void KCF_Tracker::updateTrackerPosition(BBox_c &bbox)
112 if (p_resize_image) {
123 BBox_c KCF_Tracker::getBBox()
126 tmp.w *= p_current_scale;
127 tmp.h *= p_current_scale;
135 void KCF_Tracker::track(cv::Mat &img)
137 cv::Mat input_gray, input_rgb = img.clone();
138 if (img.channels() == 3){
139 cv::cvtColor(img, input_gray, CV_BGR2GRAY);
140 input_gray.convertTo(input_gray, CV_32FC1);
142 img.convertTo(input_gray, CV_32FC1);
144 // don't need too large image
145 if (p_resize_image) {
146 cv::resize(input_gray, input_gray, cv::Size(0, 0), 0.5, 0.5, cv::INTER_AREA);
147 cv::resize(input_rgb, input_rgb, cv::Size(0, 0), 0.5, 0.5, cv::INTER_AREA);
150 std::vector<cv::Mat> patch_feat;
151 double max_response = -1.;
152 cv::Mat max_response_map;
153 cv::Point2i max_response_pt;
155 std::vector<double> scale_responses;
157 if (m_use_multithreading){
158 std::vector<std::future<cv::Mat>> async_res(p_scales.size());
159 for (size_t i = 0; i < p_scales.size(); ++i) {
160 async_res[i] = std::async(std::launch::async,
161 [this, &input_gray, &input_rgb, i]() -> cv::Mat
163 std::vector<cv::Mat> patch_feat_async = get_features(input_rgb, input_gray, this->p_pose.cx, this->p_pose.cy, this->p_windows_size[0],
164 this->p_windows_size[1], this->p_current_scale * this->p_scales[i]);
165 ComplexMat zf = fft2(patch_feat_async, this->p_cos_window);
166 if (m_use_linearkernel)
167 return ifft2((p_model_alphaf_num * zf).sum_over_channels() / (p_model_alphaf_den + p_lambda));
169 ComplexMat kzf = gaussian_correlation(zf, this->p_model_xf, this->p_kernel_sigma);
170 return ifft2(this->p_model_alphaf * kzf);
175 for (size_t i = 0; i < p_scales.size(); ++i) {
178 cv::Mat response = async_res[i].get();
180 double min_val, max_val;
181 cv::Point2i min_loc, max_loc;
182 cv::minMaxLoc(response, &min_val, &max_val, &min_loc, &max_loc);
184 double weight = p_scales[i] < 1. ? p_scales[i] : 1./p_scales[i];
185 if (max_val*weight > max_response) {
186 max_response = max_val*weight;
187 max_response_map = response;
188 max_response_pt = max_loc;
191 scale_responses.push_back(max_val*weight);
194 for (size_t i = 0; i < p_scales.size(); ++i) {
195 patch_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1], p_current_scale * p_scales[i]);
196 ComplexMat zf = fft2(patch_feat, p_cos_window);
198 if (m_use_linearkernel)
199 response = ifft2((p_model_alphaf_num * zf).sum_over_channels() / (p_model_alphaf_den + p_lambda));
201 ComplexMat kzf = gaussian_correlation(zf, p_model_xf, p_kernel_sigma);
202 response = ifft2(p_model_alphaf * kzf);
205 /* target location is at the maximum response. we must take into
206 account the fact that, if the target doesn't move, the peak
207 will appear at the top-left corner, not at the center (this is
208 discussed in the paper). the responses wrap around cyclically. */
209 double min_val, max_val;
210 cv::Point2i min_loc, max_loc;
211 cv::minMaxLoc(response, &min_val, &max_val, &min_loc, &max_loc);
213 double weight = p_scales[i] < 1. ? p_scales[i] : 1./p_scales[i];
214 if (max_val*weight > max_response) {
215 max_response = max_val*weight;
216 max_response_map = response;
217 max_response_pt = max_loc;
220 scale_responses.push_back(max_val*weight);
224 //sub pixel quadratic interpolation from neighbours
225 if (max_response_pt.y > max_response_map.rows / 2) //wrap around to negative half-space of vertical axis
226 max_response_pt.y = max_response_pt.y - max_response_map.rows;
227 if (max_response_pt.x > max_response_map.cols / 2) //same for horizontal axis
228 max_response_pt.x = max_response_pt.x - max_response_map.cols;
230 cv::Point2f new_location(max_response_pt.x, max_response_pt.y);
232 if (m_use_subpixel_localization)
233 new_location = sub_pixel_peak(max_response_pt, max_response_map);
235 p_pose.cx += p_current_scale*p_cell_size*new_location.x;
236 p_pose.cy += p_current_scale*p_cell_size*new_location.y;
237 if (p_pose.cx < 0) p_pose.cx = 0;
238 if (p_pose.cx > img.cols-1) p_pose.cx = img.cols-1;
239 if (p_pose.cy < 0) p_pose.cy = 0;
240 if (p_pose.cy > img.rows-1) p_pose.cy = img.rows-1;
242 //sub grid scale interpolation
243 double new_scale = p_scales[scale_index];
244 if (m_use_subgrid_scale)
245 new_scale = sub_grid_scale(scale_responses, scale_index);
247 p_current_scale *= new_scale;
249 if (p_current_scale < p_min_max_scale[0])
250 p_current_scale = p_min_max_scale[0];
251 if (p_current_scale > p_min_max_scale[1])
252 p_current_scale = p_min_max_scale[1];
254 //obtain a subwindow for training at newly estimated target position
255 patch_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1], p_current_scale);
256 ComplexMat xf = fft2(patch_feat, p_cos_window);
258 //subsequent frames, interpolate model
259 p_model_xf = p_model_xf * (1. - p_interp_factor) + xf * p_interp_factor;
261 ComplexMat alphaf_num, alphaf_den;
263 if (m_use_linearkernel) {
264 ComplexMat xfconj = xf.conj();
265 alphaf_num = xfconj.mul(p_yf);
266 alphaf_den = (xf * xfconj).sum_over_channels();
268 //Kernel Ridge Regression, calculate alphas (in Fourier domain)
269 ComplexMat kf = gaussian_correlation(xf, xf, p_kernel_sigma, true);
270 // ComplexMat alphaf = p_yf / (kf + p_lambda); //equation for fast training
271 // p_model_alphaf = p_model_alphaf * (1. - p_interp_factor) + alphaf * p_interp_factor;
272 alphaf_num = p_yf * kf;
273 alphaf_den = kf * (kf + p_lambda);
276 p_model_alphaf_num = p_model_alphaf_num * (1. - p_interp_factor) + alphaf_num * p_interp_factor;
277 p_model_alphaf_den = p_model_alphaf_den * (1. - p_interp_factor) + alphaf_den * p_interp_factor;
278 if (!m_use_linearkernel)
279 p_model_alphaf = p_model_alphaf_num / p_model_alphaf_den;
282 // ****************************************************************************
284 std::vector<cv::Mat> KCF_Tracker::get_features(cv::Mat & input_rgb, cv::Mat & input_gray, int cx, int cy, int size_x, int size_y, double scale)
286 int size_x_scaled = floor(size_x*scale);
287 int size_y_scaled = floor(size_y*scale);
289 cv::Mat patch_gray = get_subwindow(input_gray, cx, cy, size_x_scaled, size_y_scaled);
290 cv::Mat patch_rgb = get_subwindow(input_rgb, cx, cy, size_x_scaled, size_y_scaled);
292 //resize to default size
294 //if we downsample use INTER_AREA interpolation
295 cv::resize(patch_gray, patch_gray, cv::Size(size_x, size_y), 0., 0., cv::INTER_AREA);
297 cv::resize(patch_gray, patch_gray, cv::Size(size_x, size_y), 0., 0., cv::INTER_LINEAR);
301 std::vector<cv::Mat> hog_feat = FHoG::extract(patch_gray, 2, p_cell_size, 9);
303 //get color rgb features (simple r,g,b channels)
304 std::vector<cv::Mat> color_feat;
305 if ((m_use_color || m_use_cnfeat) && input_rgb.channels() == 3) {
306 //resize to default size
308 //if we downsample use INTER_AREA interpolation
309 cv::resize(patch_rgb, patch_rgb, cv::Size(size_x/p_cell_size, size_y/p_cell_size), 0., 0., cv::INTER_AREA);
311 cv::resize(patch_rgb, patch_rgb, cv::Size(size_x/p_cell_size, size_y/p_cell_size), 0., 0., cv::INTER_LINEAR);
315 if (m_use_color && input_rgb.channels() == 3) {
316 //use rgb color space
317 cv::Mat patch_rgb_norm;
318 patch_rgb.convertTo(patch_rgb_norm, CV_32F, 1. / 255., -0.5);
319 cv::Mat ch1(patch_rgb_norm.size(), CV_32FC1);
320 cv::Mat ch2(patch_rgb_norm.size(), CV_32FC1);
321 cv::Mat ch3(patch_rgb_norm.size(), CV_32FC1);
322 std::vector<cv::Mat> rgb = {ch1, ch2, ch3};
323 cv::split(patch_rgb_norm, rgb);
324 color_feat.insert(color_feat.end(), rgb.begin(), rgb.end());
327 if (m_use_cnfeat && input_rgb.channels() == 3) {
328 std::vector<cv::Mat> cn_feat = CNFeat::extract(patch_rgb);
329 color_feat.insert(color_feat.end(), cn_feat.begin(), cn_feat.end());
332 hog_feat.insert(hog_feat.end(), color_feat.begin(), color_feat.end());
336 cv::Mat KCF_Tracker::gaussian_shaped_labels(double sigma, int dim1, int dim2)
338 cv::Mat labels(dim2, dim1, CV_32FC1);
339 int range_y[2] = {-dim2 / 2, dim2 - dim2 / 2};
340 int range_x[2] = {-dim1 / 2, dim1 - dim1 / 2};
342 double sigma_s = sigma*sigma;
344 for (int y = range_y[0], j = 0; y < range_y[1]; ++y, ++j){
345 float * row_ptr = labels.ptr<float>(j);
347 for (int x = range_x[0], i = 0; x < range_x[1]; ++x, ++i){
348 row_ptr[i] = std::exp(-0.5 * (y_s + x*x) / sigma_s);
352 //rotate so that 1 is at top-left corner (see KCF paper for explanation)
353 cv::Mat rot_labels = circshift(labels, range_x[0], range_y[0]);
354 //sanity check, 1 at top left corner
355 assert(rot_labels.at<float>(0,0) >= 1.f - 1e-10f);
360 cv::Mat KCF_Tracker::circshift(const cv::Mat &patch, int x_rot, int y_rot)
362 cv::Mat rot_patch(patch.size(), CV_32FC1);
363 cv::Mat tmp_x_rot(patch.size(), CV_32FC1);
365 //circular rotate x-axis
367 //move part that does not rotate over the edge
368 cv::Range orig_range(-x_rot, patch.cols);
369 cv::Range rot_range(0, patch.cols - (-x_rot));
370 patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
373 orig_range = cv::Range(0, -x_rot);
374 rot_range = cv::Range(patch.cols - (-x_rot), patch.cols);
375 patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
376 }else if (x_rot > 0){
377 //move part that does not rotate over the edge
378 cv::Range orig_range(0, patch.cols - x_rot);
379 cv::Range rot_range(x_rot, patch.cols);
380 patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
383 orig_range = cv::Range(patch.cols - x_rot, patch.cols);
384 rot_range = cv::Range(0, x_rot);
385 patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
386 }else { //zero rotation
387 //move part that does not rotate over the edge
388 cv::Range orig_range(0, patch.cols);
389 cv::Range rot_range(0, patch.cols);
390 patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
393 //circular rotate y-axis
395 //move part that does not rotate over the edge
396 cv::Range orig_range(-y_rot, patch.rows);
397 cv::Range rot_range(0, patch.rows - (-y_rot));
398 tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
401 orig_range = cv::Range(0, -y_rot);
402 rot_range = cv::Range(patch.rows - (-y_rot), patch.rows);
403 tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
404 }else if (y_rot > 0){
405 //move part that does not rotate over the edge
406 cv::Range orig_range(0, patch.rows - y_rot);
407 cv::Range rot_range(y_rot, patch.rows);
408 tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
411 orig_range = cv::Range(patch.rows - y_rot, patch.rows);
412 rot_range = cv::Range(0, y_rot);
413 tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
414 }else { //zero rotation
415 //move part that does not rotate over the edge
416 cv::Range orig_range(0, patch.rows);
417 cv::Range rot_range(0, patch.rows);
418 tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
424 ComplexMat KCF_Tracker::fft2(const cv::Mat &input)
426 cv::Mat complex_result;
427 // cv::Mat padded; //expand input image to optimal size
428 // int m = cv::getOptimalDFTSize( input.rows );
429 // int n = cv::getOptimalDFTSize( input.cols ); // on the border add zero pixels
430 // copyMakeBorder(input, padded, 0, m - input.rows, 0, n - input.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
431 // cv::dft(padded, complex_result, cv::DFT_COMPLEX_OUTPUT);
432 // return ComplexMat(complex_result(cv::Range(0, input.rows), cv::Range(0, input.cols)));
434 cv::dft(input, complex_result, cv::DFT_COMPLEX_OUTPUT);
435 return ComplexMat(complex_result);
438 ComplexMat KCF_Tracker::fft2(const std::vector<cv::Mat> &input, const cv::Mat &cos_window)
440 int n_channels = input.size();
441 ComplexMat result(input[0].rows, input[0].cols, n_channels);
442 for (int i = 0; i < n_channels; ++i){
443 cv::Mat complex_result;
444 // cv::Mat padded; //expand input image to optimal size
445 // int m = cv::getOptimalDFTSize( input[0].rows );
446 // int n = cv::getOptimalDFTSize( input[0].cols ); // on the border add zero pixels
448 // copyMakeBorder(input[i].mul(cos_window), padded, 0, m - input[0].rows, 0, n - input[0].cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
449 // cv::dft(padded, complex_result, cv::DFT_COMPLEX_OUTPUT);
450 // result.set_channel(i, complex_result(cv::Range(0, input[0].rows), cv::Range(0, input[0].cols)));
452 cv::dft(input[i].mul(cos_window), complex_result, cv::DFT_COMPLEX_OUTPUT);
453 result.set_channel(i, complex_result);
458 cv::Mat KCF_Tracker::ifft2(const ComplexMat &inputf)
462 if (inputf.n_channels == 1){
463 cv::dft(inputf.to_cv_mat(), real_result, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
465 std::vector<cv::Mat> mat_channels = inputf.to_cv_mat_vector();
466 std::vector<cv::Mat> ifft_mats(inputf.n_channels);
467 for (int i = 0; i < inputf.n_channels; ++i) {
468 cv::dft(mat_channels[i], ifft_mats[i], cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
470 cv::merge(ifft_mats, real_result);
475 //hann window actually (Power-of-cosine windows)
476 cv::Mat KCF_Tracker::cosine_window_function(int dim1, int dim2)
478 cv::Mat m1(1, dim1, CV_32FC1), m2(dim2, 1, CV_32FC1);
479 double N_inv = 1./(static_cast<double>(dim1)-1.);
480 for (int i = 0; i < dim1; ++i)
481 m1.at<float>(i) = 0.5*(1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv));
482 N_inv = 1./(static_cast<double>(dim2)-1.);
483 for (int i = 0; i < dim2; ++i)
484 m2.at<float>(i) = 0.5*(1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv));
489 // Returns sub-window of image input centered at [cx, cy] coordinates),
490 // with size [width, height]. If any pixels are outside of the image,
491 // they will replicate the values at the borders.
492 cv::Mat KCF_Tracker::get_subwindow(const cv::Mat &input, int cx, int cy, int width, int height)
496 int x1 = cx - width/2;
497 int y1 = cy - height/2;
498 int x2 = cx + width/2;
499 int y2 = cy + height/2;
502 if (x1 >= input.cols || y1 >= input.rows || x2 < 0 || y2 < 0) {
503 patch.create(height, width, input.type());
508 int top = 0, bottom = 0, left = 0, right = 0;
510 //fit to image coordinates, set border extensions;
519 if (x2 >= input.cols) {
520 right = x2 - input.cols + width % 2;
525 if (y2 >= input.rows) {
526 bottom = y2 - input.rows + height % 2;
531 if (x2 - x1 == 0 || y2 - y1 == 0)
532 patch = cv::Mat::zeros(height, width, CV_32FC1);
534 cv::copyMakeBorder(input(cv::Range(y1, y2), cv::Range(x1, x2)), patch, top, bottom, left, right, cv::BORDER_REPLICATE);
537 assert(patch.cols == width && patch.rows == height);
542 ComplexMat KCF_Tracker::gaussian_correlation(const ComplexMat &xf, const ComplexMat &yf, double sigma, bool auto_correlation)
544 float xf_sqr_norm = xf.sqr_norm();
545 float yf_sqr_norm = auto_correlation ? xf_sqr_norm : yf.sqr_norm();
547 ComplexMat xyf = auto_correlation ? xf.sqr_mag() : xf * yf.conj();
549 //ifft2 and sum over 3rd dimension, we dont care about individual channels
550 cv::Mat xy_sum(xf.rows, xf.cols, CV_32FC1);
552 cv::Mat ifft2_res = ifft2(xyf);
553 for (int y = 0; y < xf.rows; ++y) {
554 float * row_ptr = ifft2_res.ptr<float>(y);
555 float * row_ptr_sum = xy_sum.ptr<float>(y);
556 for (int x = 0; x < xf.cols; ++x){
557 row_ptr_sum[x] = std::accumulate((row_ptr + x*ifft2_res.channels()), (row_ptr + x*ifft2_res.channels() + ifft2_res.channels()), 0.f);
561 float numel_xf_inv = 1.f/(xf.cols * xf.rows * xf.n_channels);
563 cv::exp(- 1.f / (sigma * sigma) * cv::max((xf_sqr_norm + yf_sqr_norm - 2 * xy_sum) * numel_xf_inv, 0), tmp);
568 float get_response_circular(cv::Point2i & pt, cv::Mat & response)
573 x = response.cols + x;
575 y = response.rows + y;
576 if (x >= response.cols)
577 x = x - response.cols;
578 if (y >= response.rows)
579 y = y - response.rows;
581 return response.at<float>(y,x);
584 cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point & max_loc, cv::Mat & response)
586 //find neighbourhood of max_loc (response is circular)
590 cv::Point2i p1(max_loc.x-1, max_loc.y-1), p2(max_loc.x, max_loc.y-1), p3(max_loc.x+1, max_loc.y-1);
591 cv::Point2i p4(max_loc.x-1, max_loc.y), p5(max_loc.x+1, max_loc.y);
592 cv::Point2i p6(max_loc.x-1, max_loc.y+1), p7(max_loc.x, max_loc.y+1), p8(max_loc.x+1, max_loc.y+1);
594 // fit 2d quadratic function f(x, y) = a*x^2 + b*x*y + c*y^2 + d*x + e*y + f
595 cv::Mat A = (cv::Mat_<float>(9, 6) <<
596 p1.x*p1.x, p1.x*p1.y, p1.y*p1.y, p1.x, p1.y, 1.f,
597 p2.x*p2.x, p2.x*p2.y, p2.y*p2.y, p2.x, p2.y, 1.f,
598 p3.x*p3.x, p3.x*p3.y, p3.y*p3.y, p3.x, p3.y, 1.f,
599 p4.x*p4.x, p4.x*p4.y, p4.y*p4.y, p4.x, p4.y, 1.f,
600 p5.x*p5.x, p5.x*p5.y, p5.y*p5.y, p5.x, p5.y, 1.f,
601 p6.x*p6.x, p6.x*p6.y, p6.y*p6.y, p6.x, p6.y, 1.f,
602 p7.x*p7.x, p7.x*p7.y, p7.y*p7.y, p7.x, p7.y, 1.f,
603 p8.x*p8.x, p8.x*p8.y, p8.y*p8.y, p8.x, p8.y, 1.f,
604 max_loc.x*max_loc.x, max_loc.x*max_loc.y, max_loc.y*max_loc.y, max_loc.x, max_loc.y, 1.f);
605 cv::Mat fval = (cv::Mat_<float>(9, 1) <<
606 get_response_circular(p1, response),
607 get_response_circular(p2, response),
608 get_response_circular(p3, response),
609 get_response_circular(p4, response),
610 get_response_circular(p5, response),
611 get_response_circular(p6, response),
612 get_response_circular(p7, response),
613 get_response_circular(p8, response),
614 get_response_circular(max_loc, response));
616 cv::solve(A, fval, x, cv::DECOMP_SVD);
618 double a = x.at<float>(0), b = x.at<float>(1), c = x.at<float>(2),
619 d = x.at<float>(3), e = x.at<float>(4);
621 cv::Point2f sub_peak(max_loc.x, max_loc.y);
622 if (b > 0 || b < 0) {
623 sub_peak.y = ((2.f * a * e) / b - d) / (b - (4 * a * c) / b);
624 sub_peak.x = (-2 * c * sub_peak.y - e) / b;
630 double KCF_Tracker::sub_grid_scale(std::vector<double> & responses, int index)
633 if (index < 0 || index > (int)p_scales.size()-1) {
634 // interpolate from all values
635 // fit 1d quadratic function f(x) = a*x^2 + b*x + c
636 A.create(p_scales.size(), 3, CV_32FC1);
637 fval.create(p_scales.size(), 1, CV_32FC1);
638 for (size_t i = 0; i < p_scales.size(); ++i) {
639 A.at<float>(i, 0) = p_scales[i] * p_scales[i];
640 A.at<float>(i, 1) = p_scales[i];
641 A.at<float>(i, 2) = 1;
642 fval.at<float>(i) = responses[i];
645 //only from neighbours
646 if (index == 0 || index == (int)p_scales.size()-1)
647 return p_scales[index];
649 A = (cv::Mat_<float>(3, 3) <<
650 p_scales[index-1] * p_scales[index-1], p_scales[index-1], 1,
651 p_scales[index] * p_scales[index], p_scales[index], 1,
652 p_scales[index+1] * p_scales[index+1], p_scales[index+1], 1);
653 fval = (cv::Mat_<float>(3, 1) << responses[index-1], responses[index], responses[index+1]);
657 cv::solve(A, fval, x, cv::DECOMP_SVD);
658 double a = x.at<float>(0), b = x.at<float>(1);
659 double scale = p_scales[index];
661 scale = -b / (2 * a);