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

时间:2019-07-09 15:20:34

标签: python opencv

我正在尝试在数据集图像上使用HOGDescriptor,因此我可以训练一个pytorch神经网络。

这些是图像转换:

train_transforms = transforms.Compose([transforms.Resize([32,32]),
                                          transforms.Grayscale(),
                                       transforms.ToTensor(),])
train_data = datasets.ImageFolder(datadir,
                transform=train_transforms)

这是我的HOG代码:

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

3 个答案:

答案 0 :(得分:1)

您需要将图像转换为灰度。 如文档所示:link

img – Source image. CV_8UC1 and CV_8UC4 types are supported for now.

您可以使用来检查图像类型

# 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

答案 1 :(得分:0)

这意味着您的图像必须是一个通道,并键入CV_8U或CV_8UC3-1或3通道图像(带无符号char值)。检查图像中的类型。

答案 2 :(得分:0)

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

 Homepage: {
    screen: HomeNavigator,
    navigationOptions: {
      tabBarIcon: ({ focused }) => {
        if (focused) {
          return <Icon source={homeLogoOn} />
        } else {
          if (currentRoute === 'Me') {
            return <Icon source={homeLogoOffLeft} />
          } else if (currentRoute === 'MyFriends') {
            return <Icon source={homeLogoOffRight} />
          }
        }
      },
      tabBarLabel: <View />,
    },