python3 if显示红色_python:只想在opencv中显示红色通道

这篇博客介绍了如何使用OpenCV库在Python中将RGB图像的三个颜色通道分开,分别显示为红色、绿色和蓝色图像。通过创建原始图像的副本并设置非所需颜色通道的像素值为0,实现了这一目标。示例代码展示了如何创建单独显示红色、绿色和蓝色通道的图像,并使用imshow函数进行展示。
摘要由CSDN通过智能技术生成

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.

# 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])

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值