为 HOG 特征分配标签以训练 SVM 分类器

Assigning labels to HOG features for training an SVM classifier

import cv2
image = cv2.imread("cat.1.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)
winStride = (8,8)
padding = (8,8)
locations = ((10,20),)
hist = hog.compute(image,winStride,padding,locations)
print (hist)
print (len(hist))
print (len(hist[0]))

我有近 50000 张狗 (25K) 和猫 (25K) 的图像。我想训练一个 SVM 分类器,以便它使用 HOG 描述符正确预测特定图像是狗还是 cat.By我正在获取特定图像的大小为 1764*1 的特征向量。

如何使用所有图像的特征向量?以及如何提供标签(例如 1 表示猫或 -1 表示狗)。注意图像文件名的格式为 at。1.jpg,cat.2.jpg..............cat.25000.jpg

参考这个linkhttps://docs.opencv.org/3.0-beta/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html

SVM 的语法略有不同,喜欢训练它变成 svm->train(training_mat, ROW_SAMPLE, labels_mat)

正确分配的标签使用 1 表示正样本,-1 表示负样本。然后将这些标签输入 labels_mat,然后用于 SVM 训练过程

import cv2
import numpy as np
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),)
out = []
for x in range(0,12500):
    image = cv2.imread("cat or dog.{}.jpg".format(x),0)
    hist = hog.compute(image,winStride,padding,locations))
    out.append(hist)
np.savetxt("hog.txt",out)

接下来,在每个猪特征之后,您可以根据自己的选择分配标签。