如何将 3 通道转换为 RGB 图像?

How to convert 3 channels as a RGB image?

我将 RGB 图像拆分为 R、G、B 通道。在处理完这些通道后,我需要将它们连接起来。我搜索了这个但找到了任何东西,所以我用 for 循环来做。但是效果不是很好

B,G,R = cv2.split(image)

#some process is here

#result after concatenate
res = np.zeros((image.shape))

for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        res[i,j,0]= B1[i,j]

for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        res[i,j,1]= G1[i,j]

for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        res[i,j,2]= R1[i,j]

但它 returns 是二进制图像。

不要写循环,改用merge()

简单如:

bgr = cv2.merge([B,G,R])

如果你使用

res = np.zeros_like(image)

您一定会得到相同的 dtype 并且您的代码可能会正常工作。

此外,尽量避免使用 for 循环,它们速度慢且容易出错。按照@berak 的建议使用 cv2.merge(),或者使用 Numpy:

bgr = np.dstack((B,G,R))