Python: ValueError: не удалось передать входной массив из фигуры (293 300) в фигуру (700 360) - PullRequest
0 голосов
/ 10 октября 2018

Я пытаюсь преобразовать эту функцию C ++ в функцию python3, однако при попытке запустить ее с двумя изображениями (соответственно 48x49 и 45x49) у меня появляется ошибка

" np.copyto (DispImage, temp, where = ROI)

ValueError: не удалось преобразовать входной массив из фигуры (293 300) в фигуру (700 360)"

У меня изначально была ошибка из формы (300,164,3) в форму (700,360) , но затем я вырезал третье измерение из изображений для их подгонки.По крайней мере, я думал, что это подойдет, но по какой-то причине это не так: / Я уже пытался удалить параметр "where = Roi", ничего не изменилось.

Вот код:

import cv2
import numpy as np

def ShowManyImages(title, *args):
    nArgs = len(args)

    # If the number of arguments is lesser than 0 or greater than 12
    # return without displaying
    if(nArgs <= 0):
        print("Number of arguments too small....\n")
        return
    elif(nArgs > 12):
        print("Number of arguments too large, can only handle maximally 12 images at a time ...\n")
        return
    elif (nArgs == 1):
        w = 1
        h = 1
        size = 300
    elif (nArgs == 2):
        w = 2
        h = 1
        size = 300
    elif (nArgs == 3 or nArgs == 4):
        w = 2
        h = 2
        size = 300
    elif (nArgs == 5 or nArgs == 6):
        w = 3
        h = 2
        size = 200
    elif (nArgs == 7 or nArgs == 8):
        w = 4
        h = 2
        size = 200
    else:
        w = 4
        h = 3
        size = 150

    # Create a new 3 channel image
    DispImage = np.zeros((100 + size*w, 60 + size*h), dtype=np.uint8)

    # Loop for nArgs number of arguments
    i = 0
    m = 20
    n = 20
    while i < nArgs:
        # Get the Pointer to the IplImage
        img = args[i]

        print(img.shape)
        img = img[:, :, 0]

        # Check whether it is NULL or not
        # If it is NULL, release the image, and return
        if(img.size == 0):
            print("Invalid arguments")
            return

        # Find the width and height of the image
        print(img.shape)
        x, y = img.shape

        # Find whether height or width is greater in order to resize the image
        maximum = (x if (x > y) else y)

        # Find the scaling factor to resize the image
        scale = maximum / size

        # Used to Align the images
        if( i % w == 0 and m!= 20):
            m = 20
            n+= 20 + size

        # Set the image ROI to display the current image
        # Resize the input image and copy the it to the Single Big Image
        ROI = {"top":m, "left":n, "width":x/scale, "height":y/scale}

        temp = cv2.resize(img, (int(ROI["width"]), int(ROI["height"])))
        np.copyto(DispImage, temp, where=ROI)

        i += 1
        m += (20 + size)

    # Create a new window, and show the Single Big Image
    cv2.namedWindow( title, 1 )
    cv2.imshow( title, DispImage )
    cv2.waitKey()


if __name__ == '__main__':
    img1 = cv2.imread("img/fish1.png")
    img2 = cv2.imread("img/fish2.png")

    ShowManyImages("test", img1, img2)
...