我的图像编码如下:image[RGBchannel][y][x]
我想为每个索引访问RGBchannel
三元组。这是我目前正在使用的方法。是否有更有效的方式来编写它?
image = [[[1, 2, 3], [4,5,6]], [[7,8,9], [10, 11, 12]], [[13,14,15], [16,17,18]]]
for y in range(len(image[0]):
for x in range(len(image[0][0]):
rgb = []
for channel in range(len(image)):
rgb.append(image[channel][y][x])
print rgb
输出是:
[1, 7, 13]
[2, 8, 14]
[3, 9, 15]
[4, 10, 16]
[5, 11, 17]
[6, 12, 18]
不导入任何第三方库或导入任何内部模块,有更好的方法吗?
答案 0 :(得分:4)
这个怎么样:
template<typename T>
class MyClass
{
public:
void f(T const& x){}
std::enable_if_t<!std::is_reference_v<T>>> f(T&& x){}
};
输出:
image = [[[1, 2, 3], [4,5,6]], [[7,8,9], [10, 11, 12]], [[13,14,15], [16,17,18]]]
result = [x for z in zip(*image) for x in zip(*z)]
print(result)
答案 1 :(得分:2)
这是两次拉链的类似方法。
image = [
[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]],
[[13, 14, 15], [16, 17, 18]],
]
image_TT = [[*x] for z in zip(*map(zip, *image)) for x in z]
# [[1, 7, 13], [4, 10, 16], [2, 8, 14], [5, 11, 17], [3, 9, 15], [6, 12, 18]]
in_order = image_TT[::2] + image_TT[1::2]
# [[1, 7, 13], [2, 8, 14], [3, 9, 15], [4, 10, 16], [5, 11, 17], [6, 12, 18]]