最簡單的目標跟蹤(模版匹配)
一、概述
目標跟蹤是計算機視覺領域的一個重要分支。研究的人很多,近幾年也出現了很多很多的演算法。大家看看淋漓滿目的paper就知道了。但在這裡,我們也聚焦下比較簡單的演算法,看看它的優勢在哪裡。畢竟有時候簡單就是一種美。
在這裡我們一起來欣賞下“模板匹配”這個簡單點的跟蹤演算法。它的思想很簡單,我們把要跟蹤的目標儲存好,然後在每一幀來臨的時候,我們在整個影象中尋找與這個目標最相似的,我們就相信這個就是目標了。那如何判斷相似呢?就用到了一些相關性的東西了,這個在我之前的一篇博文裡面介紹過,大家可以參考下:
模板匹配中差值的平方和(SSD)與互相關準則的關係
然後為了適應目標的變化,我們就需要隨時更新我們要跟蹤的目標。換句話來說,在跟蹤t幀的時候,也就是在第t幀尋找目標的時候,是與t-1幀中我們找到的目標來進行比較的。這樣目標的外觀變化就會及時的更新。這個就叫做線上跟蹤方法。當然了,這個策略會導致跟蹤漂移的問題,這就是近幾年很多跟蹤演算法關注的重要問題之一了。
二、程式碼實現
我的程式碼是基於Xcode+ OpenCV的。程式碼可以讀入視訊,也可以讀攝像頭,兩者的選擇只需要在程式碼中稍微修改即可。對於視訊來說,執行會先顯示第一幀,然後我們用滑鼠框選要跟蹤的目標,然後跟蹤器開始跟蹤每一幀。對攝像頭來說,就會一直採集影象,然後我們用滑鼠框選要跟蹤的目標,接著跟蹤器開始跟蹤後面的每一幀。具體程式碼如下:
simpleTracker.cpp
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> // 新版本寫在下面檔案中: #include <opencv2/nonfree/features2d.hpp> //#include "opencv2/features2d/features2d.hpp" #include<opencv2/legacy/legacy.hpp> using namespace std; using namespace cv; // 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(0); //開啟攝像頭 if (!capture.isOpened()) // isOpened函式用來檢測VideoCapture類是否開啟成功 { return -1; } // 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; }
三、結果
我們對在目標跟蹤領域一個benchmark的視訊-david來測試下程式碼的效果。如下圖所以,每幀的幀號在右上角所示。這個視訊的光照變化是挺大的,但是簡單的模板匹配方法還是可以挺有效的進行跟蹤的,而且速度很快,在這個視訊中,只花費了1ms左右(耗時的長度與目標框的大小和機器的效能有關)。