opencv實現對圖像的插值操作 最近鄰、雙線性、雙三次


如果本文對您有幫助,請幫忙點贊、評論、收藏,感謝!

python 為例

一. 函數原型

        dst=cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

        參數含義:

        src:input image.

        dst:output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.

        dsize:output image size; if it equals zero, it is computed as:

                dsize = Size(round(fx*src.cols), round(fy*src.rows))

                Either dsize or both fx and fy must be non-zero.

        fx :scale factor along the horizontal axis; when it equals 0, it is computed as

(double)dsize.width/src.cols

        fy :scale factor along the vertical axis; when it equals 0, it is computed as

(double)dsize.height/src.rows

        interpolation:interpolation method, see InterpolationFlags


三種插值方法的 interpolation 參數
 

二. 實驗代碼:

 1 import cv2
 2 import numpy as np 
 3 
 4 # 讀入灰度圖像
 5 im_path='../paojie_g.jpg'
 6 img = cv2.imread(im_path,0)
 7 # 注意應該先寫寬度img.shape[1]*2,再寫高度img.shape[0]*2
 8 NN_interpolation = cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_NEAREST)
 9 BiLinear_interpolation =  cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_LINEAR)
10 BiCubic_interpolation = cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_CUBIC)
11 
12 cv2.imshow('NN_interpolation',NN_interpolation)
13 cv2.imwrite('NN_interpolation.jpg',NN_interpolation)
14 cv2.imshow('BiLinear_interpolation',BiLinear_interpolation)
15 cv2.imwrite('BiLinear_interpolation.jpg',BiLinear_interpolation)
16 cv2.imshow('BiCubic_interpolation',BiCubic_interpolation)
17 cv2.imwrite('BiCubic_interpolation.jpg',BiCubic_interpolation)
18 cv2.waitKey(0)
19 cv2.destroyAllWindows()

 


三. 實驗結果:


原圖 ↑
 

NN_interpolation x2 ↑
 

BiLinear_interpolation  x2 ↑
 

BiCubic_interpolation   x2 ↑
 

        可以看到,最近鄰插值算法放大圖像后,目標圖像邊緣出現了明顯的鋸齒;而雙線性和雙三次插值算法沒有出現明顯的鋸齒邊緣。


四. 參考內容:

  https://www.jianshu.com/p/f360462f0db4


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM