OpenCV 实践程序17——实现图片标注

该程序实验在一文件夹下自动加载jpg格式(格式可以改)的图片,我们用bouningbox选取我们感兴趣的物体,程序会自动记录最小点坐标和bouningbox的长和宽,并保存到info文本文本中。

#include "cv.h"
#include "cvaux.h"
#include "highgui.h"

// for filelisting
#include <stdio.h>
#include <io.h>
// for fileoutput
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

IplImage* image=0;
IplImage* image2=0;
int start_roi=0;
int roi_x0=0;
int roi_y0=0;
int roi_x1=0;
int roi_y1=0;
int numOfRec=0;
char* window_name="<SPACE>add <ENTER>save and load next <ESC>exit";

string IntToString(int num)
{
	ostringstream myStream; //creates an ostringstream object
	myStream << num << flush;
	
	// outputs the number into the string stream and then flushes
	//the buffer (makes sure the output is put into the stream)
	
	return(myStream.str()); //returns the string form of the stringstream object
};

void onmouse(int event,int x,int y,int flag,void *param)
{
    if(event==CV_EVENT_LBUTTONDOWN)
    {
		start_roi=1;
		roi_x0=x;
		roi_y0=y;
    }
    if(event==CV_EVENT_MOUSEMOVE && flag==CV_EVENT_FLAG_LBUTTON)
    {
		roi_x1=x;
		roi_y1=y;

		//redraw ROI selection
		image2=cvCloneImage(image);
		cvRectangle(image2,cvPoint(roi_x0,roi_y0),cvPoint(roi_x1,roi_y1),CV_RGB(255,0,255),1);
		cvShowImage(window_name,image2);
		cvReleaseImage(&image2);
    }
    if(event==CV_EVENT_LBUTTONUP)
    {
		start_roi=0;
    }
}

int main(int argc, char** argv)
{
	struct _finddata_t bmp_file;
    long hFile;
	int iKey=0;
	
//	get *.bmp files in directory
	if((hFile=_findfirst("rawdata/*.jpg",&bmp_file))==-1L)
		printf("no *.jpg files in directory 'rawdata'\n");
	else
	{

	//	init highgui
		cvAddSearchPath("rawdata");
		cvNamedWindow(window_name,1);
		cvSetMouseCallback(window_name,o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值