该程序实验在一文件夹下自动加载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