[opencv-python] 이미지 크기조절(resize) 하는 법


이미지 크기 조절(resize)

이미지 크기를 조절하는 예제를 알아보겠습니다.



이미지 불러오기

import cv2

img = cv2.imread("image/cat.jpg")
cv2.imshow("img", img)
cv2.waitKey()

cat.jpg 이미지를 불러와 화면에 띄웁니다.



이미지 크기 조절

img = cv2.imread("image/cat.jpg")
print("img.shape = {0}".format(img.shape))

resize_img = cv2.resize(img, (300, 300))
print("resize_img.shape = {0}".format(resize_img.shape))

cv2.imshow("img"img)
cv2.imshow("resize img", resize_img)
cv2.waitKey()

# 출력
# img.shape = (360, 480, 3)
# resize_img.shape = (300, 300, 3)

이미지 크기 조절 방법 1 - cv2.resize(img, dsize=(300, 300))

cv2.resize(원본 이미지, 결과 크기 이미지)로 이미지의 크기를 조절할 수 있습니다.
결과 크기 이미지는 Tuple이며 (width, height)의 형식입니다.



이미지 크기 조절 방법 2 - cv2.resize(img, dsize=(300, 300), interpolation=cv2.INTER_AREA)

cv2.resize(원본 이미지, 결과 크기 이미지, 보간법)로 이미지 크기를 조절할 수 있습니다.
보간법은 이미지의 크기를 변경하면 변경된 이미지의 픽셀 사이의 값을 결정해야 하는데, 이때 보간법을 사용합니다.
default는 INTER_LINEAR

Interpolation 속성

위의 보간법에 대한 설명
http://pkban.blogspot.com/2017/08/opencv-cvresize.html

주로 사용되는 보간법

사이즈를 축소할 경우: cv2.INTER_AREA
사이즈를 확대할 경우: cv2.INTER_CUBIC, cv2.INTER_LINEAR


이미지 크기 조절 방법 3 - cv2.resize(img, dsize=(0, 0), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA)

cv2.resize(원본 이미지, 결과 크기 이미지, 가로비, 세로비, 보간법)로 이미지 크기를 조절할 수 있습니다.
결과 크기 이미지를 (0, 0)으로 결과 이미지 크기를 설정하지 않은 경우에 한에 fx와 fy를 사용하여 이미지의 비율을 조절 할 수 있습니다.

fx가 0.3이라면 원본 이미지의 width가 0.3배로 변경
fy가 0.7이라면 원본 이미지의 height이 0.7배로 변경

img = cv2.imread("image/cat.jpg"
print("img.shape = {0}".format(img.shape)) 

resize_img = cv2.resize(img, (0, 0), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA)
print("resize_img.shape = {0}".format(resize_img.shape)) 

cv2.imshow("img", img) 
cv2.imshow("resize img", resize_img) 
cv2.waitKey()

# 출력
# img.shape = (360, 480, 3)
# resize_img.shape = (252, 144, 3)

360의 0.7이라면
360 / 100 * 70 = 252 (height)

480의 0.3이라면
480 / 100 * 30 = 144 (width)



주의

만약 dsize에 크기와 fx, fy의 비율을 설정한다면?
cv2.resize(img, dsize=(0, 0), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA)

img = cv2.imread("image/cat.jpg"
print("img.shape = {0}".format(img.shape)) 

resize_img = cv2.resize(img, (300, 300), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA) 
print("resize_img.shape = {0}".format(resize_img.shape)) 

cv2.imshow("img", img) 
cv2.imshow("resize img", resize_img) 
cv2.waitKey()

# 출력
# img.shape = (360, 480, 3)
# resize_img.shape = (300, 300, 3)

결과 이미지 크기와 가로비, 세로비가 모두 설정된 경우에는 결과 이미지 크기의 값으로 이미지의 크기가 조절됩니다.



전체코드

import cv2

img = cv2.imread("image/cat.jpg")
print("img.shape = {0}".format(img.shape))

resize_img = cv2.resize(img, (300, 300))
# resize_img = cv2.resize(img, (300, 300), interpolation=cv2.INTER_AREA)
# resize_img = cv2.resize(img, (0, 0), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA)
# resize_img = cv2.resize(img, (300, 300), fx=0.3, fy=0.7, interpolation=cv2.INTER_AREA)
print("resize_img.shape = {0}".format(resize_img.shape))

cv2.imshow("img", img)
cv2.imshow("resize img", resize_img)
cv2.waitKey()


참고문헌



댓글

이 블로그의 인기 게시물

[python]파이썬: csv reader header skip (첫번째 행 무시하기, 안읽기)