目标追踪算法

模板匹配

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
// Global variables
Rect box;
bool drawing_box = false;
bool gotBB = false;
 
// bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param){
  switch( event ){
  case CV_EVENT_MOUSEMOVE:
    if (drawing_box){
        box.width = x-box.x;
        box.height = y-box.y;
    }
    break;
  case CV_EVENT_LBUTTONDOWN:
    drawing_box = true;
    box = Rect( x, y, 0, 0 );
    break;
  case CV_EVENT_LBUTTONUP:
    drawing_box = false;
    if( box.width < 0 ){
        box.x += box.width;
        box.width *= -1;
    }
    if( box.height < 0 ){
        box.y += box.height;
        box.height *= -1;
    }
    gotBB = true;
    break;
  }
}
 
 
// tracker: get search patches around the last tracking box,
// and find the most similar one
void tracking(Mat frame, Mat &model, Rect &trackBox)
{
	Mat gray;
	cvtColor(frame, gray, CV_RGB2GRAY);
 
	Rect searchWindow;
	searchWindow.width = trackBox.width * 3;
	searchWindow.height = trackBox.height * 3;
	searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
	searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
	searchWindow &= Rect(0, 0, frame.cols, frame.rows);
 
	Mat similarity;
	matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED); 
 
	double mag_r;
	Point point;
	minMaxLoc(similarity, 0, &mag_r, 0, &point);
	trackBox.x = point.x + searchWindow.x;
	trackBox.y = point.y + searchWindow.y;
	model = gray(trackBox);
}
 
int main(int argc, char * argv[])
{
	VideoCapture capture;
	capture.open("david.mpg");
	bool fromfile = true;
	//Init camera
	if (!capture.isOpened())
	{
		cout << "capture device failed to open!" << endl;
		return -1;
	}
	//Register mouse callback to draw the bounding box
	cvNamedWindow("Tracker", CV_WINDOW_AUTOSIZE);
	cvSetMouseCallback("Tracker", mouseHandler, NULL ); 
 
	Mat frame, model;
	capture >> frame;
	while(!gotBB)
	{
		if (!fromfile)
			capture >> frame;
 
		imshow("Tracker", frame);
		if (cvWaitKey(20) == 'q')
			return 1;
	}
	//Remove callback
	cvSetMouseCallback("Tracker", NULL, NULL ); 
	
	Mat gray;
	cvtColor(frame, gray, CV_RGB2GRAY); 
	model = gray(box);
 
	int frameCount = 0;
 
	while (1)
	{
		capture >> frame;
		if (frame.empty())
			return -1;
		double t = (double)cvGetTickCount();
		frameCount++;
 
		// tracking
		tracking(frame, model, box);	
 
		// show
		stringstream buf;
		buf << frameCount;
		string num = buf.str();
		putText(frame, num, Point(20, 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
		rectangle(frame, box, Scalar(0, 0, 255), 3);
		imshow("Tracker", frame);
 
 
		t = (double)cvGetTickCount() - t;
		cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;
 
		if ( cvWaitKey(1) == 27 )
			break;
	}
 
	return 0;
}

模板匹配的表现
ZCU102 1920*1080
小目标
大约在60-70ms

KCF

这个的版本号较高,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()
{
    Rect2d roi;
    Mat frame;
    Ptr<TrackerKCF> tracker = TrackerKCF::create();//高版本一般是这样创建KCF的
    string video = ".\KCFTestData\NBA.mov"; //视频流
    //    VideoCapture cap(0);//启用摄像头
    VideoCapture cap(video);
    if (!cap.isOpened())
    {
        return 0;
    }
    cout << "press c to leap current Image" << endl;
    cout << "press q to slect current Image" << endl;
    cout << "press empty key to start track RIO Object" << endl;
 
    cap >> frame;
    while (1)
    {
        char key = waitKey(1);
        if (key == 'c')  // 按c键跳帧
        {
            cap >> frame;
        }
        if (key == 'q')  // 按q键退出跳帧
        {
            break;
        }
        imshow("first", frame);
    }
 
    cv::destroyWindow("first");
 
    roi = selectROI("tracker", frame);
 
    if (roi.width == 0 || roi.height == 0)
        return 0;
 
    tracker->init(frame, roi);
 
    // 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) {
            cv::destroyWindow("tracker");
            break;
        }
 
        // 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;
}

这个对应opencv 3.1版本的

#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");
	//! [create]
 
	// set input video
	//! [setvideo]
	std::string video = "E:\\demo1.avi";
	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;
}

关于select roi设置问题,参考下面的博客

https://blog.csdn.net/wc781708249/article/details/78518447

看一下在ZCU102上的表现 4核A53
1920*1080的图像,小目标。
时间是130ms

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值