Opencv4.5 KCF算法 示例

最近开始看一些目标跟踪的内容,下面是KCF的一个例子,数据集为地址Visual Tracker Benchmark

在使用KCF类时,数据为灰度图时需要根据要求设置参数,否则update函数报错。如Error: Bad argument (Matrix operand is an empty matrix.) in cv::checkOpe..

#include <cstring>
#include <iostream>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;

int main() {
  Rect2d roi;
  Mat frame;
  // 灰度图需要设置参数  不是灰度图可按照默认参数
  TrackerKCF::Params param;
  param.desc_pca = TrackerKCF::GRAY;
  param.compressed_size = 0x01;
  param.detect_thresh = 0.3;
  //参数设置结束
  Ptr<TrackerKCF> tracker = TrackerKCF::create(param); 


  string frame_file = "D:/OTB/Car4/Car4/img/0%03d.jpg";
  VideoCapture Sequence(frame_file);


  Sequence >> frame;
  while (1) {
    char key = waitKey(1);
    if (key == 'a') // 按a键跳帧
    {
      Sequence >> frame;
    }
    if (key == 'e') // 按e键退出跳帧
    {
      break;
    }
    imshow("image", frame);
  }


  cv::destroyWindow("image");

  roi = selectROI("tracker", frame);

  if (roi.width == 0 || roi.height == 0)
    return 0;

    tracker->init(frame, roi);

  int ncnt = 0;
  printf("Start the tracking process\n");

  for (;;) {
    // get frame from the video
    Sequence >> frame;
    // stop the program if no more images
    if (frame.rows == 0 || frame.cols == 0) {
      cv::destroyWindow("tracker");
      break;
    }

    printf("frame cnt:%d\n",ncnt);
    ncnt++;
//    imwrite(str,frame);
//    continue;
    // update the tracking result
    tracker->update(frame, roi);
    // draw the tracked object
    rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
    // show image with the tracked object
    imshow("tracker", frame);
    // quit on ESC button
    if (char(waitKey(1)) == 'q') {
      cv::destroyWindow("tracker");
      break;
    }
  }
  return 0;
}
效果如下:

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值