//本代码为在视频帧中标记帧数,在第一帧鼠标取框的功能性测试程序
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
Rect box;
bool drawing_box = false;
bool gotBB = false;
void mouseHandler(int event, int x, int y, int flags, void *param);
int main()
{
void draw_framecount(Mat Frame,int framecount);
namedWindow("text",CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("text",mouseHandler,NULL);//注册鼠标事件
//读入第一帧
char img_str[200];
sprintf(img_str,"D:\\image\\UMN\\Crowd-Activity-%04d.jpg",1);
Mat img=imread(img_str);
draw_framecount(img,1);
//在视频第一帧中等待鼠标取框
while(!gotBB)
{
rectangle(img,box,Scalar(0,0,255));
imshow("text",img);
waitKey(100);
}
//释放鼠标事件
setMouseCallback("text", NULL, NULL);
for(int count=2;count<=6000;count++)
{
char img_str[200];
sprintf(img_str,"D:\\image\\UMN\\Crowd-Activity-%04d.jpg",count);
Mat img=imread(img_str);
rectangle(img,box,Scalar(0,0,255));//在每一帧中都画出第一帧取的方框
draw_framecount(img,count);
imshow("text",img);
waitKey(100);
}
}
//标记当前帧的帧数
void draw_framecount(Mat Frame,int framecount)//Frame为传进来的当前帧,framecount为当前帧数
{
char strFrame[10];
sprintf(strFrame, "#%0d ",framecount);
putText(Frame,strFrame,cvPoint(0,20),2,1,CV_RGB(25,200,25));
}
//鼠标事件的回调函数定义,用于在视频帧中截取一个矩形,可用于跟踪
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;
default:
break;
}
}