【OpenCV入门学习--python】上采样与下采样

OpenCV官网例子:使用OpenCV函数pyrUp()和pyrDown()对给定的图像进行下采样或上采样。(网址:https://docs.opencv.org/4.x/d4/d1f/tutorial_pyramids.html)

运行结果:

原图:
原图
上采样,pyrUp()期望大小是输入图像的两倍:
在这里插入图片描述
向下采样,pyrDown()期望的大小是输入图像的一半
在这里插入图片描述

理论知识:

向上采样:如果我们想让图变大,则填充为0的列(0)。
首先,将图像在每个维度上的大小提高到原来的两倍,其中新的偶数行和使用上面所示的相同内核(乘以4)执行卷积,以近似“缺失像素”的值。
向下采样;当我们缩小图像的尺寸时,实际上我们正在丢失图像的信息。

源代码:

import sys
import cv2 as cv
def main(argv):
    print("""
    Zoom In-Out demo
    ------------------
    * [i] -> Zoom [i]n
    * [o] -> Zoom [o]ut
    * [ESC] -> Close program
    """)
    #加载图像
    filename = argv[0] if len(argv) > 0 else 'chicky_512.png'
    # Load the image
    src = cv.imread(cv.samples.findFile(filename))
    # Check if image is loaded fine
    if src is None:
        print ('Error opening image!')
        print ('Usage: pyramids.py [image_name -- default ../data/chicky_512.png] \n')
        return -1
    
    while 1:
        rows, cols, _channels = map(int, src.shape)
        
        cv.imshow('Pyramids Demo', src)#创建窗口
        
        k = cv.waitKey(0)
        if k == 27:
            break
         #(按'i'后)上采样,pyrUp()期望大小是输入图像的两倍   
        elif chr(k) == 'i':
            src = cv.pyrUp(src, dstsize=(2 * cols, 2 * rows))
            print ('** Zoom In: Image x 2')
        #(按'o'后)下采样,pyrDown()期望的大小是输入图像的一半    
        elif chr(k) == 'o':
            src = cv.pyrDown(src, dstsize=(cols // 2, rows // 2))
            print ('** Zoom Out: Image / 2')
            
    cv.destroyAllWindows()
    return 0
if __name__ == "__main__":
    main(sys.argv[1:])
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值