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描述符,我得到了特定图像的大小为1764 * 1的特征向量。
如何使用所有图像的特征向量?还可以如何提供标签(例如1表示猫或-1表示狗)。注意图像文件名格式为at.1.jpg,cat。 2.JPG ............. cat.25000.jpg
答案 0 :(得分:1)
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)
在每次生猪功能之后,您可以根据自己的选择指定标签。
答案 1 :(得分:0)
请参阅此链接https://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训练程序