【OpenCV】视频标注工具

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

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

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

 

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

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

 

 

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;
			img_original.copyTo(img_drawing);
			for(vector<Rect>::iterator it=biaozhu_boxs.begin();
				it!=biaozhu_boxs.end();++it){
					rectangle(img_drawing,(*it),Scalar(0,255,0));
			}
			rectangle(img_drawing,drawing_box,Scalar(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;
			img_original.copyTo(img_drawing);
			for(vector<Rect>::iterator it=biaozhu_boxs.begin();
				it!=biaozhu_boxs.end();++it){
					rectangle(img_drawing,(*it),Scalar(0,255,0));
			}
			rectangle(img_drawing,drawing_box,Scalar(0,255,0));
			biaozhu_boxs.push_back(drawing_box);
		}
		is_drawing=false;
		break;
	}
	imshow("Video",img_drawing);
	return;
}


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

 

具体用waitKey()函数实现:

 

while(1){
		int c=waitKey(0);
		if( (c & 255) == 27 )
		{
			cout << "Exiting ...\n";
			break;
		}
		switch((char)c)
		{
		case 'n':
			//read the next frame
			++frame_counter;
			capture>>img_original;
			if(img_original.empty()){
				cout<<"\nVideo Finished!"<<endl;
				return 0;
			}

			img_original.copyTo(img_drawing);
			//save all of the labeling rects
			for(vector<Rect>::iterator it=biaozhu_boxs.begin();
				it!=biaozhu_boxs.end();++it){
					rectangle(img_drawing,(*it),Scalar(0,255,0));
					outfile<<frame_counter<<" "<<(*it).x<<" "
						<<(*it).y<<" "<<(*it).width<<" "
						<<(*it).height<<endl;
			}
			break;
		case 'z':
			//undo the latest labeling
			if(!biaozhu_boxs.empty()){
				vector<Rect>::iterator it_end=biaozhu_boxs.end();
				--it_end;
				biaozhu_boxs.erase(it_end);
			}
			img_original.copyTo(img_drawing);
			for(vector<Rect>::iterator it=biaozhu_boxs.begin();
				it!=biaozhu_boxs.end();++it){
					rectangle(img_drawing,(*it),Scalar(0,255,0));
			}
			break;

		case 'c':
			//clear all the rects on the image
			biaozhu_boxs.clear();
			img_original.copyTo(img_drawing);
		}

		imshow("Video",img_drawing);
	}


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

 

 

全部代码下载:https://github.com/xiaoweicqu/video-label-tooll

http://download.csdn.net/detail/xiaowei_cqu/5238931

(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)

扫描或搜索公众号:小魏的修行路,在手机端查看文章

 

 

 

 

没有更多推荐了,返回首页