【问题标题】:HOG training and detection in Python using OpenCV使用 OpenCV 在 Python 中进行 HOG 训练和检测
【发布时间】:2017-08-13 17:27:35
【问题描述】:

我在使用 Python、OpenCV 3.1 和 HOG 进行有用检测时遇到问题。虽然我的工作代码可以正常执行,但经过训练的 HOG/SVM 组合无法在测试图像上检测到。

根据 OpenCV 示例和其他 Stack Overflow 讨论,我开发了以下方法。

win_size = (64, 64)
block_size = (16, 16)
block_stride = (8, 8)
cell_size = (8, 8)
nbins = 9
deriv_aperture = 1
win_sigma = 4.
histogram_norm_type = 0
l2_hys_threshold = 2.0000000000000001e-01
gamma_correction = 0
nlevels = 64

hog = cv2.HOGDescriptor(win_size,
                        block_size,
                        block_stride,
                        cell_size,
                        nbins,
                        deriv_aperture,
                        win_sigma,
                        histogram_norm_type,
                        l2_hys_threshold,
                        gamma_correction,
                        nlevels)

window_stride = (8, 8)
padding = (8, 8)
locations = ((0, 0),)

histograms = []
# not showing the loop here but
# create histograms for 600 positive and 600 negative images
# all images are of size 64x64
histograms.append(np.transpose(hog.compute(roi, window_stride, padding, locations)))

training_data = np.concatenate(histograms)
classifications = np.array([1] * 600 + [0] * 600)

svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)
svm.setTermCriteria((cv2.TermCriteria_MAX_ITER, 100, 1e-6))

svm.train(training_data, cv2.ml.ROW_SAMPLE, classifications)

# testing
test_img = cv2.imread('test_image.jpg')
svmvec = svm.getSupportVectors()[0]
rho = -svm.getDecisionFunction(0)[0]
svmvec = np.append(svmvec, rho)
hog.setSVMDetector(svmvec)
found, w = hog.detectMultiScale(test_img)

在每个测试中,found 是一个以图像为中心的单个矩形,并且不在测试图像中阳性所在的位置。

我根据 Stack Overflow 答案和其他 OpenCV 示例和讨论尝试了许多不同的参数组合。他们都没有改变结果。

【问题讨论】:

  • 对问题的详细和组织良好的解释应该得到奖励。请问您为什么只使用svm.getSupportVectors()[0]

标签: python opencv


【解决方案1】:

在中心创建单个矩形的原因是检测器将几乎所有区域都分类为“人类”。 默认情况下,detectMultiScale 抑制矩形的重叠。所以你只能看到中心的单个矩形。 您可以使用 detectMultiScale 的 finalThreshold 选项关闭此抑制。

hogParams = { 'finalThreshold': 0}
found, w = hog.detectMultiScale(test_img, **hogParams)

默认情况下,此参数设置为 2。 您可以看到几乎所有区域都被矩形颜色填充。

我对这种“错误分类”的回答是简单地改变标签的顺序。

classifications = np.array([0] * 600 + [1] * 600)

【讨论】:

    【解决方案2】:

    我认为你需要你拥有的所有支持向量。所以问题不是你的训练代码,而是你的测试。

    svm.train(training_data, cv2.ml.ROW_SAMPLE, classifications)
    

    您使用所有数据进行训练,但在测试时,您只使用生成的分类器的一小部分。

    svmvec = svm.getSupportVectors()[0]
    

    改变这一行,你会少一个问题。

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 2016-05-01
      • 2016-05-03
      • 2017-06-27
      • 2015-09-15
      • 2017-12-23
      • 1970-01-01
      • 2018-06-30
      • 2016-12-21
      相关资源
      最近更新 更多