使用python从图像集中提取hog功能时出错

我试图从图像集中提取HOG功能但我得到内存错误

hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]

的MemoryError

我从opencv example复制了HOG功能我的示例代码是

def hog(img):
  gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
  gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
  mag, ang = cv2.cartToPolar(gx, gy)
  bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
  bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
  mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
  hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
  hist = np.hstack(hists)     # hist is a 64 bit vector
  return hist
path_url="d:/anto/preimages/"
listdir = os.listdir(path_url)
for file in listdir:
  img = cv2.imread(path_url + file)
  h=hog(img)

最佳答案 问题出在你的图像集上.因为你已经从google中随机下载了图像.它可能有不同的大小,导致Error.Before调用hog函数你必须调整图像大小.在opencv中你可以调整大小

resized=cv2.resize(img,(250,250))
h=hog(resized)

在PIL库中,您可以使用调整大小

resolutin=(250.250)
resizes=img.resize(resolution , Image.ANTIALIAS)

但我必须建议添加一个单独的preproceesing步骤.在预处理步骤中,您可以调整大小并将“preimages”文件夹中的所有图像保存到另一个文件夹,然后将其作为输入提供给您的生猪提取程序.

点赞