zoukankan      html  css  js  c++  java
  • opencv_python学习笔记十一

    14 几何变换

    移动,旋转,仿射变换

    常用函数:

    cv2.getPerspectiveTransform()

    函数原型
    def getPerspectiveTransform(src,
                                dst)

    cv2.warpAffine()

    函数原型,接收2*3的矩阵
    def warpAffine(src, #输入源图像
                   M,#透视变换的矩阵
                   dsize,#输出图像尺寸
                   dst=None,#输出图像
                   flags=None,#输出图像的插值方法
                   borderMode=None,#图像边界的处理方式
                   borderValue=None)#边界有颜色设置


    cv2.warpPerspective()

    函数原型,接收3*3的矩阵
    def warpPerspective(src,#输入图像
                        M,
                        dsize,#输出图像大小
                        dst=None,#输出图像
                        flags=None, #输出图像的插值方法
                        borderMode=None,#图像边界的处理方式
                        borderValue=None)#边界颜色的设置

    1 扩展缩放

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2016/11/15 14:10
    # @Author  : Retacn
    # @Site    : 扩展缩放改变图像尺寸大小
    # @File    : imageZoom.py
    # @Software: PyCharm
    import cv2
    import numpy as np

    img=cv2.imread('test.jpg')

    #none是输出图像的尺寸,由于在后面设置,所以这里设为none
    res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)

    #设置输出图像的尺寸
    height,width=img.shape[:2]
    res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)

    while(1):
        cv2.imshow('res',res)
        cv2.imshow('img',img)

        #esc退出
        
    if cv2.waitKey(1)&0xFF==27:
            break
    cv2.destroyAllWindows()

    2 平移

    示例代码如下:

    3 旋转

    示例代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2016/11/15 15:00
    # @Author  : Retacn
    # @Site    : 图像旋转
    # @File    : imageRotate.py
    # @Software: PyCharm

    import cv2
    import numpy as np

    img=cv2.imread('test.jpg',0)
    rows,cols=img.shape

    #可以通过设置以下三个参数
    m=cv2.getRotationMatrix2D((cols/2,rows/2),#旋转中心
                              
    45,#旋转角度
                              
    0.6)#缩放比例

    dst=cv2.warpAffine(img,m,(2*cols,2*rows))
    while(1):
        cv2.imshow('img',dst)
        #按下esc退出
        
    if cv2.waitKey(1)&0xFF==27:
            break
    #释放
    cv2.destroyAllWindows()

    4 仿射变换

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2016/11/15 15:11
    # @Author  : Retacn
    # @Site    : 仿射变换
    # @File    : imageAffine.py
    # @Software: PyCharm

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt

    img=cv2.imread('test1.jpg')
    rows,cols,ch = img.shape

    pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
    pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

    M = cv2.getPerspectiveTransform(pts1,#输入点
                                    
    pts2)#输出点

    dst = cv2.warpPerspective(img,M,(cols,rows))

    plt.subplot(121),plt.imshow(img),plt.title('Input')
    plt.subplot(122),plt.imshow(dst),plt.title('Output')
    plt.show()

    5 透视变换

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2016/11/15 16:34
    # @Author  : Retacn
    # @Site    : 透视变换
    # @File    : imagePerspective.py
    # @Software: PyCharm

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt

    img=cv2.imread('test1.jpg')
    rows,cols,ch = img.shape

    pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
    pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

    M = cv2.getPerspectiveTransform(pts1,pts2)

    dst = cv2.warpPerspective(img,M,(rows,cols))

    plt.subplot(121),plt.imshow(img),plt.title('Input')
    plt.subplot(122),plt.imshow(dst),plt.title('Output')
    plt.show()

  • 相关阅读:
    Awstats显示国家地区插件GeoIP安装 枯木
    springboot_shiro与shiro.ini文件
    Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
    mybatis的dao层xml
    springboot的配置文件
    万事无忧之用泡MM的方式演绎设计模式
    C#设计模式
    软件架构
    什么时候应该使用Web Service
    Spring框架快速入门之简介
  • 原文地址:https://www.cnblogs.com/retacn-yue/p/6194165.html
Copyright © 2011-2022 走看看