OpenCV HOGDescripter Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28390614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV HOGDescripter Python
提问by fish
I was wondering if anyone knew why there is no documentation for HOGDescriptors in the Python bindings of OpenCV.
我想知道是否有人知道为什么 OpenCV 的 Python 绑定中没有关于 HOGDescriptor 的文档。
Maybe I've just missed them, but the only code I've found of them is this thread: Get HOG image features from OpenCV + Python?
也许我只是错过了它们,但我发现它们的唯一代码是这个线程:从 OpenCV + Python 获取 HOG 图像特征?
If you scroll down in that thread, this code is found in there:
如果您在该线程中向下滚动,则会在其中找到此代码:
import cv2
hog = cv2.HOGDescriptor()
im = cv2.imread(sample)
h = hog.compute(im)
I've tested this and it works -- so the Python Bindings do exist, just the documentation doesn't. I was wondering if anyone knew why documentation for the Python bindings for HOG is so difficult to find / non-existent. Does anyone know if there is a tutorial I can read anywhere about HOG (especially via the Python Bindings)? I'm new to HOG and would like to see a few examples of how OpenCV does stuff before I start writing my own stuff.
我已经测试过它并且它有效——所以 Python 绑定确实存在,只是文档不存在。我想知道是否有人知道为什么 HOG 的 Python 绑定文档很难找到/不存在。有谁知道是否有我可以在任何地方阅读有关 HOG 的教程(尤其是通过 Python 绑定)?我是 HOG 的新手,希望在我开始编写自己的东西之前先看看 OpenCV 如何做东西的几个例子。
回答by mdilip
1. Get Inbuilt Documentation:Following command on your python console will help you know the structure of class HOGDescriptor:
1.获取内置文档:在你的python控制台上执行以下命令将帮助你了解HOGDescriptor类的结构:
import cv2
help(cv2.HOGDescriptor())
import cv2
help(cv2.HOGDescriptor())
2. Example code:Here is a snippet of code to initialize an cv2.HOGDescriptor with different parameters (The terms I used here are standard terms which are well defined in OpenCV documentation here):
2。实施例的代码:这是一个代码片段来初始化不同参数的cv2.HOGDescriptor(I这里所用的术语是被OpenCV的文档中良好定义的标准条款这里):
import cv2
image = cv2.imread("test.jpg",0)
winSize = (64,64)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 9
derivAperture = 1
winSigma = 4.
histogramNormType = 0
L2HysThreshold = 2.0000000000000001e-01
gammaCorrection = 0
nlevels = 64
hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,
histogramNormType,L2HysThreshold,gammaCorrection,nlevels)
#compute(img[, winStride[, padding[, locations]]]) -> descriptors
winStride = (8,8)
padding = (8,8)
locations = ((10,20),)
hist = hog.compute(image,winStride,padding,locations)
3. Reasoning:The resultant hog descriptor will have dimension as: 9 orientations X (4 corner blocks that get 1 normalization + 6x4 blocks on the edges that get 2 normalizations + 6x6 blocks that get 4 normalizations) = 1764. as I have given only one location for hog.compute().
3. 推理:生成的猪描述符将具有以下维度:9 个方向 X(4 个角块获得 1 个归一化 + 边缘上的 6x4 个块获得 2 个归一化 + 6x6 个块获得 4 个归一化)= 1764。正如我只给出的hog.compute() 的一个位置。
4. Different way to initialize HOGDescriptor:
One more way to initialize is from xml file which contains all parameter values:
4. 初始化 HOGDescriptor 的不同方式:另
一种初始化方式是从包含所有参数值的 xml 文件:
hog = cv2.HOGDescriptor("hog.xml")
To get an xml file one can do following:
要获取 xml 文件,可以执行以下操作:
hog = cv2.HOGDescriptor()
hog.save("hog.xml")
and edit the respective parameter values in xml file.
并在 xml 文件中编辑相应的参数值。
回答by linbianxiaocao
I was wondering the same. Almost none documentation can be found for OpenCV HOGDescriptor, other than the source cpp code.
我也想知道。除了源 cpp 代码之外,几乎找不到 OpenCV HOGDescriptor 的文档。
Scikit-image has a good example page on extracting and illustrating HOG feature. It provides an alternative to explore HOG. It is documented here.
Scikit-image 有一个关于提取和说明 HOG 特征的很好的示例页面。它提供了探索 HOG 的另一种选择。它记录在此处。
However, there is one thing to point out about scikit-image's hog implementation. Its Python code for hog functiondoes not implement weighted votefor histogram orientation binning, but only does simple binning based on magnitude value falling into which bin. See its hog_histogram function. This is not following exactly Dalal and Triggs's paper.
但是,关于 scikit-image 的 hog 实现有一点需要指出。它的hog 函数的Python 代码没有实现对直方图方向分箱的加权投票,而只是根据落入哪个分箱的幅度值进行简单的分箱。查看它的hog_histogram 函数。这并没有完全遵循 Dalal 和 Triggs 的论文。
Actually, I found that object detection based on OpenCV's implementation of HOG is more accurate than with the api from scikit-image. It makes sense to me, because weighted vote is important. By casting weighted votes to bins, variation in histogram is greatly reduced when gradient magnitude falls on or around the boundary. Chris McCormick wrote a very insightful blogon hog, in which orientation binning is clearly described as
实际上,我发现基于 OpenCV 的 HOG 实现的对象检测比使用 scikit-image 的 api 更准确。这对我来说很有意义,因为加权投票很重要。通过对 bin 进行加权投票,当梯度幅度落在边界上或边界附近时,直方图的变化会大大减少。Chris McCormick 写了一篇关于 hog的非常有见地的博客,其中方向分箱被清楚地描述为
For each gradient vector, it's contribution to the histogram is given by the magnitude of the vector (so stronger gradients have a bigger impact on the histogram). We split the contribution between the two closest bins. So, for example, if a gradient vector has an angle of 85 degrees, then we add 1/4th of its magnitude to the bin centered at 70 degrees, and 3/4ths of its magnitude to the bin centered at 90.
I believe the intent of splitting the contribution is to minimize the problem of gradients which are right on the boundary between two bins. Otherwise, if a strong gradient was right on the edge of a bin, a slight change in the gradient angle (which nudges the gradient into the next bin) could have a strong impact on the histogram.
对于每个梯度向量,它对直方图的贡献由向量的大小给出(因此更强的梯度对直方图有更大的影响)。我们在两个最近的 bin 之间分配贡献。因此,例如,如果梯度向量的角度为 85 度,那么我们将其幅度的 1/4 添加到以 70 度为中心的 bin,并将其幅度的 3/4 添加到以 90 度为中心的 bin。
我相信拆分贡献的目的是最小化两个 bin 之间边界上的梯度问题。否则,如果强梯度正好位于 bin 的边缘,则梯度角度的微小变化(将梯度推入下一个 bin)可能会对直方图产生强烈影响。
So, use OpenCV to compute hog if possible (haven't digged into its code and don't feel like doing so, but I suppose OpenCV's way of hog implementation is more appropriate). Not only I found an improvement in detection accuracy, but it also runs faster. Compared to scikit-image's hog code with wonderful comments, its documentation is almost none. Yet it is still feasible that one could get OpenCV's version working in practice - it's a matter of passing the right parameter for window size, cell size, block size, block stride, number of orientations, etc. Other parameters I just went with default.
所以,如果可能的话,使用 OpenCV 来计算 hog(没有深入研究它的代码,也不想这样做,但我认为 OpenCV 的 hog 实现方式更合适)。我不仅发现检测精度有所提高,而且运行速度也更快。与 scikit-image 的带有精彩注释的 hog 代码相比,它的文档几乎没有。然而,让 OpenCV 的版本在实践中工作仍然是可行的——这是传递窗口大小、单元格大小、块大小、块步幅、方向数量等的正确参数的问题。其他参数我只是默认。