python:想只在opencv中显示红色通道

问题描述 投票:9回答:1

我是图像处理的初学者。我在许多色彩空间中显示图像,下面的代码显示3个通道中的图像R G B但是图像以灰色布局显示。我需要显示三个图像,一个用红色通道作为红色图像,另一个用作蓝色,最后一个用作绿色。提前致谢。

# 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

i need to display blue channel like this image

python opencv image-processing computer-vision
1个回答
13
投票

您只需复制原始图像并将某些通道设置为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)
© www.soinside.com 2019 - 2024. All rights reserved.