python opencv对图像进行旋转且不裁剪图片的实现方法


Posted in Python onJuly 09, 2019

最近在做深度学习时需要用到图像处理相关的操作,在度娘上找到的图片旋转方法千篇一律,旋转完成的图片都不是原始大小,很苦恼,于是google到歪果仁的网站扒拉了一个方法,亲测好用,再次嫌弃天下文章一大抄的现象,虽然我也是抄歪果仁的。

废话不多说了,直接贴代码了。

def rotate_bound(image, angle):
  # grab the dimensions of the image and then determine the
  # center
  (h, w) = image.shape[:2]
  (cX, cY) = (w // 2, h // 2)
 
  # grab the rotation matrix (applying the negative of the
  # angle to rotate clockwise), then grab the sine and cosine
  # (i.e., the rotation components of the matrix)
  M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
  cos = np.abs(M[0, 0])
  sin = np.abs(M[0, 1])
 
  # compute the new bounding dimensions of the image
  nW = int((h * sin) + (w * cos))
  nH = int((h * cos) + (w * sin))
 
  # adjust the rotation matrix to take into account translation
  M[0, 2] += (nW / 2) - cX
  M[1, 2] += (nH / 2) - cY
 
  # perform the actual rotation and return the image
  return cv2.warpAffine(image, M, (nW, nH))

其他的不用多说了吧,第一个参数穿opencv读取的图像,第二个参数传入需要旋转的角度,enjoy!

以上这篇python opencv对图像进行旋转且不裁剪图片的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 判断自定义对象类型
Mar 21 Python
将字典转换为DataFrame并进行频次统计的方法
Apr 08 Python
详解python while 函数及while和for的区别
Sep 07 Python
python 制作自定义包并安装到系统目录的方法
Oct 27 Python
浅谈Python中的全局锁(GIL)问题
Jan 11 Python
Python OpenCV中的resize()函数的使用
Jun 20 Python
python爬虫项目设置一个中断重连的程序的实现
Jul 26 Python
numpy.ndarray 实现对特定行或列取值
Dec 05 Python
Django app配置多个数据库代码实例
Dec 17 Python
使用Python爬虫库requests发送表单数据和JSON数据
Jan 25 Python
浅谈python 中的 type(), dtype(), astype()的区别
Apr 09 Python
Python通过getattr函数获取对象的属性值
Oct 16 Python
python下的opencv画矩形和文字注释的实现方法
Jul 09 #Python
Python3 执行系统命令并获取实时回显功能
Jul 09 #Python
利用python开发app实战的方法
Jul 09 #Python
python设置环境变量的作用和实例
Jul 09 #Python
python版百度语音识别功能
Jul 09 #Python
利用Python实现Shp格式向GeoJSON的转换方法
Jul 09 #Python
python实现集中式的病毒扫描功能详解
Jul 09 #Python
You might like
php批量上传的实现代码
2013/06/09 PHP
ThinkPHP CURD方法之table方法详解
2014/06/18 PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
2016/07/02 PHP
PHP自定义函数实现格式化秒的方法
2016/09/14 PHP
Laravel 使用查询构造器配合原生sql语句查询的例子
2019/10/12 PHP
js操作模态窗口及父子窗口间相互传值示例
2014/06/09 Javascript
jquery实现点击页面计算点击次数
2015/01/23 Javascript
Jquery实现地铁线路指示灯提示牌效果的方法
2015/03/02 Javascript
jQuery制作简洁的图片轮播效果
2015/04/03 Javascript
Node.js插件安装图文教程
2016/05/06 Javascript
关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法
2016/06/30 Javascript
JavaScript基础重点(必看)
2016/07/09 Javascript
文件上传,iframe跨域数据提交的实现
2016/11/18 Javascript
原生JS发送异步数据请求
2017/06/08 Javascript
Node.js使用Koa搭建 基础项目
2018/01/08 Javascript
Vue.js中的computed工作原理
2018/03/22 Javascript
Vuex的基本概念、项目搭建以及入坑点
2018/11/04 Javascript
[09:23]国际邀请赛采访专栏:iG战队VK,Tongfu战队Cu
2013/08/05 DOTA
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
2015/05/20 Python
Python绘制的二项分布概率图示例
2018/08/22 Python
python中的json总结
2018/10/11 Python
浅谈python中拼接路径os.path.join斜杠的问题
2018/10/23 Python
pandas中read_csv的缺失值处理方式
2019/12/19 Python
Python任务调度模块APScheduler使用
2020/04/15 Python
Python面向对象实现方法总结
2020/08/12 Python
python 怎样进行内存管理
2020/11/10 Python
HTML5自定义属性的问题分析
2019/08/16 HTML / CSS
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
高中生学习总结的自我评价范文
2013/10/13 职场文书
医学类导师推荐信范文
2013/11/19 职场文书
生物科学专业职业规划书范文
2014/02/11 职场文书
党员公开承诺书范文
2014/03/25 职场文书
珠宝的促销活动方案
2014/08/31 职场文书
秋季运动会广播稿(30篇)
2014/09/13 职场文书
2016年全国爱牙日宣传活动总结
2016/04/05 职场文书
Nginx 配置 HTTPS的详细过程
2022/05/30 Servers