]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/kcf.cpp
Leave 1px borders in visual debug window
[hercules2020/kcf.git] / src / kcf.cpp
index 60db47b7f88771c6784f02e90c569b19471fa173..a637ec7da623032ba4151a9218bf1173a5a73d82 100644 (file)
@@ -68,14 +68,11 @@ void KCF_Tracker::train(cv::Mat input_rgb, cv::Mat input_gray, double interp_fac
     TRACE("");
 
     // obtain a sub-window for training
-    // TODO: Move Mats outside from here
-    MatScaleFeats patch_feats(1, p_num_of_feats, feature_size);
-    MatScaleFeats temp(1, p_num_of_feats, feature_size);
-    get_features(input_rgb, input_gray, p_current_center.x, p_current_center.y,
+    get_features(input_rgb, input_gray, nullptr, p_current_center.x, p_current_center.y,
                  p_windows_size.width, p_windows_size.height,
-                 p_current_scale).copyTo(patch_feats.scale(0));
-    DEBUG_PRINT(patch_feats);
-    fft.forward_window(patch_feats, model->xf, temp);
+                 p_current_scale).copyTo(model->patch_feats.scale(0));
+    DEBUG_PRINT(model->patch_feats);
+    fft.forward_window(model->patch_feats, model->xf, model->temp);
     DEBUG_PRINTM(model->xf);
     model->model_xf = model->model_xf * (1. - interp_factor) + model->xf * interp_factor;
     DEBUG_PRINTM(model->model_xf);
@@ -180,26 +177,21 @@ void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int f
     feature_size = fit_size / p_cell_size;
 
     p_scales.clear();
-    for (int i = -int(p_num_scales) / 2; i <= int(p_num_scales) / 2; ++i)
+    for (int i = -int(p_num_scales - 1) / 2; i <= int(p_num_scales) / 2; ++i)
         p_scales.push_back(std::pow(p_scale_step, i));
 
-#ifdef CUFFT
-    if (Fft::freq_size(feature_size).area() > 1024) {
-        std::cerr << "Window after forward FFT is too big for CUDA kernels. Plese use -f to set "
-                     "the window dimensions so its size is less or equal to "
-                  << 1024 * p_cell_size * p_cell_size * 2 + 1
-                  << " pixels. Currently the size of the window is: " << fit_size
-                  << " which is  " << fit_size.area() << " pixels. " << std::endl;
-        std::exit(EXIT_FAILURE);
-    }
+    p_angles.clear();
+    for (int i = -int(p_num_angles - 1) / 2; i <= int(p_num_angles) / 2; ++i)
+        p_angles.push_back(i * p_angle_step);
 
+#ifdef CUFFT
     if (m_use_linearkernel) {
         std::cerr << "cuFFT supports only Gaussian kernel." << std::endl;
         std::exit(EXIT_FAILURE);
     }
 #endif
 
-    model.reset(new Model(Fft::freq_size(feature_size), p_num_of_feats));
+    model.reset(new Model(feature_size, p_num_of_feats));
 
 #ifndef BIG_BATCH
     for (auto scale: p_scales)
@@ -265,6 +257,7 @@ BBox_c KCF_Tracker::getBBox()
     tmp.cy = p_current_center.y;
     tmp.w = p_init_pose.w * p_current_scale;
     tmp.h = p_init_pose.h * p_current_scale;
+    tmp.a = 0;
 
     if (p_resize_image)
         tmp.scale(1 / p_downscale_factor);
@@ -308,21 +301,28 @@ double KCF_Tracker::findMaxReponse(uint &max_idx, cv::Point2d &new_location) con
     assert(max_idx < IF_BIG_BATCH(p_scales.size(), d.threadctxs.size()));
 
     if (m_visual_debug) {
-        int w = 100; //feature_size.width;
-        int h = 100; //feature_size.height;
-        cv::Mat all_responses(h * p_num_scales, w * p_num_angles,
-                              d.threadctxs[0].response.type(), cv::Scalar::all(0));
+        const bool rgb = true;
+        int type = rgb ? d.threadctxs[0].dbg_patch[0].type() : d.threadctxs[0].response.type();
+        int w = true ? 100 : (rgb ? fit_size.width  : feature_size.width);
+        int h = true ? 100 : (rgb ? fit_size.height : feature_size.height);
+        cv::Mat all_responses((h + 1) * p_num_scales - 1,
+                              (w + 1) * p_num_angles - 1, type, cv::Scalar::all(0));
         for (size_t i = 0; i < p_num_scales; ++i) {
             for (size_t j = 0; j < p_num_angles; ++j) {
-                cv::Mat tmp = d.threadctxs[IF_BIG_BATCH(0, p_num_angles * i + j)].response.plane(IF_BIG_BATCH(p_num_angles * i + j, 0));
-                tmp = circshift(tmp, -tmp.cols/2, -tmp.rows/2);
+                cv::Mat tmp;
+                if (rgb) {
+                    tmp = d.threadctxs[IF_BIG_BATCH(0, p_num_angles * i + j)].dbg_patch[IF_BIG_BATCH(p_num_angles * i + j, 0)];
+                } else {
+                    tmp = d.threadctxs[IF_BIG_BATCH(0, p_num_angles * i + j)].response.plane(IF_BIG_BATCH(p_num_angles * i + j, 0));
+                    tmp = circshift(tmp, -tmp.cols/2, -tmp.rows/2);
+                }
                 cv::resize(tmp, tmp, cv::Size(w, h));
-                cv::Mat resp_roi(all_responses, cv::Rect(j * w, i * h, w, h));
+                cv::Mat resp_roi(all_responses, cv::Rect(j * (w+1), i * (h+1), w, h));
                 tmp.copyTo(resp_roi);
             }
         }
-        cv::namedWindow("All responses", CV_WINDOW_AUTOSIZE);
-        cv::imshow("All responses", all_responses);
+        cv::namedWindow("KCF visual debug", CV_WINDOW_AUTOSIZE);
+        cv::imshow("KCF visual debug", all_responses);
     }
 
     cv::Point2i &max_response_pt = IF_BIG_BATCH(d.threadctxs[0].max[max_idx].loc,        d.threadctxs[max_idx].max.loc);
@@ -408,7 +408,8 @@ void ThreadCtx::track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input
     BIG_BATCH_OMP_PARALLEL_FOR
     for (uint i = 0; i < IF_BIG_BATCH(kcf.p_num_scales, 1); ++i)
     {
-        kcf.get_features(input_rgb, input_gray, kcf.p_current_center.x, kcf.p_current_center.y,
+        kcf.get_features(input_rgb, input_gray, &dbg_patch[i],
+                         kcf.p_current_center.x, kcf.p_current_center.y,
                          kcf.p_windows_size.width, kcf.p_windows_size.height,
                          kcf.p_current_scale * IF_BIG_BATCH(kcf.p_scales[i], scale))
                 .copyTo(patch_feats.scale(i));
@@ -457,14 +458,17 @@ void ThreadCtx::track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input
 
 // ****************************************************************************
 
-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) const
+cv::Mat KCF_Tracker::get_features(cv::Mat &input_rgb, cv::Mat &input_gray, cv::Mat *dbg_patch,
+                                  int cx, int cy, int size_x, int size_y, double scale) const
 {
     cv::Size scaled = cv::Size(floor(size_x * scale), floor(size_y * scale));
 
     cv::Mat patch_gray = get_subwindow(input_gray, cx, cy, scaled.width, scaled.height);
     cv::Mat patch_rgb = get_subwindow(input_rgb, cx, cy, scaled.width, scaled.height);
 
+    if (dbg_patch)
+        patch_rgb.copyTo(*dbg_patch);
+
     // resize to default size
     if (scaled.area() > fit_size.area()) {
         // if we downsample use  INTER_AREA interpolation
@@ -679,12 +683,19 @@ void KCF_Tracker::GaussianCorrelation::operator()(ComplexMat &result, const Comp
                                                   double sigma, bool auto_correlation, const KCF_Tracker &kcf)
 {
     TRACE("");
+    DEBUG_PRINTM(xf);
+    DEBUG_PRINT(xf_sqr_norm.num_elem);
     xf.sqr_norm(xf_sqr_norm);
+    for (uint s = 0; s < xf.n_scales; ++s)
+        DEBUG_PRINT(xf_sqr_norm[s]);
     if (auto_correlation) {
         yf_sqr_norm = xf_sqr_norm;
     } else {
+        DEBUG_PRINTM(yf);
         yf.sqr_norm(yf_sqr_norm);
     }
+    for (uint s = 0; s < yf.n_scales; ++s)
+        DEBUG_PRINTM(yf_sqr_norm[s]);
     xyf = auto_correlation ? xf.sqr_mag() : xf * yf.conj(); // xf.muln(yf.conj());
     DEBUG_PRINTM(xyf);
 
@@ -693,12 +704,6 @@ void KCF_Tracker::GaussianCorrelation::operator()(ComplexMat &result, const Comp
     DEBUG_PRINTM(xyf_sum);
     kcf.fft.inverse(xyf_sum, ifft_res);
     DEBUG_PRINTM(ifft_res);
-#if 0 && defined(CUFFT)
-    // FIXME
-    cuda_gaussian_correlation(ifft_res.deviceMem(), k.deviceMem(), xf_sqr_norm.deviceMem(),
-                              auto_correlation ? xf_sqr_norm.deviceMem() : yf_sqr_norm.deviceMem(), sigma,
-                              xf.n_channels, xf.n_scales, kcf.feature_size.height, kcf.feature_size.width);
-#else
 
     float numel_xf_inv = 1.f / (xf.cols * xf.rows * (xf.channels() / xf.n_scales));
     for (uint i = 0; i < xf.n_scales; ++i) {
@@ -708,7 +713,7 @@ void KCF_Tracker::GaussianCorrelation::operator()(ComplexMat &result, const Comp
                 * numel_xf_inv, 0), plane);
         DEBUG_PRINTM(plane);
     }
-#endif
+
     kcf.fft.forward(ifft_res, result);
 }