热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

http:blog.csdn.nethardVBarchive200710101818756.aspx请从opencv_share@163.com密码:download

http://blog.csdn.net/hardVB/archive/2007/10/10/1818756.aspx

请从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
#include
#include
#include
#include
#include
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 )
{
//pt = cvPoint(x,y);
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.prect()->y = 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 )
{
/* allocate all the buffers */
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{
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 "<else if(myPicRECT.mode==1) cout<<"in InNegativemode "<else if(myPicRECT.mode==2) cout<<"in InTestmode "<}
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<datafile<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<datafile<<" "<for(int i=0;i{
datafile<<" "<datafile<<" "<datafile<<" "<datafile<<" "<}
datafile<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<datafile<<" "<for(int i=0;i{
datafile<<" "<datafile<<" "<datafile<<" "<datafile<<" "<}
datafile<datafile.close();
myPicRECT.count =0;
myPicRECT.IsDrawing = false;
}
cout<<"Save OK!"<}
使用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个无法找到,一个误检。


推荐阅读
  • CC++如何复制 ... [详细]
  • 第3章 感受(一)——3.1. Hello world 经典版
    [回到目录]白话C++第3章.感受Helloworld!,HelloC++,我们来了!3.1.Helloworld经典版毫无疑义,一 ... [详细]
  • Here是指向最小代码的链接,如果消失了, ... [详细]
  • 解开一个困扰自己多时的小问题——从std::cout和endl说起
    解开一个困扰自己多时的小问题小序今天上班的时候问了一起工作的Sidney同学一个小问题,显然他是研究过了的,不过他当时没有给出我答案。这个问题着实困扰了我好长时间捏~~晚上吃的小葱蘸 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • Linux 程序设计学习笔记----动手编写makefile文件
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 第五周项目一——体验常成员函数(1)
    设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等。在设计中,由于求距离、求对称点等操作对原对象不能造成任何改变,所以,将这些函数设计为常成员函数是合适的,能够避免数据成 ... [详细]
  • 由CStringW(wchar_t)不能正常打印收集的
    WIN7、VS2010(工程字符集为Unicode):源代码如下:CStringWline;rs是ODBC取得的结果集(有汉字),调试发现line能成功读取line.Form ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了Android中的assets目录和raw目录的共同点和区别,包括获取资源的方法、目录结构的限制以及列出资源的能力。同时,还解释了raw目录中资源文件生成的ID,并说明了这些目录的使用方法。 ... [详细]
  • NetBPM的安装还是比较简单的,有比较详细的文档。1.当然是先下载运行程序了,netbpm-0.8.3.1.zip,官方网站ÿ ... [详细]
  • 【项目4 - 处理C++源代码的程序】(3)
    问题描述:(3)读入一个C++程序,输入m、n两个数字,从第m行起的n行代码将作为注释使用(即在这些行前面加上””),新程序保存到另一个.cpp文件中,并在屏幕上显示处理过的程序, ... [详细]
  • 下面想跟大家分享一下,请大家看下面一个例子,看看结果是什么?#include<iostream>usingnamespacestd;intmain() ... [详细]
author-avatar
优优绿园之时尚饰品_834
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有