将Mat对象转换为标准12x12矩阵

问题描述 投票:0回答:1

我将每个角色都作为Mat对象,它们的大小不同。一些样本图像是,

Sample Image 1 Sample Image 2

我正在尝试使用PIL将它们转换为图像,然后转换为标准的12x12矩阵,将其展平为144列1D阵列。建议之后,我使用的代码如下所示

#roi is a Mat Object
images = cv2.resize(roi,(12,12))
myImage = Image.fromarray(images)
withoutReshape = np.array(myImage.getdata()) #Corresponding output attached
print(withoutReshape)
withReshape = np.array(myImage.getdata()).reshape(myImage.size[0], myImage.size[1], 3) 
print(withReshape) #Corresponding output attached

无法找到使用reshape的重要性。另外,在使用resize之后,如何将矩阵展平为数组

Link to output files with and without reshape

Link for complete code and source image that I am using

python numpy opencv python-imaging-library
1个回答
1
投票

您将ndarray.resize与图像大小调整功能混淆。这不是它的工作原理。 Numpy不知道你的阵列是一个图像,只会调整阵列大小而不关心内容。

你应该使用OpenCV resize函数。

images = cv2.resize(images, (12, 12))

此外,您需要在从PIL数据创建后将images数组重新整形为图像尺寸。看看this question,看看它是如何完成的。

© www.soinside.com 2019 - 2024. All rights reserved.