Home > OS >  How to convert 3 channels as a RGB image?
How to convert 3 channels as a RGB image?

Time:10-17

I split a RGB image into R,G,B channels. After processing on these channels, I need to concatenated them. I searched this but find any thing, so I do it with for loops. 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.

CodePudding user response:

dont write loops, use merge() instead.

as simple as:

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

CodePudding user response:

If you use

res = np.zeros_like(image)

you will be assured of getting the same dtype and your code will likely work.

Also, try to avoid using for loops, they are slow and error-prone. Either use cv2.merge() as suggested by @berak, or use Numpy:

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