Python ValueError:无法将大小为1251936的数组重塑为形状(11181118)

Python ValueError:无法将大小为1251936的数组重塑为形状(11181118),python,cv2,Python,Cv2,这似乎是一个经常被问到的问题,但问题是我理解这个错误,但要得到尺寸为2的1251936,它需要(1118.899459290241188.89945929024),但这个数字必须是整数,然后是1118。这给了1249924,这是个问题 def create_features(img): img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) features, _ = train.create_features(img, img_gr

这似乎是一个经常被问到的问题,但问题是我理解这个错误,但要得到尺寸为2的1251936,它需要(1118.899459290241188.89945929024),但这个数字必须是整数,然后是1118。这给了1249924,这是个问题

def create_features(img):

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    features, _ = train.create_features(img, img_gray, label=None, train=False)

    return features

def compute_prediction(img, model):

    border = 5 # (haralick neighbourhood - 1) / 2

    img = cv2.copyMakeBorder(img, top=border, bottom=border, \
                                  left=border, right=border, \
                                  borderType = cv2.BORDER_CONSTANT, \
                                  value=[0, 0, 0])

    features = create_features(img)
    predictions = model.predict(features.reshape(-1, features.shape[1]))
    pred_size = int(math.sqrt(features.shape[0]))
    inference_img = predictions.reshape(pred_size, pred_size)

    return inference_img

def infer_images(image_dir, model_path, output_dir):

    filelist = glob(os.path.join(image_dir,'*.png'))

    print ('[INFO] Running inference on %s test images' %len(filelist))

    model = pkl.load(open( model_path, "rb" ) )

    for file in filelist:
        print ('[INFO] Processing images:', os.path.basename(file))
        inference_img = compute_prediction(cv2.imread(file, 1), model)
        cv2.imwrite(os.path.join(output_dir, os.path.basename(file)), inference_img)


您的错误已连接到以下行:

predicts=model.predict(features.reforme(-1,features.shape[1]))

这里您的数组
不是方形数组,大小为
1251936
。 这就是为什么当你这样做的时候

pred_size = int(math.sqrt(features.shape[0]))
inference_img = predictions.reshape(pred_size, pred_size)
你有错误。因此,在进行重塑之前,您应该检查形状。我可以猜测错误的来源是添加边框。因此,请检查边框大小。 您正在此处添加边框:

border = 5 # (haralick neighbourhood - 1) / 2
img = cv2.copyMakeBorder(img, top=border, bottom=border, \
                              left=border, right=border, \
                              borderType = cv2.BORDER_CONSTANT, \
                              value=[0, 0, 0])

您的错误已连接到以下行:

predicts=model.predict(features.reforme(-1,features.shape[1]))

这里您的数组
不是方形数组,大小为
1251936
。 这就是为什么当你这样做的时候

pred_size = int(math.sqrt(features.shape[0]))
inference_img = predictions.reshape(pred_size, pred_size)
你有错误。因此,在进行重塑之前,您应该检查形状。我可以猜测错误的来源是添加边框。因此,请检查边框大小。 您正在此处添加边框:

border = 5 # (haralick neighbourhood - 1) / 2
img = cv2.copyMakeBorder(img, top=border, bottom=border, \
                              left=border, right=border, \
                              borderType = cv2.BORDER_CONSTANT, \
                              value=[0, 0, 0])

哦,谢谢,我没有想到边界。那是布尔值吗?我应该在哪里添加False,或者使用PIL?我更新了答案。你最初的图像形状是什么?特征形状是什么?事实上,在删除这一部分时,我得到了这样一个问题:“无法将大小为1229416的数组重塑为形状(11081108)”,这与此中没有边框的问题相同case@AngeloSantarossa你最初的图像形状和变换后的图像形状是什么?哦,谢谢,我没有考虑边界。那是布尔值吗?我应该在哪里添加False,或者使用PIL?我更新了答案。你最初的图像形状是什么?特征形状是什么?事实上,在删除这一部分时,我得到了这样一个问题:“无法将大小为1229416的数组重塑为形状(11081108)”,这与此中没有边框的问题相同case@AngeloSantarossa您最初的图像形状是什么?转换后的图像形状是什么?