소스 뷰어
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 이미지 읽기 (OpenCV는 기본적으로 BGR로 이미지를 불러옵니다)
img_bgr = cv2.imread('example.jpg')
# NumPy를 사용하여 BGR을 RGB로 변경
img_rgb = img_bgr[:,:,::-1] # 채널 순서를 역으로 뒤집음 (BGR -> RGB)
# RGB 채널 분리
red_channel = img_rgb[:, :, 0] # R 채널
green_channel = img_rgb[:, :, 1] # G 채널
blue_channel = img_rgb[:, :, 2] # B 채널
# 각 채널 이미지를 단일 채널이 아닌 컬러로 출력하기 위해 R, G, B 이미지를 만듦
# R 채널 이미지: R만 유지, G와 B는 0으로 설정
img_red = np.zeros_like( img_rgb )
img_red[:, :, 0] = red_channel
# G 채널 이미지: G만 유지, R과 B는 0으로 설정
img_green = np.zeros_like( img_rgb )
img_green[:, :, 1] = green_channel
# B 채널 이미지: B만 유지, R과 G는 0으로 설정
img_blue = np.zeros_like( img_rgb )
img_blue[:, :, 2] = blue_channel
# 원본 및 채널 이미지를 비교하여 출력
plt.figure(figsize=(12, 8))
# 원본 이미지 (RGB)
plt.subplot(2, 2, 1)
plt.imshow( img_rgb )
plt.title('Original RGB Image')
plt.axis('off')
# Red 채널 이미지
plt.subplot(2, 2, 2)
plt.imshow( img_red )
plt.title('Red Channel')
plt.axis('off')
# Green 채널 이미지
plt.subplot(2, 2, 3)
plt.imshow( img_green )
plt.title('Green Channel')
plt.axis('off')
# Blue 채널 이미지
plt.subplot(2, 2, 4)
plt.imshow( img_blue )
plt.title('Blue Channel')
plt.axis('off')
# 레이아웃 조정 및 출력
plt.tight_layout()
plt.show()