本文只是提供一下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)