组合通道功能问题-python3



我有一个任务,我需要构建一个接收X个颜色通道的函数,并将其组合为一个,如下所示:功能接收:

[[[1]], [[2]]]) → [[[1, 2]]]

但从上个赛季开始,我的功能返回这个

[[[1], [2]]]
def combine_channels(channels):
channels_num = len(channels[0][0])
channel_row = []
new_channel = []
channel_combine = []
pixle_index = 0
while pixle_index < channels_num:
for i in image:
for j in i:
channel_row.append(j[pixle_index])
new_channel.append(channel_row)
channel_row = []
channel_combine.append(new_channel)
new_channel = []
pixle_index += 1
return channel_combine

NB:只有在有帮助的情况下,我才能导入sys和math。

我不知道我是否知道图像的样子,我会假设你的教授实际上会这样输入:

[
# channels
[
# rows
[1, 2, 3, ...],  # pixels
[4, 5, 6, ...],
...
],
# channels
[
# rows
[1, 2, 3, ...],
[4, 5, 6, ...],
...
],
]

代码:

>>> def combine_channels(image):
...     copy = []
...     for row_pairs in zip(*image):
...          copy.append([channel_combined for channel_combined in zip(*row_pairs)])
...     return copy
>>> combine_channels([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
[[(1, 1), (2, 2), (3, 3)], [(4, 4), (5, 5), (6, 6)]]
>>> combine_channels([[[1]], [[2]]])
[[(1, 2)]]

最新更新