基于opencv视频中运动目标标定

这个程序设计用于视频标注,通过鼠标左键绘制矩形框来标记目标,右键拖动调整已标记的矩形。可以使用键盘快捷键进行操作,如ESC退出,N跳到下一帧,U撤销最后的标记,C清除所有标记,D删除选定的拖动矩形,+和-调整选定矩形大小。
摘要由CSDN通过智能技术生成
//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; 
drawing_box.x=x;
drawing_box.y=y;  


break;


case CV_EVENT_MOUSEMOVE:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值