opencv实现对图像的插值操作 最近邻、双线性、双三次

如果本文对您有帮助,请帮忙点赞、评论、收藏,感谢!

python 为例

一. 函数原型

        dst=cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

        参数含义:

        src:input image.

        dst:output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.

        dsize:output image size; if it equals zero, it is computed as:

                dsize = Size(round(fx*src.cols), round(fy*src.rows))

                Either dsize or both fx and fy must be non-zero.

        fx :scale factor along the horizontal axis; when it equals 0, it is computed as

(double)dsize.width/src.cols

        fy :scale factor along the vertical axis; when it equals 0, it is computed as

(double)dsize.height/src.rows

        interpolation:interpolation method, see InterpolationFlags


三种插值方法的 interpolation 参数
 

二. 实验代码:

 1 import cv2
 2 import numpy as np 
 3 
 4 # 读入灰度图像
 5 im_path='../paojie_g.jpg'
 6 img = cv2.imread(im_path,0)
 7 # 注意应该先写宽度img.shape[1]*2,再写高度img.shape[0]*2
 8 NN_interpolation = cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_NEAREST)
 9 BiLinear_interpolation =  cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_LINEAR)
10 BiCubic_interpolation = cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_CUBIC)
11 
12 cv2.imshow('NN_interpolation',NN_interpolation)
13 cv2.imwrite('NN_interpolation.jpg',NN_interpolation)
14 cv2.imshow('BiLinear_interpolation',BiLinear_interpolation)
15 cv2.imwrite('BiLinear_interpolation.jpg',BiLinear_interpolation)
16 cv2.imshow('BiCubic_interpolation',BiCubic_interpolation)
17 cv2.imwrite('BiCubic_interpolation.jpg',BiCubic_interpolation)
18 cv2.waitKey(0)
19 cv2.destroyAllWindows()

三. 实验结果:


原图 ↑
 

NN_interpolation x2 ↑
 

BiLinear_interpolation  x2 ↑
 

BiCubic_interpolation   x2 ↑
 

        可以看到,最近邻插值算法放大图像后,目标图像边缘出现了明显的锯齿;而双线性和双三次插值算法没有出现明显的锯齿边缘。


四. 参考内容:

  https://www.jianshu.com/p/f360462f0db4

原文地址:https://www.cnblogs.com/wojianxin/p/12517101.html