소스 뷰어
import cv2, numpy as np, matplotlib.pyplot as plt
# 1. 이미지 읽기
image_bgr = cv2.imread('example.jpg')
# BGR 채널을 RGB 채널로 변환
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
# 2. HSV로 변환
image_hsv = cv2.cvtColor( image_rgb, cv2.COLOR_BGR2HSV)
# 3. HSV 채널 분리
h_channel, s_channel, v_channel = cv2.split(image_hsv)
# 4. 레이아웃 정의
rows, cols = 2, 2 # 행 열
chart_idx = 0
# 5. 시각화
plt.figure(figsize=(4*cols, 3*rows))
# 원본 이미지
plt.subplot(rows, cols, chart_idx := chart_idx + 1 )
plt.imshow(image_rgb)
plt.title("Original Image (RGB)")
plt.axis('off')
# H 채널
plt.subplot(rows, cols, chart_idx := chart_idx + 1)
plt.imshow(h_channel, cmap='hsv' )
plt.title("Hue Channel (H)")
plt.axis('off')
# S 채널
plt.subplot(rows, cols, chart_idx := chart_idx + 1)
plt.imshow(s_channel, cmap='gray' )
plt.title("Saturation Channel (S)")
plt.axis('off')
# V 채널
plt.subplot(rows, cols, chart_idx := chart_idx + 1)
plt.imshow(v_channel, cmap='gray' )
plt.title("Value Channel (V)")
plt.axis('off')
plt.tight_layout()
plt.show()