opencv怎么训练分类器
时间: 2023-11-25 08:51:49 浏览: 42
Opencv训练分类器的步骤如下:
1. 收集需要训练的正、负样本,并对正样本进行归一化处理,负样本不处理,但是保证负像素尺寸 >= 正样品像素尺寸。
2. 生成正、负样本描述文件。
3. 样本训练。
具体步骤如下:
1. 收集需要训练的正、负样本,并对正样本进行归一化处理,负样本不处理,但是保证负像素尺寸 >= 正样品像素尺寸。
2. 生成正、负样本描述文件。使用opencv_annotation工具在一张大图中标定一个或多个需要检测的目标,生成正样本描述文件。使用opencv_createsamples工具制作positive sample的vec,生成负样本描述文件。
3. 样本训练。使用opencv_traincascade工具训练得到需要的cascade.xml。
代码示例:
1. 生成正样本描述文件
```shell
opencv_annotation --annotations=positive.txt --images=positive --maxxangle=0.5 --maxyangle=0.5 --maxzangle=0.5
```
2. 生成负样本描述文件
```shell
opencv_createsamples -info negative.txt -num 1000 -w 48 -h 48 -vec negative.vec
```
3. 样本训练
```shell
opencv_traincascade -data classifier -vec samples.vec -bg negative.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000 -numNeg 600 -w 48 -h 48
```