攝像頭對物體進行跟蹤kcf算法

KCF作者主頁http://www.robots.ox.ac.uk/~joao/circulant/
代碼謹是對runtracker.cpp進行了修改,以便支持對視頻和攝像頭的跟蹤,源代碼是對圖像序列的跟蹤。

貼出修改後的runkcftracker.cpp

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "kcftracker.hpp"
using namespace std;
using namespace cv;
bool drawing_box = false;
bool gotBB = false;
Rect box;
//畫矩形框
void drawBox(Mat& image, CvRect box, Scalar color, int thick){
	rectangle(image, cvPoint(box.x, box.y), cvPoint(box.x + box.width, box.y + box.height), color, thick);
}
//鼠標追蹤函數
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;   //已經獲得bounding box  
		break;
	}
}
int main(int argc, char* argv[]){
	VideoCapture capture;      //打開攝像頭函數
	////////////////////////
	//capture.open("E://lihaibiao//目標跟蹤視頻//Woman.avi");

	///////////////////////////////// Jogging \ Basketball \ Woman\ Bolt \ CarScale \ David \ Girl
    capture.open(0);
	if (!capture.isOpened())
	{
		cout << "capture device failed to open!" << endl;
		return 1;
	}
	//Register mouse callback to draw the bounding box  
	cvNamedWindow("KCF", CV_WINDOW_AUTOSIZE);
	cvSetMouseCallback("KCF", mouseHandler, NULL);  //用鼠標選中初始目標的bounding box  
	bool HOG = true;
    //bool FIXEDWINDOW = false;
	//bool MULTISCALE = true;
	bool SILENT = false;
	bool LAB = false;

	///////////固定窗口跟蹤//////////
	bool FIXEDWINDOW = true;
	bool MULTISCALE = false;
	//////////////////////////
	// Create KCFTracker object
	KCFTracker tracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);
	// Frame readed
	Mat frame;
	// Tracker results
	Rect result;
	/////////////////////
	capture >> frame                                                                                                                                                                                                                                                                             ;
	/////////////////////
	///Initialization  
	while (!gotBB)
	{
			//capture >> frame;
			//drawBox(frame, box,(0,0,255),2);  //把bounding box 畫出來  
			imshow("KCF", frame);
			if (cvWaitKey(33) == 'q')
				return 0;
	}
	/////////////////////////
	drawBox(frame, box, (0, 0, 255), 2);  //把bounding box 畫出來  
	///////////////////////
	//Remove callback  
	cvSetMouseCallback("KCF", NULL, NULL);  //如果已經獲得第一幀用戶框定的box了,就取消鼠標響應  
	printf("Initial Bounding Box = x:%d y:%d h:%d w:%d\n", box.x, box.y, box.width, box.height);
	tracker.init(box, frame);//初始化追蹤框
	
	///////////加入幀數參數//////////////////////
	int frameCount = 0;
	double t = 0;
	////////////////////////////////////////////
	while (capture.read(frame))
	{
		////////////加入檢測的時間//////////////

	    t = (double)cvGetTickCount();
		frameCount++;
		//////////////////////////////

		result = tracker.update(frame);//追蹤
		//畫矩形框
		rectangle(frame, Point(result.x, result.y), Point(result.x + result.width, result.y + result.height), Scalar(0, 255, 255), 1, 8);
		if (!SILENT){
			imshow("KCF", frame);
			waitKey(10);
			//////////////輸出平均幀率//////////////////
			if (frameCount % 20 == 0){

				t = (double)cvGetTickCount() - t;
				cout << "平均幀率:FPS = "  <<  ((double)cvGetTickFrequency()*1000.) *1000 / t << endl;

			}
		}
	}
 
	

}

在這裏插入圖片描述

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