소스 뷰어
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 'example.jpg' 이미지 읽기
img = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)
# 제목과 이미지를 튜플로 묶어서 리스트에 저장 (타이틀 먼저, 이미지 나중)
titles_and_images = [
('Original Image', img),
('Over clipping: cv2.add(img, 100)', cv2.add(img, 100)),
('Under clipping: cv2.subtract(img, 100)', cv2.subtract(img, 100) ),
('Original Image', img),
('Overflow: img + 100', img + 100 ),
('Underflow: img - 100', img - 100 ),
('Original Image', img),
('Over clipping: np.where(img + 100 > 255)', np.where( img + 100.0 > 255, 255, img + 100 ) ),
('Under clipping: np.where(img -100 < 0)', np.where( img - 100.0 < 0, 0, img - 100 ) ),
]
# 결과를 matplotlib로 출력
cols = 3
rows = int( len( titles_and_images ) / cols + 0.5 )
plt.figure(figsize=(4*cols, 3*rows))
# 각 제목과 이미지를 반복하며 출력
for i, (title, image) in enumerate(titles_and_images) :
plt.subplot(rows, cols, i + 1)
plt.imshow(image, cmap='gray')
plt.title(title)
plt.axis('off')
pass
# 그래프 출력
plt.tight_layout()
plt.show()