简体   繁体   中英

How to split colour channels in openCV without returning a gray scale image? I have tried the following it returns a gray scale image

How to split colour channels in openCV without returning a gray scale image? I have tried the following it returns a gray scale image?

import cv2
import numpy as np

img = cv2.imread("1.jpeg")
(channel_b, channel_g, channel_r) = (img[:,:,0], img[:,:,1], img[:,:,2])

cv2.imshow('red',channel_b)
cv2.waitKey(0)
cv2.destroyAllWindows()

The thing is, that the seperate channels do not really have a color assigned to them.

If you use imshow to display an image of dimensions (m, n, 3) the method assumes that the 3 channels are representing R, G and B. However if it gets an image of dimensions (m, n, 1) or (m, n) it assumes that there is no color in the image and it is displayed as gray scale.

In conclusion this means that there is nothing wrong with your seperation of the channels, imshow just doesn't know, that the values in channel_r are the red part of an image.

If you really want to display only the red part, you can do the following:

red = np.zeros(img.shape)
red[:,:,2] = img[:,:,2]

cv2.imshow('red', red)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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