Python:ValueError:无法将输入数组从形状(293,300)广播到形状(700,360)

时间:2018-10-10 20:52:10

标签: python-3.x cv2

我正在尝试将this C++ function转换为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)

0 个答案:

没有答案