当前位置:   article > 正文

目标追踪:核先关滤波(KCF)、相关滤波(MOSSE)、判别相关滤波(CSRT)_kcf mosse csrt

kcf mosse csrt

追踪器选择方法:
如果追求高准确度,又能忍受慢一些的速度,那么就用CSRT;
如果对准确度的要求不苛刻,想追求速度,那么就选KCF;
纯粹想节省时间就用MOSSE。

#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<opencv2/tracking.hpp>
using namespace std;
using namespace cv;
//鼠标交互获得矩形
Rect2d bbox;
bool draw;
void on_mouse(int event,int x,int y,int flags,void *param)
{
	Mat& image = *(Mat*)param;
	switch (event)
	{
	case EVENT_LBUTTONDOWN:
	{
		draw = true;
		bbox = Rect2d(x,y,0,0);
	}
	break;
	case EVENT_MOUSEMOVE:
	{
		if (draw)
		{
			bbox.width = x - bbox.x;
			bbox.height = y - bbox.y;
		}
	}
	break;
	case EVENT_LBUTTONUP:
	{
		draw = false;
		if (bbox.width < 0)
		{
			bbox.x += bbox.width;
			bbox.width *= -1;
		}
		if (bbox.height<0)
		{
			bbox.y += bbox.height;
			bbox.height *= -1;
		}
		rectangle(image,bbox,Scalar(0,0,255),1,8);
	}
	break;
	default:
		break;
	}

}
int main()
{
	//跟踪算法常用类型
	string trackerTypes[3] = { "KCF","MOSSE","CSRT" };
	//创建跟踪算法
	string trackerType = trackerTypes[2];
	Ptr<Tracker> tracker;
	if (trackerType == "KCF")
		tracker = TrackerKCF::create();
	if (trackerType == "MOSSE")
		tracker = TrackerMOSSE::create();
	if (trackerType == "CSRT")
		tracker = TrackerCSRT::create();
	//制作目标对象选框
	VideoCapture capture(0);
	if (!capture.isOpened())
	{
		cout << "Could not read video file" << endl;
		return -1;
	}
	Mat frame;
	capture >> frame;
	Mat src_one;
	frame.copyTo(src_one);
	imshow("用来制作矩形选框的图像", frame);
	//鼠标交互获得矩形
	namedWindow("选择目标矩形",WINDOW_AUTOSIZE);
	setMouseCallback("选择目标矩形",on_mouse,&src_one);
	while (true)
	{
		Mat temp_image;
		src_one.copyTo(temp_image);
		if (draw)
		{
			rectangle(temp_image,bbox,Scalar(0,0,255),1,8);
		}
		imshow("选择目标矩形",temp_image);
		if (waitKey(10) == 27)break;
	}
	//初始化跟踪器
	tracker->init(src_one,bbox);
	//追踪视频目标
	while (capture.read(frame))
	{
		//开始计时tick
		double start = (double)getTickCount();
		//追踪当前帧目标
		bool ok = tracker->update(frame, bbox);
		//计算帧率
		float fps = getTickFrequency() / ((double)getTickCount() - start);
		if (ok)
		{
			//如果追踪到画目标框
			rectangle(frame,bbox,Scalar(0,0,255),1,8);
		}
		else
		{
			putText(frame,"Tracking failure detected",Point(10,10), FONT_HERSHEY_SIMPLEX,0.5,Scalar(255,0,0),2);
		}
		//展示检测算法类型
		putText(frame, trackerType + " Tracker", Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(50, 170, 50), 2);

		// Display FPS on frame 表示FPS
		putText(frame, "FPS : " + to_string(int(fps)), Point(10, 50), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(50, 170, 50), 2);
		// Display frame.
		imshow("Tracking", frame);
		int k = waitKey(1);
		if (k == 27)
		{
			break;
		}
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

1、KCF核相关滤波解读
全看
https://zhuanlan.zhihu.com/p/48249974
看结论部分
https://www.cnblogs.com/jins-note/p/10215511.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/864785
推荐阅读
相关标签
  

闽ICP备14008679号