利用python实现图像的最邻近插值,双线性内插和双三次卷积插值

python实现图像的最邻近插值,双线性内插和双三次卷积插值代码演示

from PIL import Image

import numpy as np
import math


def NN_interpolation(img, dstH, dstW):      #定义最邻近插值函数,三个参数分别为图像,插值后像素高度,宽度
    scrH, scrW, _ = img.shape       #图像形状是原始图像的高和宽
    retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8)        #返回图像是生成的无符号整数8位,zeros的第一个参数含义
    for i in range(dstH):       #range()是创建的用于for循环的整数列表,在此即在高的范围内创建所有整数列表下,嵌套宽的整数列表
        for j in range(dstW):       #每行进行循环计算,最邻近插值的计算公式如下
            scrx = round((i + 1) * (scrH / dstH))       #像素的横纵位置四舍五入为整数
            scry = round((j + 1) * (scrW / dstW))
            retimg[i, j] = img[scrx - 1, scry - 1]
    return retimg

def BiLinear_interpolation(img, dstH, dstW):        #同样定义双线性内插函数
    scrH, scrW, _ = img.shape
    img = np.pad(img, ((0, 1), (0, 1), (0, 0)), 'constant')        #pad填充,第一个参数img是带填充的图像或数组,第二个参数是填充的形状,第三个参数是填充的方法‘constant’
    retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8)      #形状,类型,默认为numpy.float64
    for i in range(dstH):
        for j in range(dstW):
            scrx = (i + 1) * (scrH / dstH) - 1
            scry = (j + 1) * (scrW / dstW) - 1
            x = math.floor(scrx)        #向下取整
            y = math.floor(scry)
            u = scrx - x
            v = scry - y
            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]
    return retimg


def BiCubic(x):
    x = abs(x)
    if x <= 1:
        return 1-2*(x**2)+(x**3)
    elif x < 2:
        return 4-8*x+5*(x**2)-(x**3)
    else:
        return 0
# 双三次卷积有三个for循环
def BiCubic_interpolation (img, dstH, dstW):
    scrH, scrW, _ = img.shape
    # img = np.pad(img,((1,3),(1,3),(0,0)),'constant')
    retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8)
    for i in range(dstH):
        for j in range(dstW):
            scrx = i*(scrH/dstH)
            scry = j*(scrW/dstW)
            x = math.floor(scrx)
            y = math.floor(scry)
            u = scrx-x
            v = scry-y
            tmp = 0
            for ii in range(-1, 2):
                for jj in range(-1, 2):
                    if x + ii < 0 or y + jj < 0 or x + ii >= scrH or y + jj >= scrW:
                        continue
                    tmp += img[x + ii, y + jj] * BiCubic(ii - u) * BiCubic(jj - v)      #调用了BiCubic()
            retimg[i, j] = np.clip(tmp, 0, 255)
    return retimg
im_path = 'E:/2python/interpolation_used/photograph.jpg'
image = np.array(Image.open(im_path))

image1 = NN_interpolation(image, image.shape[0]*2, image.shape[1]*2)
image1 = Image.fromarray(image1.astype('uint8')).convert('RGB')
image1.save('E:/2python/interpolation_used/photograph1.jpg')

image2 = BiLinear_interpolation(image, image.shape[0]*2, image.shape[1]*2)
image2 = Image.fromarray(image2.astype('uint8')).convert('RGB')
image2.save('E:/2python/interpolation_used/photograph2.jpg')

image3 = BiCubic_interpolation(image, image.shape[0]*2, image.shape[1]*2)
image3 = Image.fromarray(image3.astype('uint8')).convert('RGB')
image3.save('E:/2python/interpolation_used/photograph3.jpg')

print("over")		#提示自己插值完成
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值