圖像處理: 五種 插值法

參考:https://blog.csdn.net/jningwei/article/details/78822026

Syntax

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst

interpolation 選項 所用的插值方法
INTER_NEAREST 最近鄰插值
INTER_LINEAR 雙線性插值(默認設置)
INTER_AREA 使用像素區域關係進行重採樣。 它可能是圖像抽取的首選方法,因爲它會產生無雲紋理的結果。 但是當圖像縮放時,它類似於INTER_NEAREST方法。
INTER_CUBIC 4x4像素鄰域的雙三次插值
INTER_LANCZOS4 8x8像素鄰域的Lanczos插值

INTER_NEAREST | 最近鄰插值

在一維空間中,最近點插值就相當於四捨五入取整。在二維圖像中,像素點的座標都是整數,該方法就是選取離目標點最近的點。

會在一定程度上損失 空間對稱性(Alignment),在 RoI Pooling 中使用。

這裏寫圖片描述

INTER_LINEAR | 雙線性插值(默認設置)

在兩個方向分別進行一次線性插值。

這裏寫圖片描述

在圖像處理的時候,我們先根據

srcX = dstX* (srcWidth/dstWidth)
srcY = dstY * (srcHeight/dstHeight)

    寫成 f(i+u,j+v) 的形式,則 u=0.2,v=0.4, i=1, j=3

    f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1) 
    

      保證了 空間對稱性(Alignment),在 RoI Align 中使用。

      這裏寫圖片描述

      INTER_AREA | 使用像素區域關係進行重採樣。

      略。

      INTER_CUBIC | 4x4像素鄰域的雙三次插值

      略。

      INTER_LANCZOS4 | 8x8像素鄰域的Lanczos插值

      在x,y方向分別對相鄰的八個點進行插值,也就是計算加權和,所以它是一個8x8的描述子。

      Code

      # coding=utf-8
      
      import cv2
      """
      INTER_NEAREST | 最近鄰插值
      INTER_LINEAR | 雙線性插值(默認設置)
      INTER_AREA |  使用像素區域關係進行重採樣
      INTER_CUBIC  | 4x4像素鄰域的雙三次插值
      INTER_LANCZOS4 |  8x8像素鄰域的Lanczos插值
      """
      
      if __name__ == '__main__':
          img = cv2.imread("girl.jpg")
          height, width = img.shape[:2]
      
          # 縮小圖像
          size = (int(width*0.8), int(height*0.7))
          shrink_NEAREST = cv2.resize(img, size, interpolation=cv2.INTER_NEAREST)
          shrink_LINEAR = cv2.resize(img, size, interpolation=cv2.INTER_LINEAR)
          shrink_AREA = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
          shrink_CUBIC = cv2.resize(img, size, interpolation=cv2.INTER_CUBIC)
          shrink_LANCZOS4 = cv2.resize(img, size, interpolation=cv2.INTER_LANCZOS4)
      
          # 放大圖像
          fx = 1.2
          fy = 1.1
          enlarge_NEAREST = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_NEAREST)
          enlarge_LINEAR = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_LINEAR)
          enlarge_AREA = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_AREA)
          enlarge_CUBIC = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC)
          enlarge_LANCZOS4 = cv2.resize(img, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_LANCZOS4)
      
          # 保存圖像
          cv2.imwrite("shrink_NEAREST.jpg", shrink_NEAREST)
          cv2.imwrite("shrink_LINEAR.jpg", shrink_LINEAR)
          cv2.imwrite("shrink_AREA.jpg", shrink_AREA)
          cv2.imwrite("shrink_CUBIC.jpg", shrink_CUBIC)
          cv2.imwrite("shrink_LANCZOS4.jpg", shrink_LANCZOS4)
      
          cv2.imwrite("enlarge_NEAREST.jpg", enlarge_NEAREST)
          cv2.imwrite("enlarge_LINEAR.jpg", enlarge_LINEAR)
          cv2.imwrite("enlarge_AREA.jpg", enlarge_AREA)
          cv2.imwrite("enlarge_CUBIC.jpg", enlarge_CUBIC)
          cv2.imwrite("enlarge_LANCZOS4.jpg", enlarge_LANCZOS4)

        Demo

        原圖像:
        這裏寫圖片描述

        利用插值縮小

        最近鄰插值
        這裏寫圖片描述

        雙線性插值(默認設置)
        這裏寫圖片描述

        使用像素區域關係進行重採樣
        這裏寫圖片描述

        4x4像素鄰域的雙三次插值
        這裏寫圖片描述

        8x8像素鄰域的Lanczos插值
        這裏寫圖片描述

        利用插值放大

        最近鄰插值
        這裏寫圖片描述

        雙線性插值(默認設置)
        這裏寫圖片描述

        使用像素區域關係進行重採樣
        這裏寫圖片描述

        4x4像素鄰域的雙三次插值
        這裏寫圖片描述

        8x8像素鄰域的Lanczos插值
        這裏寫圖片描述

        發佈了36 篇原創文章 · 獲贊 13 · 訪問量 2萬+
        發表評論
        所有評論
        還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
        相關文章