OpenCV實現圖片縮放
阿新 • • 發佈:2019-02-03
import cv2 img = cv2.imread('./res/aero3.jpg') print(img.shape[:2]) height, width = img.shape[:2] reSize1 = cv2.resize(img, (2*width, 2*height), interpolation=cv2.INTER_CUBIC) reSize2 = cv2.resize(img, (int(width/2), int(height/2)), interpolation=cv2.INTER_CUBIC) cv2.imshow('reSize1', reSize1) cv2.imshow('reSize2', reSize2) cv2.waitKey() cv2.destroyAllWindows()
- width, height = img.shape[:2] 獲取圖片的寬、高,image.shape是一個數組,取前兩位
- 第一個是放大第二個是縮小,引數要轉化成int型別。
interpolation引數說明:
interpolation 選項 | 所用的插值方法 |
---|---|
INTER_NEAREST | 最近鄰插值 |
INTER_LINEAR | 雙線性插值(預設設定) |
INTER_AREA | 使用畫素區域關係進行重取樣。 它可能是影象抽取的首選方法,因為它會產生無雲紋理的結果。 但是當影象縮放時,它類似於INTER_NEAREST方法。 |
INTER_CUBIC | 4x4畫素鄰域的雙三次插值 |
INTER_LANCZOS4 | 8x8畫素鄰域的Lanczos插值 |