首页 > 解决方案 > 将 Mat 对象转换为标准 12x12 矩阵

问题描述

我将每个字符作为不同大小的 Mat 对象。一些示例图像是,

示例图片 1 示例图像 2

我正在尝试使用 PIL 将它们转换为图像,然后转换为标准的 12x12 矩阵,该矩阵将被展平为 144 列一维数组。经过建议,我使用的代码如下

#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

链接到带有和不带有 reshape 的输出文件

我正在使用的完整代码和源图像的链接

标签: pythonnumpyopencvpython-imaging-library

解决方案


您对ndarray.resize图像大小调整功能感到困惑。这不是它的工作原理。Numpy 不知道您的数组是图像,并且只会调整数组的大小而不关心内容。

您应该改用 OpenCVresize函数。

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

此外,在从 PIL 数据创建数组后,您需要将images数组重塑为图像尺寸。看看这个问题,看看它是如何完成的。


推荐阅读