因為正樣本最后需要大小歸一化,所以我在采集樣本的時候就直接把它從原圖里摳出來了,方便后面縮放嘛,而不是只保存它的框個數和框位置信息(框個數、框位置信息看下一步解釋),在裁剪的過程中盡量保持樣本的長寬比例一致。比如我最后要歸一化成20 X 20,在裁剪樣本的時候,我都是20X20或者21X21、22X22等等,最大我也沒有超過30X30(不超過跟我的自身用途有關,對于人臉檢測這種要保證縮放不變性的樣本,肯定就可以超過啦),我資源里也給出可以直接用的裁剪樣本程序。
/** cvCreateTrainingSamples** Create training samples applying random distortions to sample image and* store them in .vec file** filename - .vec file name* imgfilename - sample image file name* bgcolor - background color for sample image* bgthreshold - background color threshold. Pixels those colors are in range* [bgcolor-bgthreshold, bgcolor+bgthreshold] are considered as transparent* bgfilename - background description file name. If not NULL samples* will be put on arbitrary background* count - desired number of samples* invert - if not 0 sample foreground pixels will be inverted* if invert == CV_RANDOM_INVERT then samples will be inverted randomly* maxintensitydev - desired max intensity deviation of foreground samples pixels* maxxangle - max rotation angles* maxyangle* maxzangle* showsamples - if not 0 samples will be shown* winwidth - desired samples width* winheight - desired samples height*/2)提供imagename、bgfilename和infoname時
與1)類似
3)提供 infoname和 vecname時,調用以下操作
(這里是我們訓練需要的)
[cpp] view plaincopyprint?
/** cvCreateTrainingSamplesFromInfo** Create training samples from a set of marked up images and store them into .vec file* infoname - file in which marked up image descriptions are stored* num - desired number of samples* showsamples - if not 0 samples will be shown* winwidth - sample width* winheight - sample height* * Return number of successfully created samples*/
int cvCreateTrainingSamplesFromInfo( const char* infoname, const char* vecfilename,int num,int showsamples,int winwidth, int winheight )
Haar Feature-based Cascade Classifier for Object Detection
The object detector described below has been initially proposed by Paul Viola?[pdf]?and improved by Rainer Lienhart?[pdf]?.
First, a classifier (namely a?cascade of boosted classifiers working with haar-like features) is trained with a few hundred sample views of a particular object (i.e., a face or a car), called positive examples, that are scaled to the same size (say, 20x20), and negative examples - arbitrary images of the same size.
After a classifier is trained, it can be applied to a region of interest (of the same size as used during the training) in an input image. The classifier outputs a “1” if the region is likely to show the object (i.e., face/car), and “0” otherwise. To search for the object in the whole image one can move the search window across the image and check every location using the classifier. The classifier is designed so that it can be easily “resized” in order to be able to find the objects of interest at different sizes, which is more efficient than resizing the image itself. So, to find an object of an unknown size in the image the scan procedure should be done several times at different scales.
The word “cascade” in the classifier name means that the resultant classifier consists of several simpler classifiers (stages) that are applied subsequently to a region of interest until at some stage the candidate is rejected or all the stages are passed. The word “boosted” means that the classifiers at every stage of the cascade are complex themselves and they are built out of basic classifiers using one of four different boosting techniques (weighted voting). Currently Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported. The basic classifiers are decision-tree classifiers with at least 2 leaves. Haar-like features are the input to the basic classifiers, and are calculated as described below. The current algorithm uses the following Haar-like features:
image
The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within the region of interest and the scale (this scale is not the same as the scale used at the detection stage, though these two scales are multiplied). For example, in the case of the third line feature (2c) the response is calculated as the difference between the sum of image pixels under the rectangle covering the whole feature (including the two white stripes and the black stripe in the middle) and the sum of the image pixels under the black stripe multiplied by 3 in order to compensate for the differences in the size of areas. The sums of pixel values over a rectangular regions are calculated rapidly using integral images (see below and the integral description).
To see the object detector at work, have a look at the facedetect demo:?https://github.com/Itseez/opencv/tree/master/samples/cpp/dbt_face_detection.cpp
The following reference is for the detection part only. There is a separate application called opencv_traincascade that can train a cascade of boosted classifiers from a set of samples.
Note
In the new C++ interface it is also possible to use LBP (local binary pattern) features in addition to Haar-like features. .. [Viola01] Paul Viola and Michael J. Jones. Rapid Object Detection using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. The paper is available online at?https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf(上述有提到)
7、opencv關于boost
(boost.cpp——opencv3.0)
Boosting
A common machine learning task is supervised learning. In supervised learning, the goal is to learn the functional relationship?F:y=F(x)?between the input?x?and the output?y?. Predicting the qualitative output is called?classification, while predicting the quantitative output is called?regression.
Boosting is a powerful learning concept that provides a solution to the supervised classification learning task. It combines the performance of many “weak” classifiers to produce a powerful committee?[125]?. A weak classifier is only required to be better than chance, and thus can be very simple and computationally inexpensive. However, many of them smartly combine results to a strong classifier that often outperforms most “monolithic” strong classifiers such as SVMs and Neural Networks.
Decision trees are the most popular weak classifiers used in boosting schemes. Often the simplest decision trees with only a single split node per tree (called stumps ) are sufficient.
The boosted model is based on?N?training examples?(xi,yi)1N?with?xi∈RK?and?yi∈?1,+1?.?xi?is a?K?-component vector. Each component encodes a feature relevant to the learning task at hand. The desired two-class output is encoded as -1 and +1.
Different variants of boosting are known as Discrete Adaboost, Real AdaBoost, LogitBoost, and Gentle AdaBoost?[49]?. All of them are very similar in their overall structure. Therefore, this chapter focuses only on the standard two-class Discrete AdaBoost algorithm, outlined below. Initially the same weight is assigned to each sample (step 2). Then, a weak classifier?fm(x)?is trained on the weighted training data (step 3a). Its weighted training error and scaling factor?cm?is computed (step 3b). The weights are increased for training samples that have been misclassified (step 3c). All weights are then normalized, and the process of finding the next weak classifier continues for another?M?-1 times. The final classifier?F(x)?is the sign of the weighted sum over the individual weak classifiers (step 4).
Two-class Discrete AdaBoost Algorithm
Set?N?examples?(xi,yi)1N?with?xi∈RK,yi∈?1,+1?.
Assign weights as?wi=1/N,i=1,...,N?.
Repeat for?m=1,2,...,M?:
Fit the classifier?fm(x)∈?1,1, using weights?wi?on the training data.
Set?wi?wiexp[cm1(yi≠fm(xi))],i=1,2,...,N,?and renormalize so that?Σiwi=1?.
Classify new samples?x?using the formula:?sign(Σm=1Mcmfm(x))?.
Note
Similar to the classical boosting methods, the current implementation supports two-class classifiers only. For M > 2 classes, there is theAdaBoost.MH?algorithm (described in?[49]) that reduces the problem to the two-class problem, yet with a much larger training set.
To reduce computation time for boosted models without substantially losing accuracy, the influence trimming technique can be employed. As the training algorithm proceeds and the number of trees in the ensemble is increased, a larger number of the training samples are classified correctly and with increasing confidence, thereby those samples receive smaller weights on the subsequent iterations. Examples with a very low relative weight have a small impact on the weak classifier training. Thus, such examples may be excluded during the weak classifier training without having much effect on the induced classifier. This process is controlled with the weight_trim_rate parameter. Only examples with the summary fraction weight_trim_rate of the total weight mass are used in the weak classifier training. Note that the weights for?all?training examples are recomputed at each training iteration. Examples deleted at a particular iteration may be used again for learning some of the weak classifiers further?[49]
See also
cv::ml::Boost
Prediction with Boost
StatModel::predict(samples, results, flags) should be used. Pass flags=StatModel::RAW_OUTPUT to get the raw sum from Boost classifier.