Opencv C++ Tutorial for making your own Haar Classifier

2023-05-16

 

Opencv C++ Tutorial for making your own Haar Classifier.

This Opencv C++ Article  is about how to make your own Haar Cascade Classifier.
Step1:
Create a Folder ImageSample.
In it Create two folders: opencv-positive-haar && NegativeFolder.
Prerequisites:
Set of Positive Images:
Images which need to be detected or in other words the actual objects.E.g. Face for Face detection,eyes for Eye detection,Pen for Pen Detection etc.
The more unique the Positive Images,the better our Classifier gets Trained.

  • For Training Classifier for face detection we need to collect a vast database of faces,where face belong to almost each age group,males and females,with and without mustaches and beards,with varied skin colour etc.
  • While when we need to train our classifier for one unique object, only one image can be enough.For e.g Image of a Company Logo,a Particular sign board etc.

Set of Negative Images: 
Images other than the desired Images or in other words the one without object in it.
It should be minimum 3 times the no. of positive images for better object recognition and should include all the backgrounds where you want your object to get detected.

Note: Below mentioned method is for Creating Training Set from a Single Positive Image.
Creating Samples:
 

  1. Place the Positive Image in a separated folder.(Here it is opencv-positive-haar).
    • Navigate to that folder which contains positive images(folder name is opencv-positive-haar).
      Right Click on that folder(opencv-positive-haar) and Open Command Window Here.
    • Or Navigate to that folder in cmd
  2. Then,
    Type : dir /b> positive.txt


Thus a positive.txt file will be created in the folder opencv-positive-haar.
Open that positive.txt file.


You will also find the name of the file positive.txt in it.
Delete it.
So,after deletion, the positive.txt file would only contain the names of the image.

 

Similarly,For Negative Images make a separate folder which will contain the negative images.
Make negative.txt file by typing: dir /b> negative.txt (Only after Navigating to that Folder)Then,

  1. Open the text  file negative.txt.
  2. Delete the file name "negative.txt" from it, so that it only contains the name of negative images


opencv_createsamples:
A large dataset of positive images is created by applying perspective transform (rotating the images at various angle and changing the intensity of light).
The amount of randmoness can be controlled by varing the command line arguments of opencv_createsamples.



Command line arguments:



-vec 
Name of the output file containing the positive samples for training.

 


-img 
Source object image (e.g., a company logo,a text file describng the path of postive images etc).

 


-bg 
Background description file; contains a list of images which are used as a background for randomly distorted versions of the object.

 


-num 
Number of positive samples to generate.

 


-bgcolor -bgthresh
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 withing bgcolor-bgthresh and bgcolor+bgthresh range are interpreted as transparent.

 


-inv
If specified, colors will be inverted.
 


-randinv
If specified, colors will be inverted randomly.
 


-maxidev 
Maximal intensity deviation of pixels in foreground samples.

 


-maxxangle 
-maxyangle 
-maxzangle 
Maximum rotation angles must be given in radians.

 


-show
Useful debugging option. If specified, each sample will be shown. Pressing Esc will continue the samples creation process without.
 


-w 
Width (in pixels) of the output samples.

 


-h 
Height (in pixels) of the output samples.

 


-pngoutput
With this option switched on opencv_createsamples tool generates a collection of PNG samples and a number of associated annotation files, instead of a single vec file.

 


Method 1
Navigate to the location where OpenCV is installed .
(In my PC the path is:D:\opencv2410\build\x86\vc10\bin.) 
And check in the bin\ folder if the .exe files (opencv_createsamples.exe, opencv_traincascade.exe , opencv_haartraining.exe )are present or not.

Copy all these .exe (opencv_createsamples.exe , opencv_traincascade.exe) in ImageSample folder.

To show the parameters that can be used for creating the samples type: cd opencv_createsamples.(i.e cd filename_for_creating_samples only after navigating to that folder through cmd).

i.e. "Right Click" That folder(ImageSample) while pressing "Shift Key"
      Click Open Command Window here.


Method 2:
1. Open the cmd. 2. Type cd /d (path_of_opencv_createsamples) (without curly bracket as shown in fig) 

 

 




The next step is to create a positive .vec file

  1. Open Notepad
  2. Type the following command for a Single Image called my_image_name.jpg:
  3. C:\Users\arjun\Desktop\ImageSample\opencv_createsamples.exe -img \opencv-positive-haar\img my_image_name.jpg -vec samples.vec -num 250 -w 30 -h 30 PAUSE
    Note:Though we have taken one positive image we are specifying -num 250.Because it will do perspective transformation and generate 250 positive images.
  4. And save it with .bat extension.
  5. Now double click the .bat file created.


Note:
C:\Users\arjun\Desktop\ImageSample\opencv_createsamples.exe : It is the File path So you need to give that path which is there in your PC.
Followed by -info negative.txt -vec samples.vec -num 250 -w 30 -h 30 PAUSE
where: negative.txt is the text file which contains path of negative samples
samples.vec: It is the name of the .vec file
num: refers to the number of positive image which we need to be generated in vec file.
w and h specifies the with and height.(If it is more the better the detection.So if possible take it 500)

To view the samples in the .vec file again create a .bat file with the code:
C:\Users\arjun\Desktop\ImageSample\opencv_createsamples.exe -vec samples.vec -w 30 -h 30 -show
i.e.
file_path -vec name_of_vecfile -w width -h height -show

Training the Classifier:
Navigate to the folder ImageSample.
Locate opencv_traincascade.exe

 




You can find the command line arguemnts of opencv_traincascade.exe

Common arguments:


-data 
Where the trained classifier should be stored.

 


-vec 
vec-file with positive samples (created by opencv_createsamples utility).

 


-bg 
Background description file.

 


-numPos 
-numNeg 
Number of positive/negative samples used in training for every classifier stage.

 


-numStages 
Number of cascade stages to be trained.

 


-precalcValBufSize 
Size of buffer for precalculated feature values (in Mb).

 


-precalcIdxBufSize 
Size of buffer for precalculated feature indices (in Mb). The more memory you have the faster the training process.

 


-baseFormatSave
This argument is actual in case of Haar-like features. If it is specified, the cascade will be saved in the old format.
 


-acceptanceRatioBreakValue
This argument is used to determine how precise your model should keep learning and when to stop. A good guideline is to train not further than 10e-5, to ensure the model does not overtrain on your training data. By default this value is set to -1 to disable this feature.
 



Cascade parameters:


-stageType 
Type of stages. Only boosted classifier are supported as a stage type at the moment.

 


-featureType<{HAAR(default), LBP}>
Type of features: HAAR - Haar-like features, LBP - local binary patterns.
 


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

 



Boosted classifer parameters:


-bt <{DAB, RAB, LB, GAB(default)}>
Type of boosted classifiers: DAB - Discrete AdaBoost, RAB - Real AdaBoost, LB - LogitBoost, GAB - Gentle AdaBoost.
 


-minHitRate 
Minimal desired hit rate for each stage of the classifier. Overall hit rate may be estimated as (min_hit_rate^number_of_stages).

 


-maxFalseAlarmRate 
Maximal desired false alarm rate for each stage of the classifier. Overall false alarm rate may be estimated as (max_false_alarm_rate^number_of_stages).

 


-weightTrimRate 
Specifies whether trimming should be used and its weight. A decent choice is 0.95.

 


-maxDepth 
Maximal depth of a weak tree. A decent choice is 1, that is case of stumps.

 


-maxWeakCount 
Maximal count of weak trees for every cascade stage. The boosted classifier (stage) will have so many weak trees (<=maxWeakCount), as needed to achieve the given -maxFalseAlarmRate.

 



Haar-like feature parameters:


-mode 
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 [Rainer2002] for more details.

 


Local Binary Patterns parameters:
Local Binary Patterns don’t have parameters.
 


Now,
First Create a Folder Classifier in ImageSample.
Thus its directory structure would be ImageSample/Classifier.

Create a .bat file with the following command:
C:\Users\arjun\Desktop\ImageSample\opencv_traincascade.exe -data classifier -vec samples.vec -bg NegativeFolder\negative.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 250 -numNeg 400 -w 30 -h 30 -mode ALL -precalcValBufSize 2048 -precalcldxBufSize 2048
Double Click the .bat file.

Note:
 

    1. C:\Users\arjun\Desktop\ImageSample\opencv_traincascase.exe : Is the path of the file.
    1. samples.vec: Is the name of the .vec file
    1. negative.txt: It is the text file which contains the path of all the negative images.

Thus we have successfully generated the haarcascade file with .xml extension in Classifier Folder..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Opencv C++ Tutorial for making your own Haar Classifier 的相关文章

  • Matlab 中 interp2 的类似 OpenCV Api

    有没有类似的功能 其工作原理与 interp2 x y frame z xd yd linear 0 在 OpenCV 中 功能cv remap 几乎可以满足您的要求 请参阅文档here http docs opencv org modul
  • 使用畸变从图像平面计算相机矢量

    我正在尝试使用相机模型来重建可以使用某些相机及其 外部 内部 参数拍摄的图像 这一点我没有任何问题 现在我想添加扭曲 正如它们中所描述的那样OpenCV https docs opencv org 4 x dc dbb tutorial p
  • 在opencv中保存帧而不压缩

    我正在尝试使用写 OpenCV 函数 我想保存帧 TIFF扩大 我遇到的问题是保存的图像被压缩 所以我无法使用它们 知道如何摆脱这种压缩吗 提前致谢 不要介意西奇说的话 TIFF 标志通过 LZW 压缩硬编码在 opencv 二进制文件中
  • 如何将 OpenCV 等待键与 Chaquopy 一起使用

    我正在尝试使用 Chaquopy 将计算机视觉应用程序移植到 Android 当我尝试运行脚本时 以下行中出现以下错误 cv2 waitKey 100 打印到嵌入式 python 控制台的错误是 java chaquopy CQPEnv c
  • 编译使用Basler相机的程序

    我正在尝试使用 Basler 相机捕获图像的 C 程序来工作 我拿到 来自制造商的代码 它应该 非常容易使用 但是 链接它有 成为一场噩梦 我的 C 时代已经过去了 最近只使用 Matlab 所以我可能会犯一些愚蠢的错误 但请赐教 代码如下
  • opencv 视频上的颜色阈值

    I am thresholding for a color range in an opencv video The goal is to seperate the B mode black and white information on
  • 如何解决 Python 'Pyzbar' 库的导入错误?

    我刚刚开始熟悉 Pyzbar 库 但是当使用decode方法我得到一个错误 这是代码 import cv2 import numpy as np import pyzbar code image cv2 imread C Users Ace
  • 在 Python 3.5 64 位上通过 pip 安装 OpenCV

    我尝试安装 OpenCV 但找不到任何合适的 pip 软件包 我决定上网查找有关如何安装它的官方文档 并发现this https opencv python tutroals readthedocs io en latest py tuto
  • brew 链接 jpeg 问题

    我正在尝试安装opencv在 Mac OSX Lion 上 brew install opencv 我收到以下错误 以及其他一些类似的错误 Error The linking step did not complete successful
  • Opencv未找到所有轮廓

    我试图找到该图像的轮廓 但是该方法查找轮廓只返回1轮廓 轮廓突出显示image 2 我正在努力寻找all外部轮廓就像这些圆圈 里面有数字 我究竟做错了什么 我可以做什么来实现它 image 1 image 2 以下是我的代码的相关部分 th
  • 使用 opencv warpPerspective() 生成道路的自上而下视图

    我正在尝试实施逆透视映射计算与道路上另一辆车的距离 我知道在应用该函数之前我需要生成一个包含源点和目标点的变换矩阵warpPerspective 但我不知道如何计算目的地点 我在这个论坛和其他网站中搜索 但无法将第一张图片转换为第二张图片
  • 如何设置K-means openCV c++的初始中心

    我正在尝试使用 OpenCv 和 Kmeans 对图像进行分割 我刚刚实现的代码如下 include opencv2 objdetect objdetect hpp include opencv2 highgui highgui hpp i
  • 如何计算图像中的 RGB 或 HSV 通道组合?

    我使用 python opencv 加载形状为 30 100 3 的图像 现在想要按颜色计算所有颜色的频率 我不是指单个通道 而是指通道组合 含义 3 个频道列表 例如 255 0 0 表示红色 255 255 0 表示黄色 100 100
  • 如何将 mat 转换为 array2d

    我为dlib http dlib net face landmark detection ex cpp html那里的面部地标代码使用 array2d 来获取图像 但我喜欢使用 Mat 读取图像并转换为 array2d 因为 dlib 仅支
  • 如何将输出视频保存到 OpenCV 中的文件中

    我想将输出视频保存到文件中而不是显示它并尝试使用 cvcaptureimage 但仍然无法获得结果 include
  • opencv 2.3.* 读取不工作

    我无法让 imread 工作 与这个人有同样的问题 OpenCV imwrite 2 2 在 Windows 7 上导致异常 并显示消息 OpenCV 错误 未指定错误 无法找到指定扩展名的编写器 https stackoverflow c
  • 如何在 OpenCV 中从 YUV 文件读取帧?

    如何在 OpenCV 中从 YUV 文件读取帧 我编写了一个非常简单的 python 代码来从二进制文件读取 YUV NV21 流 import cv2 import numpy as np class VideoCaptureYUV de
  • 检查图像中是否有太薄的区域

    我正在尝试验证雕刻机的黑白图像 更多的是剪贴画图像 不是照片 我需要考虑的主要事情之一是区域的大小 或线条的宽度 因为机器无法处理太细的线条 所以我需要找到比给定阈值更细的区域 以此图为例 竖琴的琴弦可能太细而无法雕刻 我正在阅读有关 Ma
  • OpenCV 2.3 与 VS 2008 - 鼠标事件

    强制性 我是新手 有一份涉及编程的工作 并且我一边工作一边自学 不用说 作为一名老师 我经常犯彻底的错误 我现在所处的位置 我创建了 Graph 类 它 令人惊讶的是 制作了图表 但现在我想通过单击鼠标来修改图形 但我似乎无法让鼠标处理程序
  • uri 警告中缺少端口:使用 Python OpenCV cv2.VideoCapture() 打开文件时出错

    当我尝试流式传输 ipcam 时 出现了如下所示的错误 tcp 000000000048c640 uri 中缺少端口 警告 打开文件时出错 build opencv modules videoio src cap ffmpeg impl h

随机推荐

  • latex打双引号“ “

    latex中如果用英文输入模式的双引号键入 xff0c 则输出的结果与我们预期的不符合 xff0c 这并不是LaTeX的正确输入方式 34 test 34 输出为 xff1a 正确的输入方式为 xff1a 引号左边输入两个反引号 96 xf
  • 过拟合的原因以及解决办法(深度学习)

    过拟合 xff1a 模型在训练集上表现的非常好 xff0c 但在测试集的数据下表现很差 具体观察loss函数就是 xff0c train loss一直降低 xff0c 而test loss先降低 xff0c 而后随着epoach的增加 xf
  • Linux与MAC共享以及TimeMachine服务器的搭建

    自从添置了MBPR之后 xff0c 就发现使用Samba协议的话 xff0c Linux与MacOS之间传输速度相当不稳定 xff0c 我还一度以为是MBP的无线网卡问题 随后便尝试了一下AFP协议 xff0c 果然效果立现 xff0c 因
  • Python字符串转数字

    默认转换方式 xff1a num 61 int string 把二进制 xff0c 八进制 xff0c 十六进制转化为数字 xff0c python也提供了内置函数 xff0c 非常方便 xff0c 用法分别如下 xff1a num1 61
  • Linux根据进程名字彻底删除所有相关的子进程

    Linux有些时候kill 9进程pid xff0c 进程名字还会出现 xff0c 比如spark提交应用时的SparkSubmit 这是因为当前进程有其它子进程依赖 此时可以根据进程名字彻底删除 xff0c 这里我提供了一份模板 xff1
  • Python中Json文件的写入与读取

    字典写入Json文件 xff0c 代码如下 xff1a import json sparkConfDict 61 39 defaultMaxSplitBytes 39 defaultMaxSplitBytes 39 openCostInBy
  • Python获取当前工作目录以及改变工作目录

    import os print os getcwd 获取并打印当前工作目录 os chdir 34 目标目录 34 修改当前工作目录为目标目录
  • Linux 手动杀VNC进程

    步骤 方法一 1 查VNC进程 span class token function ps span ef span class token operator span span class token function grep span
  • 记录我重新安装ORBSLAM2和PX4的过程

    1 背景 xff1a 今天卸载了Ubuntu16 04 xff0c 重新装了一个Ubuntu18 04 xff0c 成功做完系统之后需要把之前的备份恢复 我的备份比较粗暴 xff0c 就是直接把 home里的文件都先复制到Windows下
  • 【网络干货】最全BGP路由协议技术详解

    一 BGP 的基本概念 自治系统AS xff08 Autonomous System xff09 AS 是指在一个实体管辖下的拥有相同选路策略的 IP 网络 BGP 网络中的每个 AS 都被分配一个唯一的 AS 号 xff0c 用于区分不同
  • Python正则表达式之 - ?: / ?= / ?!

    用圆括号将所有选择项括起来 xff0c 相邻的选择项之间用 分隔 但用圆括号会有一个副作用 xff0c 使相关的匹配会被缓存 xff0c 此时可用 放在第一个选项前来消除这种副作用 其中 是非捕获元之一 xff0c 还有两个非捕获元是 61
  • Python教程:无参装饰器

    一 xff1a 储备知识 1 args xff0c kwargs span class token keyword def span span class token function index span span class token
  • 面向对象:类关系(泛化/实现/依赖/关联/聚合/组合)

    泛化 泛化 xff0c 也称作继承关系 指面向对象中派生类与基类之间的关系 xff0c 一个类 xff08 称为子类 子接口 xff09 继承另外的一个类 xff08 称为父类 父接口 xff09 的功能 指ClassA为ClassB Cl
  • webpack基本概念及使用

    webpack是什么 xff0c 用来干什么 xff1f webpack 是一个用于现代 JavaScript 应用程序的静态模块打包工具 xff1b webpack的下载安装 官网文档地址 xff1a https webpack js o
  • stlink制作(OSHW版)

    stlink制作安排 视频在我的B站 工程主页在开源硬件平台 0 项目原由 因为我那个板载stlink的NANO板近期要还给老师了 所以我就没有板子和stlink了 xff0c 但是对于一个stmer来说 xff0c 怎么能没stlink呢
  • JS对象销毁

    JS中对象销毁需要注意的几个方面 1 销毁你创建的其他对象 xff0c 并切断应用 2 解绑绑定事件 3 this上的成员变量 xff0c 需要切断引用的要切断 4 有继承时 xff0c 需要调用父类的销毁方法 5 清除dom结构
  • px4初级视频

    链接 xff1a https pan baidu com s 1VIQcOQt I5 evMx1jnV0ZQ 提取码 xff1a 8niq
  • 找工作、备考、面试刷题网站推荐(牛客网、力扣、计蒜客、hihocoder、七月在线)以及acm竞赛oj

    不管是找工作笔试面试白板试进大厂 xff0c 还是研究生参加初试复试 xff0c 数据结构和算法都是都是重中之重 xff0c 刷题就很必要 xff0c 来拿走自己的offer 吧 xff01 一 offer刷题推荐 1 牛客网 链接 xff
  • 安装Homebrew报错 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

    网上有很多方式 xff0c 这里只说自己解决的方式 xff0c 只针对mac 1 打开网站 https www ipaddress com 查询 raw githubusercontent com 对应的ip地址 2 将查询出来的地址映射加
  • Opencv C++ Tutorial for making your own Haar Classifier

    Opencv C 43 43 Tutorial for making your own Haar Classifier This Opencv C 43 43 Article is about how to make your own Ha