织梦CMS - 轻松建站从此开始!

罗索

使用haarTraining进行电池的识别实验

落鹤生 发布于 2010-04-26 22:36 点击:次 
使用haarTraining进行电池的识别实验 提取训练数据 在OpenCV_ObjectDetection_HowTo.pdf 提到的objectmaker.exe,实际中没有找到,只好 自己写了一个。
TAG:

请从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 代码如下:

  1. #include "stdafx.h" 
  2. #include <iostream> 
  3. #include <string.h> 
  4. #include <cxcore.h> 
  5. #include <cv.h> 
  6. #include <highgui.h> 
  7. #include <fstream> 
  8. IplImage *image = 0,*image_show = 0, *grey = 0, *prev_grey = 0; 
  9. struct PicRECT 
  10. PicRECT(){count=0;IsDrawing= false;IsStop=false;mode= 0;} 
  11. CvRect *prect() 
  12. return &rect[count]; 
  13. CvRect rect[100]; 
  14. int count; 
  15. bool IsDrawing; 
  16. bool IsStop; 
  17. int mode; 
  18. }; 
  19. PicRECT myPicRECT; 
  20. using namespace std; 
  21. long frame_count =0; 
  22. void on_mouse( int event, int x, int y, int flags, void* param ) 
  23. if( !image ) 
  24. return
  25. if( image->origin ) 
  26. y = image->height - y; 
  27. if( event == CV_EVENT_LBUTTONDOWN ) 
  28. //pt = cvPoint(x,y); 
  29. if(!myPicRECT.IsDrawing) 
  30. myPicRECT.prect()->x = x; 
  31. myPicRECT.prect()->y = y; 
  32. myPicRECT.IsDrawing = true
  33. }e 
  34. lse 
  35. myPicRECT.prect()->width = x-myPicRECT.prect()->x; 
  36. myPicRECT.prect()->height = abs(y- myPicRECT.prect()->y); 
  37. //myPicRECT.prect()->y = y; 
  38. myPicRECT.IsDrawing = false
  39. myPicRECT.count ++; 
  40. if(event == CV_EVENT_MOUSEMOVE) 
  41. if(myPicRECT.IsDrawing) 
  42. myPicRECT.prect()->width = x-myPicRECT.prect()->x; 
  43. myPicRECT.prect()->height = y- myPicRECT.prect()->y; 
  44. void saveData(); 
  45. int main(int argc, char* argv[]) 
  46. cvNamedWindow("win1",0); 
  47. cvSetMouseCallback( "win1", on_mouse, 0 ); 
  48. CvCapture* capture = 0; 
  49. capture = cvCaptureFromCAM(0 ); 
  50. if( !capture ) 
  51. fprintf(stderr,"Could not initialize capturing...\n"); 
  52. return -1; 
  53. for(;;) 
  54. IplImage* frame = 0; 
  55. frame = cvQueryFrame( capture ); 
  56. if( !frame ) 
  57. break
  58. if( !image ) 
  59. /* allocate all the buffers */ 
  60. image = cvCreateImage( cvGetSize(frame), 8, 3 ); 
  61. image_show = cvCreateImage( cvGetSize(frame), 8, 3 ); 
  62. image_show->origin=image->origin = 0; 
  63. grey = cvCreateImage( cvGetSize(frame), 8, 1 ); 
  64. prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 ); 
  65. }i 
  66. f(!myPicRECT.IsStop) cvCopy( frame, image, 0 ); 
  67. cvCopy( image, image_show, 0 ); 
  68. cvCvtColor( image, grey, CV_BGR2GRAY ); 
  69. for(int i=0;i<myPicRECT.count+1;i++) 
  70. CvPoint pt1,pt2; 
  71. pt1= cvPoint(myPicRECT.rect[i].x,myPicRECT.rect[i].y); 
  72. pt2= cvPoint(pt1.x + myPicRECT.rect[i].width, pt1.y + 
  73. myPicRECT.rect[i].height); 
  74. if(i==myPicRECT.count) 
  75. if(myPicRECT.IsDrawing ) 
  76. cvRectangle(image_show,pt1,pt2,CV_RGB(0,255,0),1,8,0); 
  77. }e 
  78. lse 
  79. cvRectangle(image_show,pt1,pt2,CV_RGB(0,255,0),1,8,0); 
  80. cvShowImage("win1",image_show); 
  81. char c = cvWaitKey(10); 
  82. if( c == 27 ) break
  83. if( c == 's' ) 
  84. if(myPicRECT.IsStop) saveData(); 
  85. myPicRECT.IsStop = myPicRECT.IsStop^1; 
  86. }i 
  87. f(c=='m'
  88. myPicRECT.mode = (myPicRECT.mode+1)%3; 
  89. if(myPicRECT.mode==0) cout<<"in InPositive mode "<<endl; 
  90. else if(myPicRECT.mode==1) cout<<"in InNegativemode "<<endl; 
  91. else if(myPicRECT.mode==2) cout<<"in InTestmode "<<endl; 
  92. frame_count++; 
  93. cvReleaseCapture( &capture ); 
  94. cvDestroyWindow("win1"); 
  95. return 0; 
  96. }/ 
  97. /save 
  98. void saveData() 
  99. if(myPicRECT.mode==1) 
  100. string fname(""); 
  101. string fpath("negdata\\"); 
  102. char buf[10]; 
  103. sprintf_s(buf,10,"%d",frame_count); 
  104. fname.append (buf); 
  105. fname.append (".bmp"); 
  106. fpath = fpath.append(fname); 
  107. cvSaveImage(fpath.c_str(),image); 
  108. std::fstream datafile("negdata\\sample.txt",std::ios::app); 
  109. datafile<<fname.c_str(); 
  110. datafile<< endl; 
  111. datafile.close(); 
  112. }i 
  113. f(myPicRECT.count>0 && myPicRECT.mode==2) 
  114. string fpath("testdata\\"); 
  115. string fname(""); 
  116. char buf[10]; 
  117. sprintf_s(buf,10,"%d",frame_count); 
  118. fname.append (buf); 
  119. fname.append (".bmp"); 
  120. fpath = fpath.append(fname); 
  121. cvSaveImage(fpath.c_str(),image); 
  122. std::fstream datafile("testdata\\sample.txt",std::ios::app); 
  123. datafile<<fname.c_str(); 
  124. datafile<<" "<<myPicRECT.count; 
  125. for(int i=0;i<myPicRECT.count;i++) 
  126. datafile<<" "<<myPicRECT.rect[i].x; 
  127. datafile<<" "<<myPicRECT.rect[i].y; 
  128. datafile<<" "<<myPicRECT.rect[i].width; 
  129. datafile<<" "<<myPicRECT.rect[i].height; 
  130. datafile<< endl; 
  131. datafile.close(); 
  132. myPicRECT.count =0; 
  133. myPicRECT.IsDrawing = false
  134. }i 
  135. f(myPicRECT.count>0 && myPicRECT.mode==0) 
  136. string fpath("posdata\\"); 
  137. string fname(""); 
  138. char buf[10]; 
  139. sprintf_s(buf,10,"%d",frame_count); 
  140. fname.append (buf); 
  141. fname.append (".bmp"); 
  142. fpath = fpath.append(fname); 
  143. cvSaveImage(fpath.c_str(),image); 
  144. std::fstream datafile("posdata\\sample.txt",std::ios::app); 
  145. datafile<<fname.c_str(); 
  146. datafile<<" "<<myPicRECT.count; 
  147. for(int i=0;i<myPicRECT.count;i++) 
  148. datafile<<" "<<myPicRECT.rect[i].x; 
  149. datafile<<" "<<myPicRECT.rect[i].y; 
  150. datafile<<" "<<myPicRECT.rect[i].width; 
  151. datafile<<" "<<myPicRECT.rect[i].height; 
  152. datafile<< endl; 
  153. datafile.close(); 
  154. myPicRECT.count =0; 
  155. myPicRECT.IsDrawing = false
  156. 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 )
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201004/9271.html]
本文出处:CSDN博客 作者:hardvb
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容