利用OpenCV给图像添加标注

来源:互联网 发布:剑网三花钱知乎 编辑:程序博客网 时间:2024/05/19 05:39

本程序使用范围:为运动目标跟踪提供ground truth【真实数据】,然后你可以进行各种跟踪算法误差对比

这是写论文的好帮手哦!


内容转自:http://blog.csdn.NET/xiaowei_cqu,是个妹子

我在她代码上稍微改进了下。

代码如下:

[cpp] view plain copy
  1. // pic_label.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "cv.h"  
  6. #include "highgui.h"  
  7. #include <iostream>  
  8. #include <string>  
  9. #include <vector>  
  10. #include <fstream>  
  11. using namespace std;  
  12.   
  13. //全局变量  
  14. bool is_drawing=false;  
  15. vector<CvRect> biaozhu_boxs;  
  16. CvRect drawing_box;  
  17. IplImage *img,*img1;  
  18.   
  19.   
  20. static void help();  
  21. static void onMouse( int event, int x, int y, intvoid* );  
  22.   
  23. int _tmain(int argc, _TCHAR* argv[])  
  24. {  
  25.     CvFont font;  
  26.     CvScalar scalar;  
  27.     char text[10];  
  28.   
  29.     // 初始化字体  
  30.     double hScale=1;     
  31.     double vScale=1;      
  32.     int lineWidth=3;// 相当于写字的线条  
  33.     scalar=CV_RGB(255,0,0);  
  34.     cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);//初始化字体,准备写到图片上的    
  35.   
  36.     int frame_counter = 0;  
  37.     int obj_id = 0;  
  38.   
  39.     CvCapture *capture=cvCreateFileCapture("a.avi");  
  40.     img = cvQueryFrame(capture);  
  41.     img1 = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,3);  
  42.     cvCopy(img,img1);  
  43.   
  44.     ofstream outfile("a.txt");  
  45.     help();  
  46.   
  47.     for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)  
  48.     {  
  49.         cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));  
  50.     }  
  51.     cvShowImage("video",img);  
  52.   
  53.     cvSetMouseCallback( "video", onMouse, 0 );  
  54.   
  55.     while (1)  
  56.     {  
  57.         int c=cvWaitKey(0);  
  58.         if( (c & 255) == 27 )  
  59.         {  
  60.             cout << "Exiting ...\n";  
  61.             break;  
  62.         }  
  63.   
  64.         switch((char)c)  
  65.         {  
  66.         case 'n':  
  67.             //read the next frame  
  68.             ++frame_counter;  
  69.             img = cvQueryFrame(capture);  
  70.             cvCopy(img,img1);  
  71.             if(!img){  
  72.                 cout<<"\nVideo Finished!"<<endl;  
  73.                 return 0;  
  74.             }  
  75.   
  76.             //save all of the labeling rects  
  77.             for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)  
  78.             {  
  79.                 cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));  
  80.                 itoa(obj_id,text,10);  
  81.                 cvPutText(img1,text,cvPoint((*it).x,(*it).y),&font,CV_RGB(255,255,255));//在图片中输出字符  
  82.                 outfile<<frame_counter<<" "<<obj_id<<" "<<(*it).x<<" "  
  83.                     <<(*it).y<<" "<<(*it).width<<" "  
  84.                     <<(*it).height<<endl;  
  85.                 obj_id++;  
  86.             }  
  87.             obj_id = 0;  
  88.             break;  
  89.         case 'c':  
  90.             //clear all the rects on the image  
  91.             biaozhu_boxs.clear();  
  92.             cvCopy(img,img1);  
  93.         }  
  94.         cvShowImage("video",img1);  
  95.     }  
  96.   
  97.     cvNamedWindow("video",0);  
  98.     cvReleaseCapture(&capture);  
  99.     cvDestroyWindow("video");  
  100.     return 0;  
  101. }  
  102.   
  103. static void help()  
  104. {  
  105.     cout << "This program designed for labeling video \n"  
  106.         <<"Coded by L. Wei on 9/4/2013\n"<<endl;  
  107.   
  108.     cout<<"Use the mouse to draw rectangle on the image for labeling.\n"<<endl;  
  109.   
  110.     cout << "Hot keys: \n"  
  111.         "\tESC - quit the program\n"  
  112.         "\tn - next frame of the video\n"  
  113.         "\tc - clear all the labels\n"  
  114.         <<endl;  
  115. }  
  116.   
  117. static void onMouse( int event, int x, int y, intvoid* )  
  118. {  
  119.     switch(event)  
  120.     {  
  121.     case CV_EVENT_LBUTTONDOWN:   
  122.         //the left up point of the rect  
  123.         is_drawing=true;  
  124.         drawing_box.x=x;  
  125.         drawing_box.y=y;  
  126.         break;  
  127.     case CV_EVENT_MOUSEMOVE:  
  128.         //adjust the rect (use color blue for moving)  
  129.         if(is_drawing){  
  130.             drawing_box.width=x-drawing_box.x;  
  131.             drawing_box.height=y-drawing_box.y;  
  132.             cvCopy(img,img1);  
  133.             for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)  
  134.             {  
  135.                 cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));  
  136.             }  
  137.             cvRectangle(img1,cvPoint(drawing_box.x,drawing_box.y),cvPoint(drawing_box.x+drawing_box.width,drawing_box.y+drawing_box.height),CV_RGB(255,0,0));  
  138.         }  
  139.         break;  
  140.     case CV_EVENT_LBUTTONUP:  
  141.         //finish drawing the rect (use color green for finish)  
  142.         if(is_drawing){  
  143.             drawing_box.width=x-drawing_box.x;  
  144.             drawing_box.height=y-drawing_box.y;  
  145.             cvCopy(img,img1);  
  146.             for(vector<CvRect>::iterator it=biaozhu_boxs.begin();  
  147.                 it!=biaozhu_boxs.end();++it){  
  148.                     cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));  
  149.             }  
  150.             cvRectangle(img1,cvPoint(drawing_box.x,drawing_box.y),cvPoint(drawing_box.x+drawing_box.width,drawing_box.y+drawing_box.height),CV_RGB(255,0,0));  
  151.             biaozhu_boxs.push_back(drawing_box);  
  152.         }  
  153.         is_drawing=false;  
  154.         break;  
  155.     }  
  156.     cvShowImage("video",img1);  
  157.     return;  
  158. }  

功能及用法:

1.鼠标框定目标【可多个】

2.按n,进入下一帧,保存当前框定目标坐标到txt文本【可多个】

3.按c,清除当前帧所有已标定区域【人总有犯错的时候】或者上一帧遗留的区域

文件保存格式:

帧编号目标编号  矩形左上角坐标矩形右下角坐标


图片如下:



接下来,就用Matlab尽情的画折线图吧!!吼吼!