示例Demo的基本功能
首先读取待跟踪图片集文件目录中第一张(第一帧),用鼠标选定一个待跟踪的矩形区域,然后按空格键继续处理后面的图片。
实验环境:vs2010 + opencv2.4.2
代码示例
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/video/tracking.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
void drawObjectRectangle(int event, int x, int y, int flags, void*);//鼠标处理事件,画一个矩形作为跟踪区域
void readImageSequenceFiles(char* ImgFilePath,vector <string> &imgNames);
/* Description: search the image names in the image sequences
Arguments:
-ImgFilePath: path of the image sequence
-imgNames: vector that stores image name
*/
Mat firstFrame;
Point previousPoint, currentPoint;
Rect initObjectRect; // [x y width height] tracking position
bool lButtonDownFlag = false;
int main(int argc, char * argv[])
{
char imgFilePath[100];
memset(imgFilePath, 0, sizeof(imgFilePath));
strcpy(imgFilePath,"./data");//跟踪图片集的路径
char tmpDirPath[MAX_PATH+1];
vector <string> imgNames;
readImageSequenceFiles(imgFilePath,imgNames);
Mat frame;
Mat grayImg;
sprintf(tmpDirPath, "%s/", imgFilePath);
imgNames[0].insert(0,tmpDirPath);
frame = imread(imgNames[0]);
namedWindow("CT", WINDOW_AUTOSIZE);
imshow("CT", frame);
firstFrame = frame;
setMouseCallback("CT", drawObjectRectangle, 0);
waitKey(0);
cout << initObjectRect.x << " " << initObjectRect.y << " " << initObjectRect.width << " " << initObjectRect.height << endl;
for(int i = 1; i < imgNames.size()-1; i ++)
{
sprintf(tmpDirPath, "%s/", imgFilePath);
imgNames[i].insert(0,tmpDirPath);
frame = imread(imgNames[i]);// get frame
//.........可插入跟踪处理函数
rectangle(frame, initObjectRect, Scalar(200,0,0),2);// Draw rectangle
imshow("CT", frame);// Display
waitKey(1);
}
return 0;
}
void drawObjectRectangle(int event, int x, int y, int flags, void*)
{
if(event == EVENT_LBUTTONDOWN)
{
previousPoint = Point(x, y);
lButtonDownFlag = true;
}else if(event == EVENT_MOUSEMOVE && lButtonDownFlag == true){
Mat tmp;
firstFrame.copyTo(tmp);
currentPoint = Point(x, y);
rectangle(tmp, previousPoint, currentPoint, Scalar(200, 0, 0), 2);
imshow("CT", tmp);
}else if(event == EVENT_LBUTTONUP){
initObjectRect.x = previousPoint.x;
initObjectRect.y = previousPoint.y;
initObjectRect.width = abs(currentPoint.x - previousPoint.x);
initObjectRect.height = abs(currentPoint.y - previousPoint.y);
lButtonDownFlag = false;
}
}
//将./data目录下的图片名字全部存入imageNames
void readImageSequenceFiles(char* imgFilePath,vector <string> &imgNames)
{
imgNames.clear();
char tmpDirSpec[MAX_PATH+1];
sprintf (tmpDirSpec, "%s/*", imgFilePath);
WIN32_FIND_DATA f;
HANDLE h = FindFirstFile(tmpDirSpec , &f); //read . --modified by Lumengru
if(h != INVALID_HANDLE_VALUE)
{
FindNextFile(h, &f); //read .. --modified by Lumengru
FindNextFile(h, &f); //read the first picture --modified by Lumengru
do
{
imgNames.push_back(f.cFileName);
} while(FindNextFile(h, &f));
}
FindClose(h);
}
用鼠标左键点击拖拽画一个矩形,之后按空格键可以处理剩下的图片。