我有一个512 x 512条目矩阵形式的图像。像
这样的东西[[12,234, . . ... . (512 entries)],
[[12,234, . . ... . (512 entries)],
[[12,234, . . ... . (512 entries)],
.
.
[12,234, . . ... . (512 entries)]]
我想将图像分成4x4块并将它们放入一个列表中。我该怎么做呢?将有128个大小为4 x 4的块,从左开始索引。
答案 0 :(得分:2)
这是我用于此目的的一个功能:
def blockshaped(arr, nrows, ncols):
h, w = arr.shape
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
其中: arr =输入的2D np数组
windowsize =整数,表示图块的大小
输出是一个形状数组(n,nrows,ncols),其中n * nrows * ncols = arr.size。
答案 1 :(得分:1)
OpenCV
使用numpy
和numpy
可让您使用image[0:4, 0:4]
之类的索引来获得正方形:
所以你需要类似的东西:
width, height = image.shape
# for image with ie. `RGB` color (3 bytes in every pixel)
#width, height, depth = image.shape
blocks = []
for y in range(0, height, 4):
for x in range(0, width, 4):
blocks.append(image[y:y+4, x:x+4])