//Tools for Labeling Video
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
using namespace cv;
struct labelRect
{
Rect rect;
int label;
};
// Global variables
bool bGlobalDragBox2 = 0; ///<满足条件:按下右键,鼠标起点在box内、拖动
bool is_drawing=false;
vector<labelRect> frameLabelBoxs; ///<当前帧的标注矩形框
vector<labelRect> preFrameLabelBoxs; ///<上一次标注矩形框
labelRect labelBox; ///<当前帧某一个标注
labelRect globalLabelBox; ///<左键画框
Rect drawing_box;
int globalDragIndex=0; ///<右键选中框索引
Mat img_original,img_drawing, preFrame ;
Mat tmpMat; ///<用于实时显示
int globalLabel=0; ///<标注的id
static void help()
{
cout << "This program designed for labeling video \n" ;
cout<<"Use the mouse left button to draw rectangle on the image for labeling.\n"<<endl;
cout<<"Use the mouse right button to drag rectangle on the image .\n"<<endl;
cout<<"parameter instructions :para1 is video file ,para2 is label save file\n"
"\tn,example:labelVideo t.avi t.txt "<<endl;
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tn - next frame of the video\n"
"\tu - undo the last label\n"
"\tc - clear all the labels\n"
"\td - delete select drag rectangle\n"
"\t+ - increase select drag rectangle\n"
"\t- - decrease select drag rectangle\n"
<<endl;
}
static void onMouse( int event, int x, int y, int flags, void* )
{
static Point pre_pt = (-1,-1);//初始坐标
static Point cur_pt = (-1,-1);//实时坐标
static CvPoint mv_pt = {-1,-1};
static CvPoint dragStart_pt = {-1,-1}; //拖动矩形起、终点
static CvPoint dragEnd_pt = {-1,-1};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);//初始化字体
char temp[16];
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
//the left up point of the rect
for(vector<labelRect>::iterator it=frameLabelBoxs.begin(); it!=frameLabelBoxs.end(); ++it)
{
rectangle(img_drawing,(*it).rect,Scalar(0,255,0));
sprintf(temp,"%d",(*it).label);
putText(img_drawing,temp, Point((*it).rect.x ,(*it).rect.y),FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,255,0,255));
}
bGlobalDragBox2=0;
pre_pt = Point(x,y);
is_drawing=true;
</
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
using namespace cv;
struct labelRect
{
Rect rect;
int label;
};
// Global variables
bool bGlobalDragBox2 = 0; ///<满足条件:按下右键,鼠标起点在box内、拖动
bool is_drawing=false;
vector<labelRect> frameLabelBoxs; ///<当前帧的标注矩形框
vector<labelRect> preFrameLabelBoxs; ///<上一次标注矩形框
labelRect labelBox; ///<当前帧某一个标注
labelRect globalLabelBox; ///<左键画框
Rect drawing_box;
int globalDragIndex=0; ///<右键选中框索引
Mat img_original,img_drawing, preFrame ;
Mat tmpMat; ///<用于实时显示
int globalLabel=0; ///<标注的id
static void help()
{
cout << "This program designed for labeling video \n" ;
cout<<"Use the mouse left button to draw rectangle on the image for labeling.\n"<<endl;
cout<<"Use the mouse right button to drag rectangle on the image .\n"<<endl;
cout<<"parameter instructions :para1 is video file ,para2 is label save file\n"
"\tn,example:labelVideo t.avi t.txt "<<endl;
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tn - next frame of the video\n"
"\tu - undo the last label\n"
"\tc - clear all the labels\n"
"\td - delete select drag rectangle\n"
"\t+ - increase select drag rectangle\n"
"\t- - decrease select drag rectangle\n"
<<endl;
}
static void onMouse( int event, int x, int y, int flags, void* )
{
static Point pre_pt = (-1,-1);//初始坐标
static Point cur_pt = (-1,-1);//实时坐标
static CvPoint mv_pt = {-1,-1};
static CvPoint dragStart_pt = {-1,-1}; //拖动矩形起、终点
static CvPoint dragEnd_pt = {-1,-1};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);//初始化字体
char temp[16];
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
//the left up point of the rect
for(vector<labelRect>::iterator it=frameLabelBoxs.begin(); it!=frameLabelBoxs.end(); ++it)
{
rectangle(img_drawing,(*it).rect,Scalar(0,255,0));
sprintf(temp,"%d",(*it).label);
putText(img_drawing,temp, Point((*it).rect.x ,(*it).rect.y),FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,255,0,255));
}
bGlobalDragBox2=0;
pre_pt = Point(x,y);
is_drawing=true;
</