opencv自帶kcf算法實現目標跟蹤

#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;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章