首页 > 解决方案 > 在 Python 中对原始 RGGB 图像数组进行自定义切片

问题描述

我有一个存储为 RGGB 的原始图像数据的二维数组。从每组 4 (2x2) 个 RGGB 簇(16 个传感器像素)中,我需要拉出 1 个 RGGB 簇以形成一个新图像,总共 4 个图像,每个图像的分辨率是原始图像的 1/4。

例如给定这个二维数组:

a = np.arange(1,65).reshape(8,8)

[[ 1  2  3  4  5  6  7  8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]
[33 34 35 36 37 38 39 40]
[41 42 43 44 45 46 47 48]
[49 50 51 52 53 54 55 56]
[57 58 59 60 61 62 63 64]]

我需要提取这 4 个数组:

[[ 1  2  5  6]
[ 9 10 13 14]
[33 34 37 38]
[41 42 45 46]]

[[ 3  4  7  8]
[11 12 15 16]
[35 36 39 40]
[43 44 47 48]]

[[17 18 21 22]
[25 26 29 30]
[49 50 53 54]
[57 58 61 62]]

[[19 20 23 24]
[27 28 31 32]
[51 52 55 56]
[59 60 63 64]]

我认为必须有一些聪明、有效的方法来做到这一点,但我还没有想出任何使用我所知道的内置切片器的方法。

我开始沿着将主数组分解成 2 个元素组的 2 个数组的路径,以为我可以以某种方式将它们重新组合在一起,但我被卡住了。

sliced = a.reshape(-1,2)[::2]
sliced2 = a.reshape(-1,2)[1::2]

标签: pythonarraysnumpymultidimensional-arrayslice

解决方案


你往好的方向走

sliced1 = a.reshape(-1,2)[::2]
sliced2 = a.reshape(-1,2)[1::2]

现在再次重塑

sliced1 = sliced1.reshape(-1,8)
sliced2 = sliced2.reshape(-1,8)

再切片

sliced1a = sliced1[::2]
sliced1b = sliced1[1::2]

sliced2a = sliced2[::2]
sliced2b = sliced2[1::2]

最后去塑造4x4

sliced1a = sliced1a.reshape(4,4)
sliced1b = sliced1b.reshape(4,4)

sliced2a = sliced2a.reshape(4,4)
sliced2b = sliced2b.reshape(4,4)

完整的工作代码

import numpy as np

a = np.arange(1,65).reshape(8,8)

print('\n- reshaped -\n')

reshaped = a.reshape(-1,2)

print(reshaped)
print()

print('\n- sliced1, sliced2 -\n')

sliced1 = reshaped[::2]
sliced2 = reshaped[1::2]

print(sliced1)
print()
print(sliced2)
print()

print('\n- reshaped1, reshaped2 -\n')

reshaped1 = sliced1.reshape(-1,8)
reshaped2 = sliced2.reshape(-1,8)

print(reshaped1)
print()
print(reshaped2)
print()

print('\n- sliced1a, slices1b, sliced2a, sliced2b -\n')
 
sliced1a = reshaped1[::2]
sliced1b = reshaped1[1::2]

print(sliced1a)
print()
print(sliced1b)
print()

sliced2a = reshaped2[::2]
sliced2b = reshaped2[1::2]

print(sliced2a)
print()
print(sliced2b)
print()

print('\n- 4x4 -\n')

#and go to shape `4x4`

result1a = sliced1a.reshape(4,4)
result1b = sliced1b.reshape(4,4)

result2a = sliced2a.reshape(4,4)
result2b = sliced2b.reshape(4,4)

print(result1a)
print()
print(result1b)
print()

print(result2a)
print()
print(result2b)

结果:

- reshaped -

[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]
 [13 14]
 [15 16]
 [17 18]
 [19 20]
 [21 22]
 [23 24]
 [25 26]
 [27 28]
 [29 30]
 [31 32]
 [33 34]
 [35 36]
 [37 38]
 [39 40]
 [41 42]
 [43 44]
 [45 46]
 [47 48]
 [49 50]
 [51 52]
 [53 54]
 [55 56]
 [57 58]
 [59 60]
 [61 62]
 [63 64]]


- sliced1, sliced2 -

[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]
 [17 18]
 [21 22]
 [25 26]
 [29 30]
 [33 34]
 [37 38]
 [41 42]
 [45 46]
 [49 50]
 [53 54]
 [57 58]
 [61 62]]

[[ 3  4]
 [ 7  8]
 [11 12]
 [15 16]
 [19 20]
 [23 24]
 [27 28]
 [31 32]
 [35 36]
 [39 40]
 [43 44]
 [47 48]
 [51 52]
 [55 56]
 [59 60]
 [63 64]]


- reshaped1, reshaped2 -

[[ 1  2  5  6  9 10 13 14]
 [17 18 21 22 25 26 29 30]
 [33 34 37 38 41 42 45 46]
 [49 50 53 54 57 58 61 62]]

[[ 3  4  7  8 11 12 15 16]
 [19 20 23 24 27 28 31 32]
 [35 36 39 40 43 44 47 48]
 [51 52 55 56 59 60 63 64]]


- sliced1a, slices1b, sliced2a, sliced2b -

[[ 1  2  5  6  9 10 13 14]
 [33 34 37 38 41 42 45 46]]

[[17 18 21 22 25 26 29 30]
 [49 50 53 54 57 58 61 62]]

[[ 3  4  7  8 11 12 15 16]
 [35 36 39 40 43 44 47 48]]

[[19 20 23 24 27 28 31 32]
 [51 52 55 56 59 60 63 64]]


- 4x4 -

[[ 1  2  5  6]
 [ 9 10 13 14]
 [33 34 37 38]
 [41 42 45 46]]

[[17 18 21 22]
 [25 26 29 30]
 [49 50 53 54]
 [57 58 61 62]]

[[ 3  4  7  8]
 [11 12 15 16]
 [35 36 39 40]
 [43 44 47 48]]

[[19 20 23 24]
 [27 28 31 32]
 [51 52 55 56]
 [59 60 63 64]]

推荐阅读