zoukankan      html  css  js  c++  java
  • python-opencv 图像几何变换--缩放、平移、旋转

    缩放

    缩放是调整图片的大小,可以指定输出图像尺寸大小,也可以指定缩放比例。

    opencv函数原型

    cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation)
    
    InputArray src                                                                                                          输入图像                                                                                                                                         
    OutputArrzy dst 输出图像
    Size 输出图像尺寸
    fx, fy x轴,y轴的缩放系数
    interpolation 插值方式

    interpolation插值方式:

    插值方式有INTER_NEAREST 最近邻插值、INTER_LINEAR 双线性插值、INTER_AREA 像素区域重采样、INTER_CUBIC 4*4像素邻域双三次插值、 INTER_LANCZOS4 8*8像素邻域Lanczos插值。其中INTER_LINEAR为默认的插值方法,首选的插值方法是INTER_AREA。我们可以使用以下方法调整图像大小:

    import cv2 as cv
    
    img = cv.imread(r'Lena.png', 1)
    res1 = cv.resize(img, None, fx=0.5, fy=0.5, interpolation=cv.INTER_AREA)
    # Or
    height, width = img.shape[ : 2]
    res2 = cv.resize(img, (int(0.5*width), int(0.5*height)), interpolation=cv.INTER_AREA)
    
    

    平移

    在对图像作平移操作时,需创建变换矩阵。2行3列矩阵,决定了何种变换。M矩阵则表示水平方向上平移为x而竖直方向上的平移距离为y。

    CodeCogsEqn

    import numpy as np
    import cv2 as cv
    
    img = cv.imread(r'Lena.png', 1)
    rows, cols, channels = img.shape
    M = np.float32([[1,0,100],[0,1,50]])
    res = cv.warpAffine(img, M, (cols, rows))
    # cv.warpAffine()第三个参数为输出的图像大小,值得注意的是该参数形式为(width, height)。
    cv.imshow('img', res)
    cv.waitKey(0)
    cv.destroyAllWindows()

     

    效果图

    imageimage

     

    旋转

    旋转需变换矩阵,而函数cv.getRotationMatrix2D()会找到此转换矩阵。

    import cv2 as cv
    
    img = cv.imread(r'Lena.png', 1)
    rows, cols, channels = img.shape
    rotate = cv.getRotationMatrix2D((rows*0.5, cols*0.5), 45, 1)
    '''
    第一个参数:旋转中心点
    第二个参数:旋转角度
    第三个参数:缩放比例
    '''
    res = cv.warpAffine(img, rotate, (cols, rows))
    cv.imshow('img', img)
    cv.imshow('res', res)
    cv.waitKey(0)
    cv.destroyAllWindows()

    效果图

    imageimage

  • 相关阅读:
    仿网易/QQ空间视频列表滚动连播炫酷效果
    UIScrollerView当前显示3张图
    iOS AVPlayer视频播放器
    iOS 购物车动画
    C++从零实现简单深度神经网络(基于OpenCV)
    Android 轻松实现仿淘宝地区选择
    微信小程序之下拉刷新,上拉更多列表实现
    iOS 指南针
    tableView 获取网络图片,并且设置为圆角(优化,fps)
    黑客技术 —— Linux 命令行
  • 原文地址:https://www.cnblogs.com/pheasant/p/13220341.html
Copyright © 2011-2022 走看看