python:只想在opencv中显示红色通道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44554125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python: want to display red channel only in opencv
提问by Abeer Yosef
I am beginner in image processing. I am showing image in many color space the below code show the image in the 3 channels R G B however the image displayed in the gray layout. i need to display three images one with red channel as red image, another as blue, and the last one as green. thanks in advance.
我是图像处理的初学者。我在许多颜色空间中显示图像,下面的代码以 3 通道 RGB 显示图像,但图像以灰色布局显示。我需要显示三个图像,其中一个红色通道为红色图像,另一个为蓝色,最后一个为绿色。提前致谢。
# 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 as displayed currently
回答by Pedro Boechat
You can just make a copy of the original image and set some channels to 0.
您可以复制原始图像并将某些通道设置为 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)
回答by John Forbes
import cv2
import numpy as np
channel_initials = list('BGR')
image = cv2.imread('download.jpg')
for channel_index in range(3):
channel = np.zeros(shape=image.shape, dtype=np.uint8)
channel[:,:,channel_index] = image[:,:,channel_index]
cv2.imshow(f'{channel_initials[channel_index]}-RGB', channel)
cv2.waitKey(0)