Python 阵列形状不正确

Python 阵列形状不正确,python,Python,我只是想快速问一下,我将如何将打印形状为(50,25,3)的列表重塑为(50,25)的形状。老实说,我有点搞不清3号到底是从哪里来的,我得参考一下尺寸以便检查。因此,即使我必须更改实现,我也需要它们匹配 一份清单: def templates(): templatesArray = [[],[],[],[],[]] counter = 0 for dirs in os.listdir(args["templates"]): pathToFiles =

我只是想快速问一下,我将如何将打印形状为(50,25,3)的列表重塑为(50,25)的形状。老实说,我有点搞不清3号到底是从哪里来的,我得参考一下尺寸以便检查。因此,即使我必须更改实现,我也需要它们匹配

一份清单:

def templates():
    templatesArray = [[],[],[],[],[]]
    counter = 0



    for dirs in os.listdir(args["templates"]):
        pathToFiles = args["templates"] + "/" + dirs + "/*.jpg"

        for imagePaths in glob.glob(pathToFiles):
            #print(imagePaths)
            tempCard = cv2.imread(imagePaths)
            templatesArray[counter].append(tempCard)
        counter += 1
    cv2.waitKey(0)
    return templatesArray
其他清单

def pointsOfIntrest(img):
    intrestArray = [[], [], [], [], []]

    yStart = 335   ####Position of FlopOne Cards Upper Left Corner
    xStart = 438
    flopOne = img[yStart:yStart + 50, xStart:xStart + 25]
    flopOne = cv2.cvtColor(flopOne, cv2.COLOR_BGR2GRAY)
    flopOne = auto_canny(flopOne)
    intrestArray[FLOP_ONE].append(flopOne)



    xStart = 530
    flopTwo = img[yStart:yStart + 50, xStart:xStart + 25]
    flopTwo = cv2.cvtColor(flopTwo, cv2.COLOR_BGR2GRAY)
    flopTwo = auto_canny(flopTwo)
    intrestArray[FLOP_TWO].append(flopTwo)



    xStart = 621
    flopThree = img[yStart:yStart + 50, xStart:xStart + 25]
    flopThree = cv2.cvtColor(flopThree, cv2.COLOR_BGR2GRAY)
    flopThree = auto_canny(flopThree)
    intrestArray[FLOP_THREE].append(flopThree)


    xStart = 711
    turn = img[yStart:yStart + 50, xStart:xStart + 25]
    turn = cv2.cvtColor(turn, cv2.COLOR_BGR2GRAY)
    turn = auto_canny(turn)
    intrestArray[TURN].append(turn)


    xStart = 801
    river = img[yStart:yStart + 50, xStart:xStart + 25]
    river = cv2.cvtColor(river, cv2.COLOR_BGR2GRAY)
    river = auto_canny(river)
    intrestArray[RIVER].append(river)
我知道这本可以实施得更好。请询问我的代码是否没有意义。 下面是我打印形状时的输出

C:\Users\Turtle\PycharmProjects\MakeGood>python main.py--templates-CardRawData--images

(‘50’、‘25’)

(‘50’、‘25’、‘3’)

有点粗糙和长,但这就是我打印形状的方式

print(str(pointsToCompare[FLOP_ONE][0].shape[0]),str(pointsToCompare[FLOP_TWO][0].shape[1]))
print(str(boardCards[FLOP_ONE][element].shape[0]), str(boardCards[FLOP_ONE][element].shape[1]),str(boardCards[FLOP_ONE][element].shape[2]))
埃弗特建议将印刷方法改为更好的印刷方法

print(str(pointsToCompare[FLOP_ONE][0].shape))
print(str(boardCards[FLOP_ONE][element].shape))

埃弗特正确地指出了这个问题的答案。 数组正在读取第三维的RGB值,因为我没有在imread上正确设置标志

之前:

tempCard = cv2.imread(imagePaths)
之后:

tempCard = cv2.imread(imagePaths, -1)
我不相信这是埃弗特耐心地为我解决的。非常感谢

与此相关的文档链接是,我的问题在参考标志的imread参数部分中提到。希望这有帮助


再次感谢。

很抱歉,我会编辑,因为这是OpenCV,我想知道这些是否只是RGB值,这将充分解释额外维度的大小。前两个值是高度和宽度…3个我不知道,甚至不确定其设置位置,因此我可以扩展其他列表:“
cv2.imread
:标志:>0返回一个3通道彩色图像。”(默认标志=1)。您仍然可以接受自己的答案作为已解决的问题来完成。我还建议添加相关文档的链接,以便人们可以查找该标志的完整含义(更好的方法是:链接文档,并引用文档中的相关部分)。