【DW打卡-计算机视觉基础】01_OpenCV框架与图像插值算法

主要内容

常见的插值算法有最近邻插值、双线性插值和三次样条插值

  • 最近邻插值,是指将目标图像中的点,对应到源图像中后,找到最相邻的整数点,作为插值后的输出。

  • 双线性插值就是线性插值在二维时的推广,在两个方向上做三次线性插值,

Python

函数原型:

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

参数:

参数 描述
src 【必需】原图像
dsize 【必需】输出图像所需大小
fx 【可选】沿水平轴的比例因子
fy 【可选】沿垂直轴的比例因子
interpolation 【可选】插值方式

插值方式:

cv.INTER_NEAREST 最近邻插值
cv.INTER_LINEAR 双线性插值
cv.INTER_CUBIC 基于4x4像素邻域的3次插值法
cv.INTER_AREA 基于局部像素的重采样

通常,缩小使用cv.INTER_AREA,放缩使用cv.INTER_CUBIC(较慢)和cv.INTER_LINEAR(较快效果也不错)。默认情况下,所有的放缩都使用cv.INTER_LINEAR。

代码

import cv2
if __name__ == '__main__':
    img = cv2.imread('./00_dog.jpg', cv2.IMREAD_UNCHANGED)
    print('原始图像大小', img.shape)

    scale_percent = 0.3
    width = int(img.shape[1] * scale_percent)
    height = int(img.shape[0] * scale_percent)
    dim = (width, height)
    # resize image
    resized0_img = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)

    fx = 1.5
    fy = 1.5
    # 在放大回去看看效果
    """
    @param dsize output image size; 
    @param fx scale factor along the horizontal axis;
    @param fy scale factor along the vertical axis;
    """
    resized1_img = cv2.resize(resized0_img, dsize=None, fx=fx, fy=fy, interpolation=cv2.INTER_NEAREST) # 紧邻
    resized2_img = cv2.resize(resized0_img, dsize=None, fx=fx, fy=fy, interpolation=cv2.INTER_LINEAR) # 插值

    print('Resized Dimensions : ',resized0_img.shape)

    cv2.imshow("Resized image", resized0_img)
    cv2.imshow("INTER_NEAREST image", resized1_img)
    cv2.imshow("INTER_LINEAR image", resized2_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

展示效果

posted @ 2021-09-14 23:44  山枫叶纷飞  阅读(69)  评论(0编辑  收藏  举报