Python: want to display red channel only in opencv

I am starting to do image processing. I am displaying an image in many color spaces, the code below shows an image in three RGB channels, however the image is displayed in a gray layout. I need to display three images with a red channel as a red image, another as blue and the last as green. thanks in advance.

# cspace.py
import cv2
import numpy as np

image = cv2.imread('download.jpg')

# Convert BGR to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsl = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) # equal to HSL
luv = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)


#RGB - Blue
cv2.imshow('B-RGB.jpg',image[:, :, 0])
cv2.imwrite('B-RGB.jpg',image[:, :, 0])

# RGB - Green
cv2.imshow('G-RGB',image[:, :, 1])
cv2.imwrite('G-RGB.jpg',image[:, :, 1])

# RGB Red
cv2.imshow('R-RGB',image[:, :, 2])
cv2.imwrite('R-RGB.jpg',image[:, :, 2])

      

cv2.waitKey (0) Blue image currently displayed I need to display a blue channel like this image

+3


source to share


1 answer


You can just make a copy of the original image and set some channels to 0.



import cv2

image = cv2.imread('download.jpg')

b = image.copy()
# set green and red channels to 0
b[:, :, 1] = 0
b[:, :, 2] = 0


g = image.copy()
# set blue and red channels to 0
g[:, :, 0] = 0
g[:, :, 2] = 0

r = image.copy()
# set blue and green channels to 0
r[:, :, 0] = 0
r[:, :, 1] = 0


# RGB - Blue
cv2.imshow('B-RGB', b)

# RGB - Green
cv2.imshow('G-RGB', g)

# RGB - Red
cv2.imshow('R-RGB', r)

cv2.waitKey(0)

      

+4


source







All Articles