当前位置:网站首页>3. opencv几何变换
3. opencv几何变换
2022-07-27 16:08:00 【追逐梦想的阿光】
1. 平移变换
rows, cols = img.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]])
res = cv.warpAffine(img, M, (cols, rows))

cv.warpAffine(图片源, 变换矩阵, 变换后图片大小)。平移是对象位置的移动。如果你知道在(x,y)方向的转变,使其为(tx,ty),可以创建转换矩阵
M = [ 1 0 t x 0 1 t y ] (1) M=\begin{bmatrix} 1&0&tx\\ 0&1&ty\\ \end{bmatrix}\tag{1} M=[1001txty](1)
注意 cv.warpAffine()功能的第三个参数是输出图像的大小,该图像应以 **(宽度、高度)**的形式出现。宽度=列数,高度=行数。
2. 旋转变换
角度的变换通过M的形式矩阵实现:
M = [ c o s θ − s i n θ s i n θ c o s θ ] (2) M=\begin{bmatrix} cosθ&-sinθ\\ sinθ&cosθ\\ \end{bmatrix}\tag{2} M=[cosθsinθ−sinθcosθ](2)
opencv提供可调节的旋转中心进行缩放旋转,修改后的旋转矩阵如下:
M = [ α β ( 1 − α ) ⋅ c e n t e r . x − β ⋅ c e n t e r . y − β α β ⋅ c e n t e r . x + ( 1 − α ) ⋅ c e n t e r . y ] (3) M=\begin{bmatrix} α&β&(1−α)⋅center.x−β⋅center.y\\ -β&α&β⋅center.x+(1−α)⋅center.y\\ \end{bmatrix}\tag{3} M=[α−ββα(1−α)⋅center.x−β⋅center.yβ⋅center.x+(1−α)⋅center.y](3)
其中: α=scale⋅cosθ, β=scale⋅sinθ
要找到此转换矩阵,OpenCV 提供了一个功能 cv.getRotationMatrix2D(),查看以下示例,该示例将图像旋转 90 度,无需任何缩放即可将图像旋转到中心。
rows, cols = img.shape
M = cv.getRotationMatrix2D((cols/2, rows/2), 90, 1)# 旋转中心, 旋转角度, 旋转后缩放尺度
res = cv.warpAffine(img, M, (cols, rows))

3. 仿射变换
在 仿射转换中,原始图像中的所有平行线仍将在输出图像中平行。要找到转换矩阵,我们需要从输入图像和它们中找出三点在输出图像相应的位置。利用cv.getAffineTransform()将创建一个2x3矩阵,这是要传到 cv.warpAffine()中。
img = cv.imread('drawing.png')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
dst = cv.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

4. 透视变换
对于透视转换,需要一个 3x3 转换矩阵。即使在转换之后,直线仍将保持直线。要找到此转换矩阵,您需要输入图像上的 4 个点和输出图像上的相应点,在四个点中,其中三个不能是线性的,然后利用cv.getPerspectiveTransform() 可以将转换矩阵找出来,然后将这个3x3矩阵用于 cv.warpPerspective ()。
img = cv.imread('sudoku.png')
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 = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

边栏推荐
- 类的六大关系——依赖和关联的区别
- 多线程导入数据并生成错误文件用redis存储
- Knowledge dry goods: basic storage service novice Experience Camp
- 深度学习:GCN案例
- 同心向前,Google Play 十周年啦!
- [user article] examples of P4 consolidation practice guide disassemble resolve
- 荣耀、小米发双十一战报:都称自己是冠军
- Together with Samsung, vivo will promote exynos980 dual-mode 5g mobile phone!
- 动态链表4单向循环链表(LoopSingle实现)
- About the problem of abnormal C printing
猜你喜欢
![[learning notes] the implementation principle of the ordered set Zset in redis - skip table](/img/c6/5d9f48fce1dc1c78b8c7dbcf046fc7.png)
[learning notes] the implementation principle of the ordered set Zset in redis - skip table

浅论分布式训练中的recompute机制

IDEA打包war包与war包位置

Set up SSO based on SAML 2.0 in salesforce and enable JIT user provisioning (between SF orgs / between SF org and experience cloud / other IDPs)

使用分布式框架WCF出现的BUG记录

深度学习:STGCN学习笔记

Introduction to ef framework

Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
![[Southwest University] information sharing of postgraduate entrance examination and re examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Southwest University] information sharing of postgraduate entrance examination and re examination

二叉树概念
随机推荐
org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous err
国巨斥资18亿美元收购竞争对手Kemet,交易或在明年下半年完成
嘉楠耘智已完成预路演,预计11月20日登陆纳斯达克
The latest advanced interview questions for big factories are necessary
小米CC9 Pro拆解:后置五摄成本是骁龙855数倍!
Please ask the great God a question, flinkcdc, the datetime field in synchronous MySQL will become a timestamp. Has anyone ever encountered it
[learning notes] classification of locks in the database
canvas根据坐标点绘制图形
动态链表3队列的链式存储结构(LinkedQueue实现)
MySQL adds users and grants query only permission
Salesforce runs all test classes and gets coverage reports
CFA exam registration instructions
深度学习:STGCN学习笔记
多线程实现循环
查看端口PID及结束进程
js实现页面或DOM元素平滑滚动
Add music to the program interface and load background photos.
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
【学习笔记】Redis中有序集合zset的实现原理——跳表
深度学习-论文阅读:动作结构性图卷积网络AS-GCN