Python HOGDescriptor在函数cv::HOGDescriptor::computeGradient中返回错误:img.type()==0 | | img.type()==((0)&;((1<;<;3)-1)和#x2B;((3)-1<;<;3))

Python HOGDescriptor在函数cv::HOGDescriptor::computeGradient中返回错误:img.type()==0 | | img.type()==((0)&;((1<;<;3)-1)和#x2B;((3)-1<;<;3)),python,opencv,Python,Opencv,我试图在我的数据集图像上使用HOGDescriptor,这样我就可以训练一个pytorch神经网络 这些是图像转换: train_transforms = transforms.Compose([transforms.Resize([32,32]), transforms.Grayscale(), transforms.ToTensor

我试图在我的数据集图像上使用HOGDescriptor,这样我就可以训练一个pytorch神经网络

这些是图像转换:

train_transforms = transforms.Compose([transforms.Resize([32,32]),
                                          transforms.Grayscale(),
                                       transforms.ToTensor(),])
train_data = datasets.ImageFolder(datadir,
                transform=train_transforms)
这是我的生猪代码:

nbins = 9  # broj binova
cell_size = (2, 2)  # broj piksela po celiji
block_size = (2, 2)  # broj celija po bloku


    for im,l in train_data:
        im = im.numpy()
        hog = cv2.HOGDescriptor(_winSize=(im.shape[1] // cell_size[1] * cell_size[1],
                                          im.shape[0] // cell_size[0] * cell_size[0]),
                                _blockSize=(block_size[1] * cell_size[1],
                                            block_size[0] * cell_size[0]),
                                _blockStride=(cell_size[1], cell_size[0]),
                                _cellSize=(cell_size[1], cell_size[0]),
                                _nbins=nbins)


        im=hog.compute(im)
我一直收到这个错误:

img.type() == 0 || img.type() == (((0) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function cv::HOGDescriptor::computeGradient

img.type()。
如文件所示:

img–源图像。目前支持CV_8UC1和CV_8UC4类型。

您可以使用检查图像类型

# reading image in grayscale
img = cv2.imread('path.jpg', 0)
print(img.dtype) # returns dtype('uint8')
img.shape # returns (h, w) if it returns (h, w, 3) then it is not grayscale

我在转换中添加了HOGDescriptor调用:

 class MyHOG(object):
        def __call__(self, input):
            target=hogF(input)
            return target

    def hogF(im):
        im = np.asarray(im, dtype="int32")        #This is used to convert
        im = np.array(im * 255, dtype=np.uint8)    #PIL image to array of correct type       
        nbins = 9  # broj binova
        cell_size = (4, 4)  # broj piksela po celiji
        block_size = (2, 2)  # broj celija po bloku

        h=cv2.HOGDescriptor(_winSize=(32 // cell_size[1] * cell_size[1],
                                      32 // cell_size[0] * cell_size[0]),
                            _blockSize=(block_size[1] * cell_size[1],
                                        block_size[0] * cell_size[0]),
                            _blockStride=(cell_size[1], cell_size[0]),
                            _cellSize=(cell_size[1], cell_size[0]),
                            _nbins=nbins)
        return h.compute(im)

   train_transforms = transforms.Compose([transforms.Resize([32,32]), 
                                          transforms.Grayscale(),
                                           MyHOG(),
                                           transforms.ToTensor(),])

如果你对这些数字感到好奇,我也做了类似的分类:)