opencv自带kcf算法实现目标跟踪

目标跟踪 与 目标检测 专栏收录该内容
11 篇文章 0 订阅
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>

using namespace std;
using namespace cv;

int main() {
	// declares all required variables
	//! [vars]
	Rect2d roi;
	Mat frame;
	//! [vars]

	// create a tracker object
	//Ptr<Tracker> tracker = Tracker::create("KCF");
	Ptr<Tracker> tracker = TrackerKCF::create();
	//! [create]

	// set input video
	//! [setvideo]
	std::string video = "E:\\car.mp4";
	VideoCapture cap(video);
	//! [setvideo]

	// get bounding box
	//! [getframe]
	cap >> frame;
	//! [getframe]
	//! [selectroi]选择目标roi以GUI的形式
	roi = selectROI("tracker", frame);
	//! [selectroi]

	//quit if ROI was not selected
	if (roi.width == 0 || roi.height == 0)
		return 0;

	// initialize the tracker
	//! [init]
	tracker->init(frame, roi);
	//! [init]

	// perform the tracking process
	printf("Start the tracking process\n");
	for (;; ) {
		// get frame from the video
		cap >> frame;

		// stop the program if no more images
		if (frame.rows == 0 || frame.cols == 0)
			break;

		// update the tracking result
		//! [update]
		tracker->update(frame, roi);
		//! [update]

		//! [visualization]
		// draw the tracked object
		rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);

		// show image with the tracked object
		imshow("tracker", frame);
		//! [visualization]
		//quit on ESC button
		if (waitKey(1) == 27)
			break;
	}

	return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 0
    评论
  • 14
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值