我正在训练HOG + SVM模型,我的训练数据有各种大小和宽高比。 SVM模型无法在可变大小的列表上进行训练,因此我希望计算不同图像大小的相同长度的梯度直方图。
有一种聪明的方法吗?或者更好地调整图像大小或填充它们?
答案 0 :(得分:1)
You can normalize the images to a given target shape using cv2.resize(), divide image into number of blocks you want and calculate the histogram of orientations along with the magnitudes. Below is a simple implementation of the same.
img = cv2.imread(filename,0)
img = cv2.resize(img,(16,16)) #resize the image
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0) #horizontal gradinets
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1) # vertical gradients
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16 # Number of bins
# quantizing binvalues in (0-16)
bins = np.int32(bin_n*ang/(2*np.pi))
# divide to 4 sub-squares
s = 8 #block size
bin_cells = bins[:s,:s],bins[s:,:s],bins[:s,s:],bins[s:,s:]
mag_cells = mag[:s,:s], mag[s:,:s], mag[:s,s:], mag[s:,s:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells,mag_cells)]
hist = np.hstack(hists) #histogram feature data to be fed to SVM model
Hope that helps!
答案 1 :(得分:1)
在这种情况下,人们通常做的是以下两件事之一:
第一种方法1.
非常简单,但它有一些方法2.
试图解决的问题。
首先,考虑一下hog描述符的作用。它将图像划分为固定长度的单元格,逐个单元地计算梯度以生成逐个单元格的直方图(基于投票)。最后,您将获得所有单元格的连续直方图,这是您的描述符。
因此存在问题,因为对象(您要检测的对象)必须以类似的方式覆盖图像。否则,根据图像内对象的位置,您的描述符看起来会有所不同。
方法2.
的工作原理如下:
k
质心。使用频率直方图来训练SVM并将其用于分类阶段。这样,位置无关紧要,您将始终拥有固定大小的输入。您还将受益于尺寸的减少。