最近写深度学习网络需要对feature map进行双线性插值,opencv的resize不能对图像进行批处理,网上找到的双线性插值的代码基本都是三重for循环实现的,效率太低下,干脆自己用numpy库的矩阵运算实现了一个。
双线性插值原理参考:
https://zhuanlan.zhihu.com/p/112030273
感谢大佬Orz。
本文知乎同款:https://zhuanlan.zhihu.com/p/266845896
import numpy as np
def bilinear_interpolate(source, scale=2, pad=0.5):
sour_shape = source.shape
(sh, sw) = (sour_shape[-2], sour_shape[-1])
padding = pad*np.ones((sour_shape[0], sour_shape[1], sh+1, sw+1))
padding[:,:,:-1,:-1] = source
(th, tw) = (round(scale*sh), round(scale*sw))
grid = np.array(np.meshgrid(np.arange(th), np.arange(tw)), dtype=np.float32)
xy = np.copy(grid)
xy[0] *= sh/th
xy[1] *= sw/tw
x = xy[0].flatten()
y = xy[1].flatten()
c

本文介绍了如何利用numpy库的矩阵运算来高效地实现双线性插值,避免了传统的三重for循环方式,适用于图像处理中的批处理操作。内容包括双线性插值原理的引用,代码实现细节,以及最终用狗子照片进行的测试验证,结果显示放大和缩小都得到了正确结果。
最低0.47元/天 解锁文章

7339

被折叠的 条评论
为什么被折叠?



