将图像转换为4x4非重叠块

时间:2016-10-05 11:19:10

标签: python image-processing

我想在python中将图像划分为4x4非重叠块,然后将该块转换为16个元素的数组。

1 个答案:

答案 0 :(得分:1)

我建议使用像scikit-image这样的质量库,特别是如果将来需要更多步骤的话。它基于numpy / scipy。

一种最小的例子(from the docs)如下。

代码:

from skimage import data
from skimage import color
from skimage.util import view_as_blocks

# get astronaut from skimage.data in grayscale
l = color.rgb2gray(data.astronaut())

# size of blocks
block_shape = (4, 4)

# see astronaut as a matrix of blocks (of shape block_shape)
view = view_as_blocks(l, block_shape)

# collapse the last two dimensions in one
flatten_view = view.reshape(view.shape[0], view.shape[1], -1)

print(flatten_view.shape)

输出

(128, 128, 16)  # 128x128 blocks a 16 elements each