设为首页 加入收藏

TOP

利用OpenCV给图像添加标注(一)
2015-03-04 22:51:13 来源: 作者: 【 】 浏览:75
Tags:利用 OpenCV 图像 添加 标注

利用OpenCV给图像添加标注


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


这是写论文的好帮手哦!


代码如下:


// pic_label.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include
#include
#include
#include
using namespace std;


//全局变量
bool is_drawing=false;
vector 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::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':
? ?//read the next frame
? ?++frame_counter;
? ?img = cvQueryFrame(capture);
? ?cvCopy(img,img1);
? ?if(!img){
? ? cout<<"\nVideo Finished!"<? ? return 0;
? ?}


? ?//save all of the labeling rects
? ?for(vector::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<? ? ?<<(*it).y<<" "<<(*it).width<<" "
? ? ?<<(*it).height<? ? obj_id++;
? ?}
? ?obj_id = 0;
? ?break;
? case 'c':
? ?//clear all the rects on the image
? ?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"<

?cout<<"Use the mouse to draw rectangle on the image for labeling.\n"<

?cout << "Hot keys: \n"
? "\tESC - quit the program\n"
? "\tn - next frame of the video\n"
? "\tc - clear all the labels\n"
? <}


static void onMouse( int event, int x, int y, int, void* )
{
?switch(event)
?{
?case CV_EVENT_LBUTTONDOWN:
? //the left up point of the rect
? is_drawing=true;
? drawing_box.x=x;
? drawing_box.y=y;
? break;
?case CV_EVENT_MOUSEMOVE:
? //adjust the rect (use color blue for moving)
? if(is_drawing){
? ?drawing_box.width=x-drawing_box.x;
? ?drawing_box.height=y-drawing_box.y;
? ?cvCopy(img,img1);
? ?for(vector::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:
? //finish drawing the rect (use color green for finish)
? if(is_drawing){
? ?drawing_box.width=x-drawing_box.x;
? ?drawing_box.height=y-drawing_box.y;
? ?cvCopy(img,img1);
? ?for(vector::iterator it=biaozhu_boxs.begin();
? ? it!=biaozhu_boxs.end();++it){
? ? ?cvRectangle(img1,cvPoint((*it).x,(*it).y),c

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇【OpenCV】使用floodfill()实现Ph.. 下一篇Linux下使用popen()执行shell命令

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: