opencv实现图像几何变换
作者:lixin051435 时间:2022-02-18 09:47:50
本文实例为大家分享了opencv实现图像几何变换的具体代码,供大家参考,具体内容如下
图像伸缩(cv2.resize)
图像的扩大与缩小有专门的一个函数,cv2.resize(),那么关于伸缩需要确定的就是缩放比例,可以是x与y方向相同倍数,也可以单独设置x与y的缩放比例。另外一个就是在缩放以后图像必然就会变化,这就又涉及到一个插值问题。那么这个函数中,缩放有几种不同的插值(interpolation)方法,在缩小时推荐cv2.INTER_ARER,扩大是推荐cv2.INTER_CUBIC和cv2.INTER_LINEAR。默认都是cv2.INTER_LINEAR,比如:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('d:/1.jpg')
# 插值:interpolation
# None本应该是放图像大小的位置的,后面设置了缩放比例,
#所有就不要了
res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
#直接规定缩放大小,这个时候就不需要缩放因子
height,width = img.shape[:2]
res2 = cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
plt.subplot(131)
plt.imshow(img)
plt.subplot(132)
plt.imshow(res1)
plt.subplot(133)
plt.imshow(res2)
plt.show()
图像平移(cv2.warpAffine)
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('d:/1.jpg')
H = np.float32([[1, 0, 100], [0, 1, 50]])
rows, cols = img.shape[:2]
res = cv2.warpAffine(img, H, (rows, cols)) # 需要图像、变换矩阵、变换后的大小
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res)
plt.show()
图像旋转(逆时针旋转,cv2.warpAffine和cv2.getRotationMatrix2D)
为了构造这个矩阵,opencv提供了一个函数:
cv2.getRotationMatrix2D(),这个函数需要三个参数,旋转中心,旋转角度,旋转后图像的缩放比例,比如下例:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('d:/1.jpg')
rows, cols = img.shape[:2]
# 第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
# 第三个参数:变换后的图像大小
res = cv2.warpAffine(img, M, (rows, cols))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res)
plt.show()
来源:https://blog.csdn.net/tsfx051435adsl/article/details/78912042
标签:opencv,图像变换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS动态添加与删除select中的Option对象(示例代码)
2023-09-17 02:15:31
Python五种下划线详解
2021-10-02 02:45:38
![](https://img.aspxhome.com/file/2023/6/91846_0s.png)
PyTorch中torch.tensor与torch.Tensor的区别详解
2023-03-07 10:48:15
python实战之用emoji表情生成文字
2023-04-27 03:22:23
![](https://img.aspxhome.com/file/2023/3/127293_0s.png)
JavaScript Dom编程:介绍学习书籍
2008-02-20 08:32:00
python实现的用于搜索文件并进行内容替换的类实例
2022-01-07 13:50:43
基于Python编写一个语音合成系统
2021-10-14 03:28:16
![](https://img.aspxhome.com/file/2023/7/84067_0s.png)
pycharm内无法import已安装的模块问题解决
2021-08-28 08:53:17
![](https://img.aspxhome.com/file/2023/1/121411_0s.jpg)
Python面向对象之继承原理与用法案例分析
2021-06-28 02:07:53
![](https://img.aspxhome.com/file/2023/4/65544_0s.png)
web前端页面性能优化
2009-08-15 12:31:00
Python去除字符串前后空格的几种方法
2021-12-22 02:10:02
利用python设计图像加密技术(Arnold算法)
2021-09-19 11:24:18
![](https://img.aspxhome.com/file/2023/5/102735_0s.jpg)
pytorch实现focal loss的两种方式小结
2023-07-02 14:43:22
通过表单的做为二进制文件上传request.totalbytes提取出上传的二级制数据
2011-03-16 10:39:00
Python将多个excel表格合并为一个表格
2021-10-18 22:50:22
![](https://img.aspxhome.com/file/2023/8/81818_0s.jpg)
python数据可视化Seaborn绘制山脊图
2023-12-22 09:04:17
![](https://img.aspxhome.com/file/2023/4/134444_0s.png)
Python利用wxPython制作股票价格查询工具
2021-11-25 19:09:52
![](https://img.aspxhome.com/file/2023/2/109452_0s.png)
django站点管理详解
2022-03-31 00:16:43
总结python实现父类调用两种方法的不同
2022-05-27 14:54:48
![](https://img.aspxhome.com/file/2023/1/123801_0s.png)
Python 运行 shell 获取输出结果的实例
2023-08-02 16:51:18