计算机视觉
图像处理

【OpenCV】视频标注工具

之前一直用师兄写的标注工具,很多操作感觉不太习惯。又没有源码可改,索性自己写了一个,功能比较简单。就是视频的读取和标注。

主要用鼠标的点击进行标注,第一次点击为标注框左上角的点,可以按住左键调整标注框;松开时会确定标注框(框由蓝色变为绿色);一帧中可以标注多个视频。

主要通过Opencv的鼠标响应函数来实现:

  1. void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 );

响应鼠标操作的onMouse函数如下:

  1. static void onMouse( int event, int x, int y, intvoid* )
  2. {
  3.     switch(event)
  4.     {
  5.     case CV_EVENT_LBUTTONDOWN:
  6.         //the left up point of the rect
  7.         is_drawing=true;
  8.         drawing_box.x=x;
  9.         drawing_box.y=y;
  10.         break;
  11.     case CV_EVENT_MOUSEMOVE:
  12.         //adjust the rect (use color blue for moving)
  13.         if(is_drawing){
  14.             drawing_box.width=x-drawing_box.x;
  15.             drawing_box.height=y-drawing_box.y;
  16.             img_original.copyTo(img_drawing);
  17.             for(vector<Rect>::iterator it=biaozhu_boxs.begin();
  18.                 it!=biaozhu_boxs.end();++it){
  19.                     rectangle(img_drawing,(*it),Scalar(0,255,0));
  20.             }
  21.             rectangle(img_drawing,drawing_box,Scalar(255,0,0));
  22.         }
  23.         break;
  24.     case CV_EVENT_LBUTTONUP:
  25.         //finish drawing the rect (use color green for finish)
  26.         if(is_drawing){
  27.             drawing_box.width=x-drawing_box.x;
  28.             drawing_box.height=y-drawing_box.y;
  29.             img_original.copyTo(img_drawing);
  30.             for(vector<Rect>::iterator it=biaozhu_boxs.begin();
  31.                 it!=biaozhu_boxs.end();++it){
  32.                     rectangle(img_drawing,(*it),Scalar(0,255,0));
  33.             }
  34.             rectangle(img_drawing,drawing_box,Scalar(0,255,0));
  35.             biaozhu_boxs.push_back(drawing_box);
  36.         }
  37.         is_drawing=false;
  38.         break;
  39.     }
  40.     imshow(“Video”,img_drawing);
  41.     return;
  42. }

另外通过键盘实现一些按键的响应:(Esc)退出;(n)下一帧;(z)撤销最近一次标注;(c)清除当前帧已有的标注框

具体用waitKey()函数实现:

  1. while(1){
  2.         int c=waitKey(0);
  3.         if( (c & 255) == 27 )
  4.         {
  5.             cout << “Exiting …n”;
  6.             break;
  7.         }
  8.         switch((char)c)
  9.         {
  10.         case ‘n’:
  11.             //read the next frame
  12.             ++frame_counter;
  13.             capture>>img_original;
  14.             if(img_original.empty()){
  15.                 cout<<“nVideo Finished!”<<endl;
  16.                 return 0;
  17.             }
  18.             img_original.copyTo(img_drawing);
  19.             //save all of the labeling rects
  20.             for(vector<Rect>::iterator it=biaozhu_boxs.begin();
  21.                 it!=biaozhu_boxs.end();++it){
  22.                     rectangle(img_drawing,(*it),Scalar(0,255,0));
  23.                     outfile<<frame_counter<<” “<<(*it).x<<” “
  24.                         <<(*it).y<<” “<<(*it).width<<” “
  25.                         <<(*it).height<<endl;
  26.             }
  27.             break;
  28.         case ‘z’:
  29.             //undo the latest labeling
  30.             if(!biaozhu_boxs.empty()){
  31.                 vector<Rect>::iterator it_end=biaozhu_boxs.end();
  32.                 –it_end;
  33.                 biaozhu_boxs.erase(it_end);
  34.             }
  35.             img_original.copyTo(img_drawing);
  36.             for(vector<Rect>::iterator it=biaozhu_boxs.begin();
  37.                 it!=biaozhu_boxs.end();++it){
  38.                     rectangle(img_drawing,(*it),Scalar(0,255,0));
  39.             }
  40.             break;
  41.         case ‘c’:
  42.             //clear all the rects on the image
  43.             biaozhu_boxs.clear();
  44.             img_original.copyTo(img_drawing);
  45.         }
  46.         imshow(“Video”,img_drawing);
  47.     }

标注信息输出在txt中,包括帧号、标注框左上角的x,y坐标以及宽、高

全部代码下载:http://pan.baidu.com/s/1i5o6uqT

转载注明来源:CV视觉网 » 【OpenCV】视频标注工具

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 1超赞 0无聊 0扯淡 0不解 0路过

评论 3

评论前必须登录!