OpenCV:如何创建与opencv_traincascade

时间:2016-02-01 07:35:04

标签: c++ ios opencv image-processing opencv3.0

正如我在上一篇文章here中所解释的那样,我正在尝试生成一些cascade.xml个文件来识别要在我的iOS应用中使用的欧元硬币。无论如何,我在理解如何生成.vec文件以作为opencv_traincascade的输入时遇到了很多困难。这是因为我听到很多不同意见:有人告诉我,矢量文件必须只包含仅包含要识别的对象的正面图像;而不是其他人(以及在我的教程中阅读)说,矢量文件必须包括" samples"图像,换句话说,opencv_createsamples添加了要识别的对象的随机背景。用:er的话来说:

opencv_createsamples -img positives/1.png -bg negatives.txt -info 1.txt -num 210 -maxxangle 0.0 -maxyangle 0.0 -maxzangle 0.9 -bgcolor 255 -bgthresh 8 -w 48 -h 48

生成12000张图像。 最后,我用:

创建了.vec文件

cat *.txt > positives.txt

opencv_createsamples -info positives.txt -bg negatives.txt -vec 2.vec -num 12600 -w 48 -h 48

所以,我想问一下以下两个包含在矢量文件中的正确图像:

enter image description here

enter image description here

此外,这是启动培训的最终命令?这是我到目前为止使用的那些:

opencv_traincascade -data final -vec 2.vec -bg negatives.txt -numPos 12000 -numNeg 3000 -numStages 20 -featureType HAAR -precalcValBufSize 2048 -precalcIdxBufSize 2048 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -w 48 -h 48 -mode ALL

其中.vec文件包含12000个样本图像(背景+硬币识别)。 如果.vec文件只包含正面图像(仅限硬币),我如何告诉opencv_traincascade使用样本图像进行训练?

我真的需要知道如何正确地做事,因为我已经开展了许多培训,导致没有正确的结果,因为他们需要花费数小时或数天来执行,我不能再浪费时间了。

感谢大家的关注。

更新 我设法用LBP创建了一个cascade.xml文件。看看如果我将一个用作训练样本的图像提供给一个简单的OpenCV程序会发生什么:

enter image description here

同时使用如下图像:

enter image description here

根本不起作用。我真的不知道我在哪里弄错了。

更新 也许首先将正面图像转换为灰度可能会有所帮助? enter image description here

2 个答案:

答案 0 :(得分:3)

我已经使用了INRIA培训的negative samples数据库http://pascal.inrialpes.fr/data/human/

和这个输入(png在硬币周围有alpha透明度):

enter image description here

使用此命令:

opencv_createsamples -img pos_color.png -num 10 -bg neg.txt -info test.dat -maxxangle 0.6 -maxyangle 0 -maxzangle 0.3 -maxidev 100 -bgcolor 0 -bgthresh 0

产生如下输出:

enter image description here

enter image description here

所以背景颜色显然不起作用。然而,在开始时转换为灰度为我提供了这样的输入:

enter image description here

和相同的命令产生如下输出:

enter image description here

enter image description here

enter image description here

enter image description here

我知道这不是你所有问题的答案,但也许它仍然有帮助。

答案 1 :(得分:2)

OpenCV级联(HAAR,LBP)可以极好地检测具有永久特征的对象。例如,所有面孔都有鼻子,眼睛和嘴巴在同一个地方。 OpenCV级联经过训练,可以搜索所需对象类中的常用特征,并忽略从一个对象到另一个对象发生变化的特征。问题是结论是级联使用矩形形状的搜索窗口,但硬币具有圆形形状。因此,硬币的图像总是具有背景的一部分。 因此,硬币的训练图像必须包括所有可能的背景,以便分类器可以忽略它们(否则它将仅在特定背景上检测硬币)。

因此,所有训练样本必须具有相同的图像比例,硬币的大小和位置(硬币在中心的方形图像,硬币的直径= 0.8-0.9图像宽度)和不同的背景!