【问题标题】:Assigning labels to HOG features for training an SVM classifier为 HOG 特征分配标签以训练 SVM 分类器
【发布时间】:2018-05-26 04:15:31
【问题描述】:
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

【问题讨论】:

    标签: machine-learning computer-vision svm


    【解决方案1】:

    参考此链接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 表示负样本来分配标签。然后将这些标签输入到 label_mat 中,然后用于 SVM 训练过程

    【讨论】:

      【解决方案2】:
      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)
      

      接下来,您可以在每个 hog 功能之后根据您的选择分配标签。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-17
        • 2018-10-25
        • 2018-11-06
        • 1970-01-01
        • 2016-12-20
        • 2016-10-09
        • 2021-08-18
        • 2013-04-20
        相关资源
        最近更新 更多