반응형

resize() 함수를 사용하여 이미지를 확대 및 축소하는 방법을 다루고 있습니다.



2018. 10. 3 최초 작성

2023. 2. 14 제목 변경

 



이미지 확대 또는 축소시 사용할 보간법(interpolation methods) 지정해줄 수 있습니다. 지정해주지 않았을 때 디폴트값은 cv2.INTER_LINEAR입니다.

 

이미지 확대할때에는 cv2.INTER_CUBIC 또는 cv2.INTER_LINEAR을  권장합니다.

cv2.INTER_CUBIC을 사용하면 좀더 선명한 이미지를 얻을 수 있지만  처리속도가 상대적으로 느립니다. 동영상을 처리하거나 큰 이미지를 처리할 때에 체감이 될듯합니다.

 




이미지를 축소할때에는  cv.INTER_AREA를 권장합니다.

 




테스트에 사용한 전체 코드입니다. 

 

import cv2


# 원본 이미지
img_source = cv2.imread('test1.png')
cv2.imshow("original", img_source)

cv2.waitKey(0)


# 2배 이미지
img_result = cv2.resize(img_source, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
cv2.imshow("x2", img_result)

cv2.waitKey(0)


# 4배 이미지
height, width = img_source.shape[:2]
img_result = cv2.resize(img_source, (4*width, 4*height), interpolation = cv2.INTER_LINEAR )
cv2.imshow("x4 INTER_LINEAR", img_result)

height, width = img_source.shape[:2]
img_result2 = cv2.resize(img_source, (4*width, 4*height), interpolation = cv2.INTER_CUBIC )
cv2.imshow("x4 INTER_CUBIC", img_result)

cv2.waitKey(0)


# INTER_CUBIC를 사용해 확대한 4배 이미지를 0.5배한 이미지
img_result = cv2.resize(img_result2, None, fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)
cv2.imshow("x0.5 INTER_AREA", img_result)

img_result = cv2.resize(img_result2, None, fx=0.5, fy=0.5) # cv2.INTER_LINEAR
cv2.imshow("x0.5 INTER_LINEAR", img_result)

cv2.waitKey(0)


cv2.destroyAllWindows()




테스트 이미지 출처 https://en.wikipedia.org/wiki/Geometric_transformation 



반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts