網(wǎng)站源碼素材視頻專用客戶端app
1、雙線性插值概念
雙線性插值是一種用于在二維網(wǎng)格上進(jìn)行插值的方法,適用于圖像處理、計(jì)算機(jī)圖形學(xué)等領(lǐng)域。它通過利用四個(gè)鄰近點(diǎn)的已知值,估算出任意點(diǎn)的值。雙線性插值在兩個(gè)方向(通常是水平和垂直)上分別進(jìn)行線性插值,因此得名“雙線性”。雙線性插值是一種常用的圖像縮放方法,它通過在四個(gè)最近鄰像素之間進(jìn)行線性插值來計(jì)算新的像素值。以下是雙線性插值的詳細(xì)步驟和公式。
雙線性插值的步驟
假設(shè)我們有一個(gè)源圖像 I(x, y)
,目標(biāo)是將其縮放到一個(gè)新的尺寸 (new_width, new_height)
。對(duì)于目標(biāo)圖像中的每一個(gè)像素 (xx, yy)
,我們需要找到其在源圖像中的對(duì)應(yīng)位置,并使用雙線性插值計(jì)算該像素的值。
-
確定源圖像中的坐標(biāo):
- 計(jì)算目標(biāo)圖像中每個(gè)像素
(xx, yy)
對(duì)應(yīng)的源圖像坐標(biāo)(x, y)
。 - 使用縮放比例
xRatio = (src_width - 1) / (new_width - 1)
和yRatio = (src_height - 1) / (new_height - 1)
來計(jì)算源圖像坐標(biāo)。 x = floor(xx * xRatio)
和y = floor(yy * yRatio)
得到最接近的左上角像素坐標(biāo)。x_l
和y_l
分別是x
和y
的整數(shù)部分,x_h = min(x_l + 1, src_width - 1)
和y_h = min(y_l + 1, src_height - 1)
是右下角的像素坐標(biāo)。
- 計(jì)算目標(biāo)圖像中每個(gè)像素
-
計(jì)算權(quán)重:
- 計(jì)算小數(shù)部分
dx = xx * xRatio - x_l
和dy = yy * yRatio - y_l
。 - 這些小數(shù)部分將用于線性插值。
- 計(jì)算小數(shù)部分
-
雙線性插值公式:
- 使用四個(gè)最近鄰像素的值
I(x_l, y_l)
、I(x_h, y_l)
、I(x_l, y_h)
和I(x_h, y_h)
進(jìn)行插值。 - 首先在水平方向上進(jìn)行線性插值:
a = I ( x l , y l ) ? ( 1 ? d x ) + I ( x h , y l ) ? d x a = I(x_l, y_l) \cdot (1 - dx) + I(x_h, y_l) \cdot dx a=I(xl?,yl?)?(1?dx)+I(xh?,yl?)?dx b = I ( x l , y h ) ? ( 1 ? d x ) + I ( x h , y h ) ? d x b = I(x_l, y_h) \cdot (1 - dx) + I(x_h, y_h) \cdot dx b=I(xl?,yh?)?(1?dx)+I(xh?,yh?)?dx - 然后在垂直方向上進(jìn)行線性插值:
I ′ ( x x , y y ) = a ? ( 1 ? d y ) + b ? d y I'(xx, yy) = a \cdot (1 - dy) + b \cdot dy I′(xx,yy)=a?(1?dy)+b?dy
- 使用四個(gè)最近鄰像素的值
2、雙線性插值實(shí)現(xiàn)代碼(NumPy,CV2)
2.1 Python代碼
提供了3種實(shí)現(xiàn)方式:for-loop
、NumPy廣播機(jī)制
、CV2庫函數(shù)
:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import time
import cv2#方式1:for-loop
def bilinear_resize(img, new_shape):img = np.array(img)height, width, depth = img.shapenew_height, new_width = new_shaperesult = np.zeros((new_height, new_width, depth))x_ratio = float(width - 1) / new_widthy_ratio = float(height - 1) / new_heightfor i in range(new_height):for j in range(new_width):x_l, y_l = int(j * x_ratio), int(i * y_ratio)x_h, y_h = min(x_l + 1, width - 1), min(y_l + 1, height - 1)x_weight = (j * x_ratio) - x_ly_weight = (i * y_ratio) - y_la = img[y_l, x_l] * (1 - x_weight) + img[y_l, x_h] * x_weightb = img[y_h, x_l] * (1 - x_weight) + img[y_h, x_h] * x_weightresult[i, j] = a * (1 - y_weight) + b * y_weightreturn Image.fromarray(np.uint8(result))#方式2:NumPy廣播機(jī)制
def bilinear_resize_numpy(img, new_shape):img = np.array(img)height, width, depth = img.shapenew_height, new_width = new_shape# 計(jì)算縮放比例x_ratio = float(width - 1) / (new_width - 1) if new_width > 1 else 0y_ratio = float(height - 1) / (new_height - 1) if new_height > 1 else 0# 創(chuàng)建網(wǎng)格坐標(biāo)x_grid = np.linspace(0, width - 1, new_width)y_grid = np.linspace(0, height - 1, new_height)# 獲取每個(gè)新像素點(diǎn)在原圖中的位置x_l = np.floor(x_grid).astype(int)y_l = np.floor(y_grid).astype(int)x_h = np.minimum(x_l + 1, width - 1)y_h = np.minimum(y_l + 1, height - 1)# 計(jì)算權(quán)重x_weight = x_grid[:, None] - x_l[:, None]y_weight = y_grid[:, None] - y_l[:, None]# 使用numpy索引獲取四個(gè)鄰近像素的值a = img[y_l[:, None], x_l].reshape(new_height, new_width, depth)b = img[y_l[:, None], x_h].reshape(new_height, new_width, depth)c = img[y_h[:, None], x_l].reshape(new_height, new_width, depth)d = img[y_h[:, None], x_h].reshape(new_height, new_width, depth)# 調(diào)整權(quán)重形狀以匹配圖像數(shù)據(jù)x_weight = x_weight[:, :, None]y_weight = y_weight[:, :, None]# 進(jìn)行雙線性插值ab = a * (1 - x_weight.transpose((1, 0, 2))) + b * x_weight.transpose((1, 0, 2))cd = c * (1 - x_weight.transpose((1, 0, 2))) + d * x_weight.transpose((1, 0, 2))result = ab * (1 - y_weight) + cd * y_weightreturn Image.fromarray(np.uint8(result))#方式3:CV2庫函數(shù)
def bilinear_resize_cv2(img, new_shape):# 將PIL圖像轉(zhuǎn)換為numpy數(shù)組img_array = np.array(img)# 計(jì)算新的尺寸new_height, new_width = new_shape# 使用cv2.resize進(jìn)行雙線性插值start_time = time.time()resized_img = cv2.resize(img_array, (new_width, new_height), interpolation=cv2.INTER_LINEAR)processing_time = time.time() - start_timeprint(f"OpenCV processing time: {processing_time:.4f} seconds")# 將numpy數(shù)組轉(zhuǎn)換回PIL圖像并返回return Image.fromarray(resized_img)if __name__ == "__main__":# 加載圖像img_path = 'image.jpg'original_img = Image.open(img_path)# 設(shè)置新的尺寸# new_shape = (original_img.size[0] // 2, original_img.size[1] // 2)new_shape = (640,640)# 使用for循環(huán)遍歷處理并計(jì)時(shí)start_time = time.time()resized_img_for_loop= bilinear_resize(original_img, new_shape)numpy_time = time.time() - start_timeprint(f"for-loop processing time: {numpy_time:.4f} seconds")# 使用NumPy廣播機(jī)制處理并計(jì)時(shí)start_time = time.time()resized_img_numpy= bilinear_resize_numpy(original_img, new_shape)numpy_time = time.time() - start_timeprint(f"NumPy processing time: {numpy_time:.4f} seconds")# 使用OpenCV處理并計(jì)時(shí)resized_img_cv2 = bilinear_resize_cv2(original_img, new_shape)# 顯示結(jié)果(可選)# 創(chuàng)建一個(gè)包含三個(gè)子圖的圖形,并設(shè)置布局fig, axes = plt.subplots(1, 3, figsize=(15, 5))# 顯示第一張圖像axes[0].imshow(resized_img_for_loop)axes[0].set_title("Resized with NumPy")axes[0].axis('off')# 顯示第二張圖像axes[1].imshow(resized_img_numpy)axes[1].set_title("Resized with NumPy (New)")axes[1].axis('off')# 顯示第三張圖像axes[2].imshow(resized_img_cv2)axes[2].set_title("Resized with OpenCV")axes[2].axis('off')# 調(diào)整布局以防止重疊plt.tight_layout()# 顯示圖像plt.show()
2.2 運(yùn)行結(jié)果
運(yùn)行結(jié)果耗時(shí)對(duì)比:
for-loop processing time: 3.0354 seconds
NumPy processing time: 0.0666 seconds
OpenCV processing time: 0.0035 seconds
可以看出OpenCV處理速度最快。
- 另外本想嘗試支持
OpenCL
的pyopencl
的加速處理,但是報(bào)了點(diǎn)錯(cuò)就沒有放代碼。