将图像的numpy数组转换为块

时间:2022-04-12 18:18:07

I have of a numpy array of a image.I want to convert this image into 8*8 block using python.How should I do this?

我有一个numpy数组的图像。我想用python将这个图像转换成8 * 8块。我该怎么做?

2 个解决方案

#1


1  

Please provide your array structure.

请提供您的阵列结构。

you can use img_arrary.reshape(8,8), to work total elements must be 64

你可以使用img_arrary.reshape(8,8),工作总元素必须是64

#2


0  

reshape and then swapaxes:

重塑然后交换:

import numpy as np

img = np.random.randint(0, 255, size=(128, 256, 3)).astype(np.uint8)

blocks = img.reshape(img.shape[0]//8, 8, img.shape[1]//8, 8, 3).swapaxes(1, 2)
print(blocks.shape)

to check the result:

检查结果:

np.allclose(blocks[0, 0], img[:8, :8, :])
np.allclose(blocks[3, 2], img[3*8:3*8+8, 2*8:2*8+8, :])

#1


1  

Please provide your array structure.

请提供您的阵列结构。

you can use img_arrary.reshape(8,8), to work total elements must be 64

你可以使用img_arrary.reshape(8,8),工作总元素必须是64

#2


0  

reshape and then swapaxes:

重塑然后交换:

import numpy as np

img = np.random.randint(0, 255, size=(128, 256, 3)).astype(np.uint8)

blocks = img.reshape(img.shape[0]//8, 8, img.shape[1]//8, 8, 3).swapaxes(1, 2)
print(blocks.shape)

to check the result:

检查结果:

np.allclose(blocks[0, 0], img[:8, :8, :])
np.allclose(blocks[3, 2], img[3*8:3*8+8, 2*8:2*8+8, :])