OpenCV训练分类器

简介:
OpenCV训练分类器
一、简介
    目标检测方法最初由Paul Viola [Viola01]提出,并由Rainer Lienhart [Lienhart02]对这一方法进行了改善。该方法的基本步骤为: 首先,利用样本(大约几百幅样本图片)的 harr 特征进行分类器训练,得到一个级联的boosted分类器。
    分类器中的"级联"是指最终的分类器是由几个简单分类器级联组成。在图像检测中,被检窗口依次通过每一级分类器, 这样在前面几层的检测中大部分的候选区域就被排除了,全部通过每一级分类器检测的区域即为目标区域。
    分类器训练完以后,就可以应用于输入图像中的感兴趣区域(与训练样本相同的尺寸)的检测。检测到目标区域(汽车或人脸)分类器输出为1,否则输出为0。为了检测整副图像,可以在图像中移动搜索窗口,检测每一个位置来确定可能的目标。为了搜索不同大小的目标物体,分类器被设计为可以进行尺寸改变,这样比改变待检图像的尺寸大小更为有效。所以,为了在图像中检测未知大小的目标物体,扫描程序通常需要用不同比例大小的搜索窗口对图片进行几次扫描。
    目前支持这种分类器的boosting技术有四种: Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost。
"boosted" 即指级联分类器的每一层都可以从中选取一个boosting算法(权重投票),并利用基础分类器的自我训练得到。
    根据上面的分析,目标检测分为三个步骤:
      1、 样本的创建
      2、 训练分类器
      3、 利用训练好的分类器进行目标检测。
二、样本创建
    训练样本分为正例样本和反例样本,其中正例样本是指待检目标样本(例如人脸或汽车等),反例样本指其它任意图片,所有的样本图片都被归一化为同样的尺寸大小(例如,20x20)。
    负样本
       负样本可以来自于任意的图片,但这些图片不能包含目标特征。负样本由背景描述文件来描述。背景描述文件是一个文本文件,每一行包含了一个负样本图片的文件名(基于描述文件的相对路径)。该文件必须手工创建。
      e.g: 负样本描述文件的一个例子:
      假定目录结构如下:
      /img
        img1.jpg
        img2.jpg
        bg.txt
      则背景描述文件bg.txt的内容为:
         img/img1.jpg
         img/img2.jpg
    正样本
       正样本由程序craatesample程序来创建。该程序的源代码由OpenCV给出,并且在bin目录下包含了这个可执行的程序。
       正样本可以由单个的目标图片或者一系列的事先标记好的图片来创建。
   Createsamples程序的命令行参数:
     命令行参数:
     -vec <vec_file_name>
          训练好的正样本的输出文件名。
     -img<image_file_name>
          源目标图片(例如:一个公司图标)
     -bg<background_file_name>
          背景描述文件。
     -num<number_of_samples>
          要产生的正样本的数量,和正样本图片数目相同。
     -bgcolor<background_color>
          背景色(假定当前图片为灰度图)。背景色制定了透明色。对于压缩图片,颜色方差量由bgthresh参数来指定。则在bgcolor-bgthresh和bgcolor+bgthresh中间的像素被认为是透明的。
     -bgthresh<background_color_threshold>
     -inv
          如果指定,颜色会反色
     -randinv
          如果指定,颜色会任意反色
     -maxidev<max_intensity_deviation>
          背景色最大的偏离度。
     -maxangel<max_x_rotation_angle>
     -maxangle<max_y_rotation_angle>,
     -maxzangle<max_x_rotation_angle>
          最大旋转角度,以弧度为单位。
     -show
          如果指定,每个样本会被显示出来,按下"esc"会关闭这一开关,即不显示样本图片,而创建过程继续。这是个有用的debug选项。
     -w<sample_width>
          输出样本的宽度(以像素为单位)
     -h《sample_height》
          输出样本的高度,以像素为单位。
注:正样本也可以从一个预先标记好的图像集合中获取。这个集合由一个文本文件来描述,类似于背景描述文件。每一个文本行对应一个图片。每行的第一个元素是图片文件名,第二个元素是对象实体的个数。后面紧跟着的是与之匹配的矩形框(x, y, 宽度,高度)。
    下面是一个创建样本的例子:
假定我们要进行人脸的检测,有5个正样本图片文件img1.bmp,…img5.bmp;有2个背景图片文件:bg1.bmp,bg2.bmp,文件目录结构如下:
positive
    img1.bmp
    ……
    Img5.bmp
negative
    bg1.bmp
    bg2.bmp
info.dat
bg.txt
  正样本描述文件info.dat的内容如下:
     Positive/imag1.bmp 1 0 0 24 28
     ……
     Positive/imag5.bmp 1 0 0 24 28
       图片img1.bmp包含了单个目标对象实体,矩形为(0,0,24,28)。
注意:要从图片集中创建正样本,要用-info参数而不是用-img参数。
-info <collect_file_name>
    标记特征的图片集合的描述文件。
背景(负样本)描述文件的内容如下:
   nagative/bg1.bmp
   nagative/bg2.bmp
我们用一个批处理文件run.bat来进行正样本的创建:该文件的内容如下:
cd    e:\face\bin
CreateSamples     -vec e:\face\a.vec
-info e:\face\info.dat
-bg e:\face\bg.txt
-num 5
-show
-w 24
-h 28
其中e:\face\bin目录包含了createsamples可执行程序,生成的正样本文件a.vec在e:\face目录下。
三、训练分类器
    样本创建之后,接下来要训练分类器,这个过程是由haartraining程序来实现的。该程序源码由OpenCV自带,且可执行程序在OpenCV安装目录的bin目录下。
Haartraining的命令行参数如下:
-data<dir_name>
    存放训练好的分类器的路径名。
-vec<vec_file_name>
    正样本文件名(由trainingssamples程序或者由其他的方法创建的)
-bg<background_file_name>
    背景描述文件。
-npos<number_of_positive_samples>,
-nneg<number_of_negative_samples>
    用来训练每一个分类器阶段的正/负样本。合理的值是:nPos = 7000;nNeg = 3000
-nstages<number_of_stages>
    训练的阶段数。
-nsplits<number_of_splits>
    决定用于阶段分类器的弱分类器。如果1,则一个简单的stump classifier被使用。如果是2或者更多,则带有number_of_splits个内部节点的CART分类器被使用。
-mem<memory_in_MB>
    预先计算的以MB为单位的可用内存。内存越大则训练的速度越快。
-sym(default)
-nonsym
    指定训练的目标对象是否垂直对称。垂直对称提高目标的训练速度。例如,正面部是垂直对称的。
-minhitrate《min_hit_rate》
    每个阶段分类器需要的最小的命中率。总的命中率为min_hit_rate的number_of_stages次方。
-maxfalsealarm<max_false_alarm_rate>
    没有阶段分类器的最大错误报警率。总的错误警告率为max_false_alarm_rate的number_of_stages次方。
-weighttrimming<weight_trimming>
    指定是否使用权修正和使用多大的权修正。一个基本的选择是0.9
-eqw
-mode<basic(default)|core|all>
    选择用来训练的haar特征集的种类。basic仅仅使用垂直特征。all使用垂直和45度角旋转特征。
-w《sample_width》
-h《sample_height》
    训练样本的尺寸,(以像素为单位)。必须和训练样本创建的尺寸相同。
一个训练分类器的例子:
    同上例,分类器训练的过程用一个批处理文件run2.bat来完成:
cd e:\face\bin
haartraining -data e:\face\data
-vec e:\face\a.vec
-bg e:\face\bg.txt
-npos 5
-nneg 2
-w 24
-h 28
训练结束后,会在目录data下生成一些子目录,即为训练好的分类器。

注:OpenCv 的某些版本可以将这些目录中的分类器直接转换成xml文件。但在实际的操作中,haartraining程序却好像永远不会停止,而且没有生成xml文件,后来在OpenCV的yahoo论坛上找到一个haarconv的程序,才将分类器转换为xml文件,其中的原因尚待研究。


英文原文如下:

Rapid Object Detection With A Cascade of Boosted Classifiers Based on Haar-like Features

Introduction

This document describes how to train and use a cascade of boosted classifiers for rapid object detection. A large set of over-complete haar-like features provide the basis for the simple individual classifiers. Examples of object detection tasks are face, eye and nose detection, as well as logo detection.

 

The sample detection task in this document is logo detection, since logo detection does not require the collection of large set of registered and carefully marked object samples. Instead we assume that from one prototype image, a very large set of derived object examples can be derived (createsamples utility, see below).

 

A detailed description of the training/evaluation algorithm can be found in [1] and [2].

Samples Creation

For training a training samples must be collected. There are two sample types: negative samples and positive samples. Negative samples correspond to non-object images. Positive samples correspond to object images.

Negative Samples

Negative samples are taken from arbitrary images. These images must not contain object representations. Negative samples are passed through background description file. It is a text file in which each text line contains the filename (relative to the directory of the description file) of negative sample image. This file must be created manually. Note that the negative samples and sample images are also called background samples or background samples images, and are used interchangeably in this document

 

Example of negative description file:

 

Directory structure:

/img

  img1.jpg

  img2.jpg

bg.txt

 

File bg.txt:

img/img1.jpg

img/img2.jpg

Positive Samples

Positive samples are created by createsamples utility. They may be created from single object image or from collection of previously marked up images.

The single object image may for instance contain a company logo. Then are large set of positive samples are created from the given object image by randomly rotating, changing the logo color as well as placing the logo on arbitrary background.

The amount and range of randomness can be controlled by command line arguments.

Command line arguments:

- vec <vec_file_name>

name of the output file containing the positive samples for training

- img <image_file_name>

source object image (e.g., a company logo)

- bg <background_file_name>

background description file; contains a list of images into which randomly distorted versions of the object are pasted for positive sample generation

- num <number_of_samples>

number of positive samples to generate

- bgcolor <background_color>

      background color (currently grayscale images are assumed); the background color denotes the transparent color. Since there might be compression artifacts, the amount of color tolerance can be specified by –bgthresh. All pixels between bgcolor-bgthresh and bgcolor+bgthresh are regarded as transparent.

- bgthresh <background_color_threshold>

- inv

      if specified, the colors will be inverted

- randinv

      if specified, the colors will be inverted randomly

- maxidev <max_intensity_deviation>

  maximal intensity deviation of foreground samples pixels

- maxxangle <max_x_rotation_angle>,

- maxyangle <max_y_rotation_angle>,

- maxzangle <max_z_rotation_angle>

      maximum rotation angles in radians

-show

      if specified, each sample will be shown. Pressing ‘Esc’ will continue creation process without samples showing. Useful debugging option.

- w <sample_width>

  width (in pixels) of the output samples

- h <sample_height>

  height (in pixels) of the output samples

 

For following procedure is used to create a sample object instance:

The source image is rotated random around all three axes. The chosen angle is limited my -max?angle. Next pixels of intensities in the range of [bg_color-bg_color_threshold; bg_color+bg_color_threshold] are regarded as transparent. White noise is added to the intensities of the foreground. If –inv key is specified then foreground pixel intensities are inverted. If –randinv key is specified then it is randomly selected whether for this sample inversion will be applied. Finally, the obtained image is placed onto arbitrary background from the background description file, resized to the pixel size specified by –w and –h and stored into the file specified by the –vec command line parameter.

 

Positive samples also may be obtained from a collection of previously marked up images. This collection is described by text file similar to background description file. Each line of this file corresponds to collection image. The first element of the line is image file name. It is followed by number of object instances. The following numbers are the coordinates of bounding rectangles (x, y, width, height).

 

Example of description file:

 

Directory structure:

/img

  img1.jpg

  img2.jpg

info.dat

 

File info.dat:

img/img1.jpg  1  140 100 45 45

img/img2.jpg  2  100 200 50 50   50 30 25 25

 

Image img1.jpg contains single object instance with bounding rectangle (140, 100, 45, 45). Image img2.jpg contains two object instances.

 

In order to create positive samples from such collection –info argument should be specified instead of –img:

- info <collection_file_name>

description file of marked up images collection

 

The scheme of sample creation in this case is as follows. The object instances are taken from images. Then they are resized to samples size and stored in output file. No distortion is applied, so the only affecting arguments are –w, -h, -show and –num.

 

createsamples utility may be used for examining samples stored in positive samples file. In order to do this only –vec, –w and –h parameters should be specified.

 

Note that for training, it does not matter how positive samples files are generated. So the createsamples utility is only one way to collect/create a vector file of positive samples.

Training

The next step after samples creation is training of classifier. It is performed by the haartraining utility.

 

Command line arguments:

- data <dir_name>

      directory name in which the trained classifier is stored

- vec <vec_file_name>

      file name of positive sample file (created by trainingsamples utility or by any other means)

- bg <background_file_name>

      background description file

- npos <number_of_positive_samples>,

- nneg <number_of_negative_samples>

      number of positive/negative samples used in training of each classifier stage. Reasonable values are npos = 7000 and nneg = 3000.

- nstages <number_of_stages>

  number of stages to be trained

- nsplits <number_of_splits>

      determines the weak classifier used in stage classifiers. If 1, then a simple stump classifier is used, if 2 and more, then CART classifier with number_of_splits internal (split) nodes is used

- mem <memory_in_MB>

      Available memory in MB for precalculation. The more memory you have the faster the training process

- sym (default),

- nonsym

      specifies whether the object class under training has vertical symmetry or not. Vertical symmetry speeds up training process. For instance, frontal faces show off vertical symmetry

- minhitrate <min_hit_rate>

      minimal desired hit rate for each stage classifier. Overall hit rate may be estimated as (min_hit_rate^number_of_stages)

- maxfalsealarm <max_false_alarm_rate>

      maximal desired false alarm rate for each stage classifier. Overall false alarm rate may be estimated as (max_false_alarm_rate^number_of_stages)

- weighttrimming <weight_trimming>

  Specifies wheter and how much weight trimming should be used. A decent choice is 0.90.

- eqw

- mode <BASIC (default) | CORE | ALL>

      selects the type of haar features set used in training. BASIC use only upright features, while ALL uses the full set of upright and 45 degree rotated feature set. See [1] for more details.

- w <sample_width>,

- h <sample_height>

      Size of training samples (in pixels). Must have exactly the same values as used during training samples creation (utility trainingsamples)

 

Note: in order to use multiprocessor advantage a compiler that supports OpenMP 1.0 standard should be used.

Application

OpenCV cvHaarDetectObjects() function (in particular haarFaceDetect demo) is used for detection.

Test Samples

In order to evaluate the performance of trained classifier a collection of marked up images is needed. When such collection is not available test samples may be created from single object image bycreatesamples utility. The scheme of test samples creation in this case is similar to training samples creation since each test sample is a background image into which a randomly distorted and randomly scaled instance of the object picture is pasted at a random position.

 

If both –img and –info arguments are specified then test samples will be created by createsamples utility. The sample image is arbitrary distorted as it was described below, then it is placed at random location to background image and stored. The corresponding description line is added to the file specified by –info argument.

 

The –w and –h keys determine the minimal size of placed object picture.

 

The test image file name format is as follows:

imageOrderNumber_x_y_width_height.jpg, where x, y, width and height are the coordinates of placed object bounding rectangle.

Note that you should use a background images set different from the background image set used during training.

Performance Evaluation

In order to evaluate the performance of the classifier performance utility may be used. It takes a collection of marked up images, applies the classifier and outputs the performance, i.e. number of found objects, number of missed objects, number of false alarms and other information.

 

Command line arguments:

- data <dir_name>

      directory name in which the trained classifier is stored

- info <collection_file_name>

      file with test samples description

- maxSizeDiff <max_size_difference>,

- maxPosDiff <max_position_difference>

      determine the criterion of reference and detected rectangles coincidence. Default values are 1.5 and 0.3 respectively.

- sf <scale_factor>,

      detection parameter. Default value is 1.2.

- w <sample_width>,

- h <sample_height>

      Size of training samples (in pixels). Must have exactly the same values as used during training (utility haartraining)

References

[1] Rainer Lienhart and Jochen Maydt. An Extended Set of Haar-like Features for Rapid Object Detection. Submitted to ICIP2002.

[2] Alexander Kuranov, Rainer Lienhart, and Vadim Pisarevsky. An Empirical Analysis of Boosting Algorithms for Rapid Objects With an Extended Set of Haar-like Features. Intel Technical Report MRL-TR-July02-01, 2002.


相关文章
|
XML 机器学习/深度学习 存储
基于OpenCV训练口罩检测数据集并测试
基于OpenCV训练口罩检测数据集并测试
388 0
基于OpenCV训练口罩检测数据集并测试
|
XML 数据采集 计算机视觉
基于opencv的haar训练自己的识别器【含 opencv_traincascade.exe和opencv_haartraining.exe下载】
基于opencv的haar训练自己的识别器【含 opencv_traincascade.exe和opencv_haartraining.exe下载】
541 0
基于opencv的haar训练自己的识别器【含 opencv_traincascade.exe和opencv_haartraining.exe下载】
|
XML 编译器 C语言
OpenCV编程:OpenCV3.X训练自己的分类器
OpenCV编程:OpenCV3.X训练自己的分类器
199 0
OpenCV编程:OpenCV3.X训练自己的分类器
|
计算机视觉 机器学习/深度学习 数据格式
|
机器学习/深度学习 计算机视觉 iOS开发
OpenCV实现手写体数字训练与识别
OpenCV实现手写体数字训练与识别 机器学习(ML)是OpenCV模块之一,对于常见的数字识别与英文字母识别都可以做到很高的识别率,完成这类应用的主要思想与方法是首选对训练图像数据完成预处理与特征提取,根据特征数据组成符合OpenCV要求的训练数据集与标记集,然后通过机器学习的KNN、SVM、ANN等方法完成训练,训练结束之后保存训练结果,对待检测的图像完成分割、二值化、ROI等操作之后,加载训练好的分类数据,就可以预言未知分类。
2670 0
|
XML 计算机视觉 数据格式
opencv 之 icvCreateHidHaarClassifierCascade 分类器信息初始化函数部分详细代码注释。
请看注释。这个函数,是人脸识别主函数,里面出现过的函数之一,作用是初始化分类器的数据,就是一个xml文件的数据初始化。 1 static CvHidHaarClassifierCascade* icvCreateHidHaarClassifierCascade( CvHaarClassi...
763 0
|
1月前
|
计算机视觉
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
这篇文章详细介绍了OpenCV库中的图像二值化函数`cv2.threshold`,包括二值化的概念、常见的阈值类型、函数的参数说明以及通过代码实例展示了如何应用该函数进行图像二值化处理,并展示了运行结果。
344 0
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
|
2月前
|
算法 计算机视觉
opencv图像形态学
图像形态学是一种基于数学形态学的图像处理技术,它主要用于分析和修改图像的形状和结构。
49 4
|
2月前
|
存储 计算机视觉
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
本文介绍了使用OpenCV进行图像读取、显示和存储的基本操作,以及如何绘制直线、圆形、矩形和文本等几何图形的方法。
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
|
3月前
|
算法 计算机视觉 Python
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
该文章详细介绍了使用Python和OpenCV进行相机标定以获取畸变参数,并提供了修正图像畸变的全部代码,包括生成棋盘图、拍摄标定图像、标定过程和畸变矫正等步骤。
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
下一篇
无影云桌面