소스 뷰어
import cv2
import numpy as np
import matplotlib.pyplot as plt
# lena.jpg 이미지를 로드하고 RGB로 변환
image = cv2.imread('lena.jpg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 스케일링 비율 정의
scale_x = 1.2 # 가로 방향 확대 비율
scale_y = 0.8 # 세로 방향 축소 비율
# 보간법별 크기 변경
interpolations = {
"Nearest": cv2.INTER_NEAREST,
"Linear": cv2.INTER_LINEAR,
"Cubic": cv2.INTER_CUBIC,
"Lanczos4": cv2.INTER_LANCZOS4,
"Area": cv2.INTER_AREA,
}
resized_images = {
name: cv2.resize(image_rgb, None, fx=scale_x, fy=scale_y, interpolation=method)
for name, method in interpolations.items()
}
# 이미지 출력
plt.figure(figsize=(15, 10))
fs = 15 # 폰트 사이즈
# 원본 이미지
plt.subplot(2, 3, 1)
plt.imshow(image_rgb)
plt.title(f"Original Image\nScale: 1.0 x 1.0", fontsize=fs)
plt.axis("off")
# 크기 변경된 이미지들
for i, (name, resized_image) in enumerate(resized_images.items(), 2):
plt.subplot(2, 3, i)
plt.imshow(resized_image)
plt.title(f"{name} Interpolation\nScale: {scale_x:.1f} x {scale_y:.1f}", fontsize=fs)
plt.axis("off")
plt.tight_layout()
plt.show()