码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • opencv快速入门(三)——图像几何变换


    目录

    图像的裁剪、放大和缩小 

     错切变换

    镜像变换

    旋转变换 

     透视变换


    图像的裁剪、放大和缩小 

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. import cv2 as cv
    4. def show(img):
    5. if img.ndim == 2:
    6. plt.imshow(img, cmap='gray')
    7. else:
    8. plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
    9. plt.show()
    10. img = cv.imread('pic/rabbit500x333.jpg')
    11. show(img)

     

    1. M = np.array([
    2. [1, 0, 100],
    3. [0, 1, 50]
    4. ], dtype=np.float32)
    5. img2 = cv.warpAffine(img, M, (533, 560))
    6. show(img2)

     

     错切变换

     

     

    1. img = cv.imread('pic/rabbit500x333.jpg')
    2. show(img)
    3. M1 = np.array([
    4. [1, 0.2, 0],
    5. [0, 1, 0]
    6. ], dtype=np.float32)
    7. M2 = np.array([
    8. [1, 0, 0],
    9. [0.3, 1, 0]
    10. ], dtype=np.float32)
    11. img3 = cv.warpAffine(img, M1, (333,700))
    12. img4 = cv.warpAffine(img, M2, (333,700))
    13. show(img3)
    14. show(img4)

    镜像变换

     

     

    1. Mx = np.array([
    2. [-1, 0, 333],
    3. [0, 1, 0]
    4. ], dtype=np.float32)
    5. img2 = cv.warpAffine(img, Mx, (333, 500))
    6. show(img2)
    7. My = np.array([
    8. [1, 0, 0],
    9. [0, -1, 500]
    10. ], dtype=np.float32)
    11. img3 = cv.warpAffine(img, My, (333, 500))
    12. show(img3)

     

    1. img4 = cv.flip(img, 1)#垂直
    2. img5 = cv.flip(img, 0)#水平
    3. img6 = cv.flip(img, -1)#同时
    4. show(np.hstack([img, img4, img5, img6]))

     

    旋转变换 

     

    1. h, w, c = img.shape
    2. M2 = cv.getRotationMatrix2D((w//2, h//2), 45, 1)#center, angle, scale
    3. img3 = cv.warpAffine(img, M2, (533, 500))
    4. show(img3)

     透视变换

    1. img = cv.imread('pic/parthenon500x750.jpg')
    2. show(img)
    3. src = np.array([
    4. [210, 50],
    5. [610, 270],
    6. [650, 470],
    7. [150, 450]
    8. ], dtype=np.float32)
    9. dst = np.array([
    10. [150, 50],
    11. [650, 50],
    12. [650, 470],
    13. [150, 470]
    14. ], dtype=np.float32)
    15. M = cv.getPerspectiveTransform(src, dst)
    16. h, w, c = img.shape
    17. img2 = cv.warpPerspective(img, M, (w, h))
    18. show(img2)

     

     

     

     

     

     

     

  • 相关阅读:
    springboot基于WEB的高校文档打印系统毕业设计源码101004
    Adobe奇葩续费机制被网友狂喷:一不留神就扣2500,按月付费还随时取订?长点心吧...
    C++并发与多线程(4) | 传递临时对象作为线程参数的一些问题Ⅰ
    资深程序员-5年大厂经验代码究竟写的如何-H5-WEBgl-typeScript的笔记代码之一
    Element类型【2】
    Lua热更学习--使用toLua中的协程
    VScode 中 CRLF 和 LF 兼容问题,报错原因及解决方案
    12.2排序
    C# 使用SIMD向量类型加速浮点数组求和运算(1):使用Vector4、Vector<T>
    Python函数和模块
  • 原文地址:https://blog.csdn.net/Recursions/article/details/126949699
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号