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()

  • 相关阅读:
    将vcf文件转化为plink格式并且保持phasing状态
    bcftools合并vcf文件
    NCBI通过氨基酸位置查看相邻SNP
    bcftools或vcftools提取指定区段或者多个指定位置的vcf文件(extract specified position )
    vcf文件去除非变异的基因型(use GATK exclude nonvariant in vcf format,0|0,0/0)
    使用bcftools提取指定样本的vcf文件(extract specified samples in vcf format)
    课程三(Structuring Machine Learning Projects),第一周(ML strategy(1)) —— 1.Machine learning Flight simulator:Bird recognition in the city of Peacetopia (case study)
    python中大于0的元素全部转化为1,小于0的元素全部转化为0的代码
    numpy中int类型与python中的int
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第三周(Hyperparameter tuning, Batch Normalization and Programming Frameworks) —— 2.Programming assignments
  • 原文地址:https://www.cnblogs.com/retacn-yue/p/6194165.html
Copyright © 2011-2022 走看看