碼字不易,如果此文對你有所幫助,請幫忙點贊,感謝!
一. 雙線性插值法原理:
① 何為線性插值?
插值就是在兩個數之間插入一個數,線性插值原理圖如下:

② 各種插值法:
插值法的第一步都是相同的,計算目標圖(dstImage)的坐標點對應原圖(srcImage)中哪個坐標點來填充,計算公式為:
srcX = dstX * (srcWidth/dstWidth)
srcY = dstY * (srcHeight/dstHeight)
(dstX,dstY)表示目標圖像的某個坐標點,(srcX,srcY)表示與之對應的原圖像的坐標點。srcWidth/dstWidth 和 srcHeight/dstHeight 分別表示寬和高的放縮比。
那么問題來了,通過這個公式算出來的 srcX, scrY 有可能是小數,但是原圖像坐標點是不存在小數的,都是整數,得想辦法把它轉換成整數才行。
不同插值法的區別就體現在 srcX, scrY 是小數時,怎么將其變成整數去取原圖像中的像素值。
最近鄰插值(Nearest-neighborInterpolation):看名字就很直白,四舍五入選取最接近的整數。這樣的做法會導致像素變化不連續,在目標圖像中產生鋸齒邊緣。
雙線性插值(Bilinear Interpolation):雙線性就是利用與坐標軸平行的兩條直線去把小數坐標分解到相鄰的四個整數坐標點。權重與距離成反比。
雙三次插值(Bicubic Interpolation):與雙線性插值類似,只不過用了相鄰的16個點。但是需要注意的是,前面兩種方法能保證兩個方向的坐標權重和為1,但是雙三次插值不能保證這點,所以可能出現像素值越界的情況,需要截斷。
③ 雙線性插值算法原理

假如我們想得到未知函數 f 在點 P = (x, y) 的值,假設我們已知函數 f 在 Q11 = (x1, y1)、Q12 = (x1, y2), Q21 = (x2, y1) 以及 Q22 = (x2, y2) 四個點的值。最常見的情況,f就是一個像素點的像素值。首先在 x 方向進行線性插值,然后再在 y 方向上進行線性插值,最終得到雙線性插值的結果。



④ 舉例說明



二. python實現灰度圖像雙線性插值算法:
灰度圖像雙線性插值放大縮小
1 import numpy as np 2 import math 3 import cv2 4 5 def double_linear(input_signal, zoom_multiples): 6 ''' 7 雙線性插值 8 :param input_signal: 輸入圖像 9 :param zoom_multiples: 放大倍數 10 :return: 雙線性插值后的圖像 11 ''' 12 input_signal_cp = np.copy(input_signal) # 輸入圖像的副本 13 14 input_row, input_col = input_signal_cp.shape # 輸入圖像的尺寸(行、列) 15 16 # 輸出圖像的尺寸 17 output_row = int(input_row * zoom_multiples) 18 output_col = int(input_col * zoom_multiples) 19 20 output_signal = np.zeros((output_row, output_col)) # 輸出圖片 21 22 for i in range(output_row): 23 for j in range(output_col): 24 # 輸出圖片中坐標 (i,j)對應至輸入圖片中的最近的四個點點(x1,y1)(x2, y2),(x3, y3),(x4,y4)的均值 25 temp_x = i / output_row * input_row 26 temp_y = j / output_col * input_col 27 28 x1 = int(temp_x) 29 y1 = int(temp_y) 30 31 x2 = x1 32 y2 = y1 + 1 33 34 x3 = x1 + 1 35 y3 = y1 36 37 x4 = x1 + 1 38 y4 = y1 + 1 39 40 u = temp_x - x1 41 v = temp_y - y1 42 43 # 防止越界 44 if x4 >= input_row: 45 x4 = input_row - 1 46 x2 = x4 47 x1 = x4 - 1 48 x3 = x4 - 1 49 if y4 >= input_col: 50 y4 = input_col - 1 51 y3 = y4 52 y1 = y4 - 1 53 y2 = y4 - 1 54 55 # 插值 56 output_signal[i, j] = (1-u)*(1-v)*int(input_signal_cp[x1, y1]) + (1-u)*v*int(input_signal_cp[x2, y2]) + u*(1-v)*int(input_signal_cp[x3, y3]) + u*v*int(input_signal_cp[x4, y4]) 57 return output_signal 58 59 # Read image 60 img = cv2.imread("../paojie_g.jpg",0).astype(np.float) 61 out = double_linear(img,2).astype(np.uint8) 62 # Save result 63 cv2.imshow("result", out) 64 cv2.imwrite("out.jpg", out) 65 cv2.waitKey(0) 66 cv2.destroyAllWindows()
三. 灰度圖像雙線性插值實驗結果:


四. 彩色圖像雙線性插值python實現
1 from PIL import Image 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import math 5 def BiLinear_interpolation(img,dstH,dstW): 6 scrH,scrW,_=img.shape 7 img=np.pad(img,((0,1),(0,1),(0,0)),'constant') 8 retimg=np.zeros((dstH,dstW,3),dtype=np.uint8) 9 for i in range(dstH): 10 for j in range(dstW): 11 scrx=(i+1)*(scrH/dstH)-1 12 scry=(j+1)*(scrW/dstW)-1 13 x=math.floor(scrx) 14 y=math.floor(scry) 15 u=scrx-x 16 v=scry-y 17 retimg[i,j]=(1-u)*(1-v)*img[x,y]+u*(1-v)*img[x+1,y]+(1-u)*v*img[x,y+1]+u*v*img[x+1,y+1] 18 return retimg 19 im_path='../paojie.jpg' 20 image=np.array(Image.open(im_path)) 21 image2=BiLinear_interpolation(image,image.shape[0]*2,image.shape[1]*2) 22 image2=Image.fromarray(image2.astype('uint8')).convert('RGB') 23 image2.save('out.png')
五. 彩色圖像雙線性插值實驗結果:


六. 最近鄰插值算法和雙三次插值算法可參考:
① 最近鄰插值算法:https://www.cnblogs.com/wojianxin/p/12515061.html
https://blog.csdn.net/Ibelievesunshine/article/details/104936006
② 雙三次插值算法:https://www.cnblogs.com/wojianxin/p/12516762.html
https://blog.csdn.net/Ibelievesunshine/article/details/104942406
七. 參考內容: