Python >> Tutoriel Python >  >> Python

python:veut afficher le canal rouge uniquement dans opencv

Vous pouvez simplement faire une copie de l'image d'origine et définir certains canaux sur 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)

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)