利用OpenCV给图像添加标注
来源:互联网 发布:剑网三花钱知乎 编辑:程序博客网 时间:2024/05/19 05:39
本程序使用范围:为运动目标跟踪提供ground truth【真实数据】,然后你可以进行各种跟踪算法误差对比
这是写论文的好帮手哦!
内容转自:http://blog.csdn.NET/xiaowei_cqu,是个妹子
我在她代码上稍微改进了下。
代码如下:
-
-
-
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
-
-
- bool is_drawing=false;
- vector<CvRect> biaozhu_boxs;
- CvRect drawing_box;
- IplImage *img,*img1;
-
-
- static void help();
- static void onMouse( int event, int x, int y, int, void* );
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- CvFont font;
- CvScalar scalar;
- char text[10];
-
-
- double hScale=1;
- double vScale=1;
- int lineWidth=3;
- scalar=CV_RGB(255,0,0);
- cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
-
- int frame_counter = 0;
- int obj_id = 0;
-
- CvCapture *capture=cvCreateFileCapture("a.avi");
- img = cvQueryFrame(capture);
- img1 = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,3);
- cvCopy(img,img1);
-
- ofstream outfile("a.txt");
- help();
-
- for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)
- {
- cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));
- }
- cvShowImage("video",img);
-
- cvSetMouseCallback( "video", onMouse, 0 );
-
- while (1)
- {
- int c=cvWaitKey(0);
- if( (c & 255) == 27 )
- {
- cout << "Exiting ...\n";
- break;
- }
-
- switch((char)c)
- {
- case 'n':
-
- ++frame_counter;
- img = cvQueryFrame(capture);
- cvCopy(img,img1);
- if(!img){
- cout<<"\nVideo Finished!"<<endl;
- return 0;
- }
-
-
- for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)
- {
- cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));
- itoa(obj_id,text,10);
- cvPutText(img1,text,cvPoint((*it).x,(*it).y),&font,CV_RGB(255,255,255));
- outfile<<frame_counter<<" "<<obj_id<<" "<<(*it).x<<" "
- <<(*it).y<<" "<<(*it).width<<" "
- <<(*it).height<<endl;
- obj_id++;
- }
- obj_id = 0;
- break;
- case 'c':
-
- biaozhu_boxs.clear();
- cvCopy(img,img1);
- }
- cvShowImage("video",img1);
- }
-
- cvNamedWindow("video",0);
- cvReleaseCapture(&capture);
- cvDestroyWindow("video");
- return 0;
- }
-
- static void help()
- {
- cout << "This program designed for labeling video \n"
- <<"Coded by L. Wei on 9/4/2013\n"<<endl;
-
- cout<<"Use the mouse to draw rectangle on the image for labeling.\n"<<endl;
-
- cout << "Hot keys: \n"
- "\tESC - quit the program\n"
- "\tn - next frame of the video\n"
- "\tc - clear all the labels\n"
- <<endl;
- }
-
- static void onMouse( int event, int x, int y, int, void* )
- {
- switch(event)
- {
- case CV_EVENT_LBUTTONDOWN:
-
- is_drawing=true;
- drawing_box.x=x;
- drawing_box.y=y;
- break;
- case CV_EVENT_MOUSEMOVE:
-
- if(is_drawing){
- drawing_box.width=x-drawing_box.x;
- drawing_box.height=y-drawing_box.y;
- cvCopy(img,img1);
- for(vector<CvRect>::iterator it=biaozhu_boxs.begin();it!=biaozhu_boxs.end();++it)
- {
- cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));
- }
- 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));
- }
- break;
- case CV_EVENT_LBUTTONUP:
-
- if(is_drawing){
- drawing_box.width=x-drawing_box.x;
- drawing_box.height=y-drawing_box.y;
- cvCopy(img,img1);
- for(vector<CvRect>::iterator it=biaozhu_boxs.begin();
- it!=biaozhu_boxs.end();++it){
- cvRectangle(img1,cvPoint((*it).x,(*it).y),cvPoint((*it).x + (*it).width,(*it).y + (*it).height),CV_RGB(0,255,0));
- }
- 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));
- biaozhu_boxs.push_back(drawing_box);
- }
- is_drawing=false;
- break;
- }
- cvShowImage("video",img1);
- return;
- }
功能及用法:1.鼠标框定目标【可多个】
2.按n,进入下一帧,保存当前框定目标坐标到txt文本【可多个】
3.按c,清除当前帧所有已标定区域【人总有犯错的时候】或者上一帧遗留的区域
文件保存格式:
帧编号目标编号 矩形左上角坐标矩形右下角坐标
图片如下:
接下来,就用Matlab尽情的画折线图吧!!吼吼!