请从opencv_share@163.com 密码:download 下载完整格式的文档与代码。
使用harrTraining进行电池的识别实验
by hardy
http://blog.csdn.net/hardvb
提取训练数据
在OpenCV_ObjectDetection_HowTo.pdf 提到的objectmaker.exe,实际中没有找到,只好
自己写了一个。
该小程序完成从摄像头中选定小图像的功能。
使用方法:
使用m按键切换模式
然后按键s 使画面暂停,
用鼠标点击移动选取检测物体的位置。
再按s按键存储数据。
数据提取模式分别为
• 创建正向训练数据,
• 创建背景图像
• 创建测试数据
其中正向训练数据在目录posdata\sample.txt下,格式为:
1100.bmp 3 20 80 21 58 142 69 18 58 255 94 22 74
1243.bmp 3 17 78 27 62 142 68 19 61 251 94 25 77
1470.bmp 3 16 88 32 72 137 83 23 74 273 100 28 79
背景图像为抓取图像,在目录negdata\sample.txt下,格式为:
01NFW-Large.bmp
01NFW-Small.bmp
02Block-Large.bmp
(这里的背景图像也可以从硬盘中文件中搜索的,大小可以与正向训练数据不同)
其中测试训练数据在目录testdata\sample.txt下,格式为:
1100.bmp 3 20 80 21 58 142 69 18 58 255 94 22 74
与正向训练数据一样。
新的objectmaker.exe 代码如下:
- #include "stdafx.h"
- #include <iostream>
- #include <string.h>
- #include <cxcore.h>
- #include <cv.h>
- #include <highgui.h>
- #include <fstream>
- IplImage *image = 0,*image_show = 0, *grey = 0, *prev_grey = 0;
- struct PicRECT
- {
- PicRECT(){count=0;IsDrawing= false;IsStop=false;mode= 0;}
- CvRect *prect()
- {
- return &rect[count];
- }
- CvRect rect[100];
- int count;
- bool IsDrawing;
- bool IsStop;
- int mode;
- };
- PicRECT myPicRECT;
- using namespace std;
- long frame_count =0;
- void on_mouse( int event, int x, int y, int flags, void* param )
- {
- if( !image )
- return;
- if( image->origin )
- y = image->height - y;
- if( event == CV_EVENT_LBUTTONDOWN )
- {
-
- if(!myPicRECT.IsDrawing)
- {
- myPicRECT.prect()->x = x;
- myPicRECT.prect()->y = y;
- myPicRECT.IsDrawing = true;
- }e
- lse
- {
- myPicRECT.prect()->width = x-myPicRECT.prect()->x;
- myPicRECT.prect()->height = abs(y- myPicRECT.prect()->y);
-
- myPicRECT.IsDrawing = false;
- myPicRECT.count ++;
- }
- }
- if(event == CV_EVENT_MOUSEMOVE)
- {
- if(myPicRECT.IsDrawing)
- {
- myPicRECT.prect()->width = x-myPicRECT.prect()->x;
- myPicRECT.prect()->height = y- myPicRECT.prect()->y;
- }
- }
- }
- void saveData();
- int main(int argc, char* argv[])
- {
- cvNamedWindow("win1",0);
- cvSetMouseCallback( "win1", on_mouse, 0 );
- CvCapture* capture = 0;
- capture = cvCaptureFromCAM(0 );
- if( !capture )
- {
- fprintf(stderr,"Could not initialize capturing...\n");
- return -1;
- }
- for(;;)
- {
- IplImage* frame = 0;
- frame = cvQueryFrame( capture );
- if( !frame )
- break;
- if( !image )
- {
-
- image = cvCreateImage( cvGetSize(frame), 8, 3 );
- image_show = cvCreateImage( cvGetSize(frame), 8, 3 );
- image_show->origin=image->origin = 0;
- grey = cvCreateImage( cvGetSize(frame), 8, 1 );
- prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 );
- }i
- f(!myPicRECT.IsStop) cvCopy( frame, image, 0 );
- cvCopy( image, image_show, 0 );
- cvCvtColor( image, grey, CV_BGR2GRAY );
- for(int i=0;i<myPicRECT.count+1;i++)
- {
- CvPoint pt1,pt2;
- pt1= cvPoint(myPicRECT.rect[i].x,myPicRECT.rect[i].y);
- pt2= cvPoint(pt1.x + myPicRECT.rect[i].width, pt1.y +
- myPicRECT.rect[i].height);
- if(i==myPicRECT.count)
- {
- if(myPicRECT.IsDrawing )
- cvRectangle(image_show,pt1,pt2,CV_RGB(0,255,0),1,8,0);
- }e
- lse
- cvRectangle(image_show,pt1,pt2,CV_RGB(0,255,0),1,8,0);
- }
- cvShowImage("win1",image_show);
- char c = cvWaitKey(10);
- if( c == 27 ) break;
- if( c == 's' )
- {
- if(myPicRECT.IsStop) saveData();
- myPicRECT.IsStop = myPicRECT.IsStop^1;
- }i
- f(c=='m')
- {
- myPicRECT.mode = (myPicRECT.mode+1)%3;
- if(myPicRECT.mode==0) cout<<"in InPositive mode "<<endl;
- else if(myPicRECT.mode==1) cout<<"in InNegativemode "<<endl;
- else if(myPicRECT.mode==2) cout<<"in InTestmode "<<endl;
- }
- frame_count++;
- }
- cvReleaseCapture( &capture );
- cvDestroyWindow("win1");
- return 0;
- }/
- /save
- void saveData()
- {
- if(myPicRECT.mode==1)
- {
- string fname("");
- string fpath("negdata\\");
- char buf[10];
- sprintf_s(buf,10,"%d",frame_count);
- fname.append (buf);
- fname.append (".bmp");
- fpath = fpath.append(fname);
- cvSaveImage(fpath.c_str(),image);
- std::fstream datafile("negdata\\sample.txt",std::ios::app);
- datafile<<fname.c_str();
- datafile<< endl;
- datafile.close();
- }i
- f(myPicRECT.count>0 && myPicRECT.mode==2)
- {
- string fpath("testdata\\");
- string fname("");
- char buf[10];
- sprintf_s(buf,10,"%d",frame_count);
- fname.append (buf);
- fname.append (".bmp");
- fpath = fpath.append(fname);
- cvSaveImage(fpath.c_str(),image);
- std::fstream datafile("testdata\\sample.txt",std::ios::app);
- datafile<<fname.c_str();
- datafile<<" "<<myPicRECT.count;
- for(int i=0;i<myPicRECT.count;i++)
- {
- datafile<<" "<<myPicRECT.rect[i].x;
- datafile<<" "<<myPicRECT.rect[i].y;
- datafile<<" "<<myPicRECT.rect[i].width;
- datafile<<" "<<myPicRECT.rect[i].height;
- }
- datafile<< endl;
- datafile.close();
- myPicRECT.count =0;
- myPicRECT.IsDrawing = false;
- }i
- f(myPicRECT.count>0 && myPicRECT.mode==0)
- {
- string fpath("posdata\\");
- string fname("");
- char buf[10];
- sprintf_s(buf,10,"%d",frame_count);
- fname.append (buf);
- fname.append (".bmp");
- fpath = fpath.append(fname);
- cvSaveImage(fpath.c_str(),image);
- std::fstream datafile("posdata\\sample.txt",std::ios::app);
- datafile<<fname.c_str();
- datafile<<" "<<myPicRECT.count;
- for(int i=0;i<myPicRECT.count;i++)
- {
- datafile<<" "<<myPicRECT.rect[i].x;
- datafile<<" "<<myPicRECT.rect[i].y;
- datafile<<" "<<myPicRECT.rect[i].width;
- datafile<<" "<<myPicRECT.rect[i].height;
- }
- datafile<< endl;
- datafile.close();
- myPicRECT.count =0;
- myPicRECT.IsDrawing = false;
- }
- cout<<"Save OK!"<<endl;
- }
使用haartraining
步骤一,创建sample
"C:\Program Files\OpenCV\bin\createsamples.exe" -info "posdata\sample.txt" -vec
data\pos.vec -num 609 -w 20 -h 50
步骤二,haartraining
"C:\Program Files\OpenCV\bin\haartraining.exe" -data data\cascade -vec
data\pos.vec -bg negdata\sample.txt -npos 609 -nneg 918 -mem 1200 -mode ALL -w
20 -h 50
步骤三,测试
"C:\Program Files\OpenCV\bin\performance.exe" -data data\cascade -info
testdata\sample.txt -w 20 -h 50 -rs 18
检测结果:18个测试图例中3个无法找到,一个误检。
(hardvb ) |