• OpenCV 图像的几何变换


    一、图像缩放

    1.API

    cv2.resize(src, dsize, fx=0,fy=0,interpolation = cv2.INTER_LINEAR)

    参数:

    src :输入图像

    dsize:绝对尺寸

    fx,fy:相对尺寸

    interpolation插值方法

    2.代码演示 

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. [rows, cols] = img.shape[:2]
    7. res_1 = cv.resize(img, (2*cols, 2*rows), interpolation=cv.INTER_CUBIC)
    8. cv.imshow('image', res_1)
    9. cv.waitKey()
    10. res_2 = cv.resize(img, None, fx=0.5, fy=0.5)
    11. cv.imshow('image', res_1)
    12. cv.waitKey()

    二、图像平移

    1.API

    cv2.warpAffine(img, M, dsize)

    参数:

    img:输入图像

    M:2×3移动矩阵,为np.float32类型

    dsize:输出图像的大小

     2.代码演示 

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. [rows, cols] = img.shape[:2]
    7. M = np.float32([[1, 0, 100], [0, 1, 50]])
    8. dst = cv.warpAffine(img, M, (cols, rows))
    9. cv.imshow('image', dst)
    10. cv.waitKey()

    三、图像旋转

    1.API

    1. cv2.getRotationMatrix2D(center, angle, scale)
    2. cv.warpAffine()

    参数:

    center:旋转中心

    angle:旋转角度

    scale:缩放比例

    返回值:

    M旋转矩阵 

    2.代码演示 

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. [rows, cols] = img.shape[:2]
    7. M = cv.getRotationMatrix2D((cols/2, rows/2), 120, 1)
    8. dst = cv.warpAffine(img, M, (cols, rows))
    9. cv.imshow('image', dst)
    10. cv.waitKey()

     四、仿射变换

    1.API

    1. cv2.getAffineTransform()
    2. cv2.warpAffine()

    2.代码演示

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. [rows, cols] = img.shape[:2]
    7. pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
    8. pts2 = np.float32([[100, 100], [200, 50], [100, 250]])
    9. M = cv.getAffineTransform(pts1, pts2)
    10. dst = cv.warpAffine(img, M, (cols, rows))
    11. cv.imshow('image', dst)
    12. cv.waitKey()

    五、透射变换

    1.API

    1. cv2.getPerspectiveTransform()
    2. cv2.warpPerspective()

     2.代码演示

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. img = cv.resize(img, None, fx=0.5, fy=0.5)
    7. [rows, cols] = img.shape[:2]
    8. pts1 = np.float32([[56, 65], [368, 52], [28, 138], [389, 390]])
    9. pts2 = np.float32([[100, 145], [300, 100], [80, 290], [310, 300]])
    10. T = cv.getPerspectiveTransform(pts1, pts2)
    11. dst = cv.warpPerspective(img, T, (cols, rows))
    12. cv.imshow('image', dst)
    13. cv.waitKey()

    六、图像金字塔

    1.API

    1. cv2.pyrUp(img) #对图像进行上采样
    2. cv2.pyrDown(img) #对图像进行下采样

    2.代码演示

    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread('Genshin.jpeg', -1)
    5. cv.imshow('image', img)
    6. img = cv.pyrDown(img)
    7. img = cv.pyrDown(img)
    8. img = cv.pyrDown(img)
    9. cv.imshow('image', img)
    10. cv.waitKey()

     

  • 相关阅读:
    【Java】volatile-内存可见性问题
    【附源码】计算机毕业设计SSM图书管理系统小程序
    网络安全(黑客)自学
    自动化抢票 12306
    爬虫 — Bs4 数据解析
    ZooKeeper核心知识总结!
    [小程序技能树]路由跳转
    【数学+贪心】第十三届蓝桥杯省赛C++ B组《X 进制减法》(C++)
    tiechui_lesson14_网络连接请求的拦截
    数字IC前端笔试常见大题整理(简答+手撕)
  • 原文地址:https://blog.csdn.net/obstacle19/article/details/136630075