双线性差值python

# 将双线性插值法源代码复现
import cv2 as cv
import numpy as np

dst_h = 320
dst_w = 640
dst = np.zeros([dst_h, dst_w, 3], dtype='uint8')
src = cv.imread("tree.jpg")

src_h = src.shape[0]
src_w = src.shape[1]
scale_y = src.shape[0] / dst_h
scale_x = src.shape[1] / dst_w
for n in range(3):
    for dst_y in range(dst_h):
        for dst_x in range(dst_w):
            src_y = (dst_y+0.5) * scale_y - 0.5
            src_x = (dst_x+0.5) * scale_x - 0.5

            src_y_0 = int(src_y)
            src_y_1 = int(min(src_y+1, src_h))
            src_x_0 = int(src_x)
            src_x_1 = int(min(src_x+1, src_w))

            value0 = (src_x_1-src_x)*src[src_y_0,src_x_0,n] + (src_x - src_x_0)*src[src_y_0,src_x_1,n]
            value1 = (src_x_1-src_x)*src[src_y_1,src_x_0,n] + (src_x - src_x_0)*src[src_y_1,src_x_1,n]

            dst[dst_y,dst_x,n]=int( (src_y_1-src_y)*value0 + (src_y-src_y_0)*value1)

print(type(dst))
print(type(src))
cv.imshow("img", src)
cv.waitKey(0)
cv.imshow("dst", dst)
cv.waitKey(0)
cv.destroyAllWindows()
cv.imwrite("250x250.jpg", dst)

你可能感兴趣的:(工具包,python,numpy,开发语言)