双线性插值(Python版)

本文只是提供一下python版本实现,具体原理给出参考资料网址,应该已经有很多人写过相关的介绍了。

https://blog.csdn.net/jia20003/article/details/8014213
https://blog.csdn.net/xbinworld/article/details/65660665

python现在实现的运行速度非常慢,还是C++用指针进行操作会节省很多时间。当然还有一些优化点,后续我会慢慢补充进来。

如果发现有什么错误的地方还请评论指出。直接给出代码,后续还会更新当前文章。

def BilinearInterpolation(img,shape):
    height,width,channel = img.shape
    outimg = np.zeros((shape[0],shape[1],3))    
    for i in range(shape[0]):
        sy = i*height/shape[0]
        i_sy=int(sy)
        j_sy=sy-i_sy
        for j in range(shape[1]):
            sx = j*width/shape[1]
            i_sx=int(sx)
            j_sx=sx-i_sx
            c1 = (1-j_sy)*(1-j_sx)
            c2 = (j_sy)*(1-j_sx)
            c3 = (j_sy)*(j_sx)
            c4 = (1-j_sy)*(j_sx)
            Sji_x = saturate(int(np.round((j+0.5)*width/shape[1]-0.5)),width-1)
            Sji_y = saturate(int(np.round((i+0.5)*height/shape[0]-0.5)),height-1)
            Sji1_x = saturate(int(np.round((j+0.5)*width/shape[1]-0.5)),width-1)
            Sji1_y = saturate(int(np.round((i+1.5)*height/shape[0]-0.5)),height-1)
            Sj1i1_x = saturate(int(np.round((j+1.5)*width/shape[1]-0.5)),width-1)
            Sj1i1_y = saturate(int(np.round((i+1.5)*height/shape[0]-0.5)),height-1)
            Sj1i_x = saturate(int(np.round((j+1.5)*width/shape[1]-0.5)),width-1)
            Sj1i_y = saturate(int(np.round((i+0.5)*height/shape[0]-0.5)),height-1)
            for c in range(channel):                
                outimg[i,j,c] = int(img[Sji_y,Sji_x,c]*c1 + img[Sji1_y,Sji1_x,c]*c2 + img[Sj1i1_y,Sj1i1_x,c]*c3 + img[Sj1i_y,Sj1i_x,c]*c4)
    return np.array(outimg,dtype=np.uint8)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值