首页 > 编程知识 正文

图像插值方法,python插值函数

时间:2023-05-05 11:27:30 阅读:34444 作者:4641

图像插值算法多采用最近邻插值和双线性插值。

最近邻插值如其名称所示,是取图像最近邻点的值作为新的插值,当然直接用于代码

def nearest _ interpolation (src _ img, tar _ size (3360 h=src _ img.shape [0] #原始图像的高度W=src_img.shape[1] #原始图像的宽度channel=src _ img.shape [2] tape ) 注意一定要注意NP.uint8forhinrange(tar_h-1 ) : forwinrange (tar _ w-1 ) :src_h=round(h/tar_size[0] ) # src_w] #赋值return tar_img双线性插值网络介绍了很多双线性插值的原理,这里不再赘述,一个简单的原理是周围的j ()1-u ) VF(I,j 1) u (1- v )

def bilinear _ interpolation (org _ img,tar_size ) : org_h,org_w,channel=org_img.shape #是原始图像的长度和宽度之间的dtype=np.uint8) #插值后图像的大小dst_h, 生成dst_w,并求出channel=dst_img.shape #插值后的图像的长度和宽度。scale_h=org_h/dst_h #插值后的倍数scale _ w=org _ w/dst 计算原图像中对象像素的位置src _ y=j * scale _ wsrc _ x _ int=int (math.floor (src _ x ) ),取得src整体的u=src _ x-src _ x 确定边界if src _ x _ int1==org _ horsrc _ y _ int1==org _ int1=3360 ) *org_img[src_x_int,src :]=(1-u )求(1-u ) v*org_img ) src_y_int,src _ y _ int 1, ) u*(1-v ) org_img ) src

所谓frompilimportimageimportmatplotlib.pyplotaspltimportcv2ascvimportnumpyasnpimportmath #最近邻插值, 是简单地选择x和y整数部分的坐标像素点并代入新的图像def nearest _ interpolation (src _ img ),tar _ size (3360 h=src _ img.shape [0] #元原始图像的宽度channel]=src _ img.shape [2] tape tar _ size [1] #插值后的宽度tar_img=NP.Zeros([tar_h,tar_W,channel 注意一定要注意NP.uint8forhinrange(tar_h-1 ) : forwinrange (tar _ w-1 ) :src_h=round(h/tar_size[0] ) # j ) UVF(I1,j 1 )的计算由def bilinear _ interpolation (org _ img,dst_shape ) : org_h,org_w, channel=org_img.shape #得到的原始图像的长度和宽度dst _ img=NP.zeros ((org _ h * dst _ shape [0],org _ w * dst _ shape channel=dst_img.shape #插值后的倍数scale_h=org_h/dst_h #插值后的倍数scale _ w=org _ w/dst _ wforinranance 计算原图像中对象像素的位置src _ y=j * scale _ wsrc _ x _ int=int (math.floor (src _ x ) ),取得src整体的u=src _ x-src _ x 确定是否检测边界if src _ x _ int1==org _ horsrc _ y _ int1==org _ int1=3360 ) *org_img[src_x_int,src :]=(1-u )求(1-u ) v*org_img ) src_y_int,src _ y _ int 1, ) u*(1-v ) org_img ) src src_x_int1) src ) ) u 3360 ) return dst _ img image=image.open (img.jpg ) )读取图像imgdata=NP.array(image ) tar _ img=nearest _ interpolation (imgdata,tar_size ) #邻域插值tar_img=bilinear_interpolation )邻域插值tar _ intion tar_size ) ##双线性插值PLT.figure(image ) PLT.im show (image ) PLT.axis (PLT.title ) image (PLT.show )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。