简体   繁体   English

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

[英]How to convert 3 channels as a RGB image?

I split a RGB image into R,G,B channels.我将 RGB 图像拆分为 R、G、B 通道。 After processing on these channels, I need to concatenated them.在这些通道上处理后,我需要将它们连接起来。 I searched this but find any thing, so I do it with for loops.我搜索了这个但找到了任何东西,所以我用 for 循环来做。 But it doesn't work well.但效果不佳。

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]

but it returns a binary image instead.但它返回一个二进制图像。

dont write loops, use merge() instead.不要写循环,改用merge()

as simple as:就像这样简单:

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

If you use如果你使用

res = np.zeros_like(image)

you will be assured of getting the same dtype and your code will likely work.您将确保获得相同的dtype并且您的代码可能会起作用。

Also, try to avoid using for loops, they are slow and error-prone.此外,尽量避免使用for循环,它们速度慢且容易出错。 Either use cv2.merge() as suggested by @berak, or use Numpy:要么cv2.merge()建议使用cv2.merge() ,要么使用 Numpy:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM