【问题标题】:Convert image into 4x4 non-overlapping blocks将图像转换为 4x4 非重叠块
【发布时间】:2016-10-05 11:19:09
【问题描述】:

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

【问题讨论】:

  • 太棒了!你试过什么?你期待什么?反而发生了什么?如果没有minimal reproducible example,我们将无能为力。
  • 我使用 Image.open() 读取图像,然后使用 numpy.asarray 将其转换为 ndarray,然后我尝试拆分数组,但我认为它无法正常工作。
  • 你为什么不显示你的代码?另外:使用一些外部特殊库可能是个好主意(不一定是因为你的问题很难;但它会被优化、健壮,也许还有其他你想做的事情)

标签: python image-processing


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多