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使用正则表达式检测密码强度源码分享
Jun 11 Python
Python网络爬虫实例讲解
Apr 28 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
Mar 13 Python
python 搭建简单的http server,可直接post文件的实例
Jan 03 Python
浅谈python str.format与制表符\t关于中文对齐的细节问题
Jan 14 Python
python 批量解压压缩文件的实例代码
Jun 27 Python
Django中在xadmin中集成DjangoUeditor过程详解
Jul 24 Python
python爬虫 urllib模块url编码处理详解
Aug 20 Python
python模块和包的应用BASE_PATH使用解析
Dec 14 Python
Selenium向iframe富文本框输入内容过程图解
Apr 10 Python
如何用python实现一个HTTP连接池
Jan 14 Python
只用50行Python代码爬取网络美女高清图片
Jun 02 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
phpMyAdmin无法登陆的解决方法
2017/04/27 PHP
php实现统计IP数及在线人数的示例代码
2020/07/22 PHP
用jquery与css打造个性化的单选框和复选框
2010/10/20 Javascript
50款非常棒的 jQuery 插件分享
2012/03/29 Javascript
js的Prototype属性解释及常用方法
2014/05/08 Javascript
jQuery插件EasyUI设置datagrid的checkbox为禁用状态的方法
2016/08/05 Javascript
JS控制静态页面传递参数并获取参数应用
2016/08/10 Javascript
基于Vue实现tab栏切换内容不断实时刷新数据功能
2017/04/13 Javascript
vuejs如何配置less
2017/04/25 Javascript
利用nginx + node在阿里云部署https的步骤详解
2017/12/19 Javascript
在vue项目中使用Nprogress.js进度条的方法
2018/01/31 Javascript
详解Webstorm 下的Angular2.0开发之路(图文)
2018/12/06 Javascript
详解单页面路由工程使用微信分享及二次分享解决方案
2019/02/22 Javascript
VUE引入第三方js包及调用方法讲解
2019/03/01 Javascript
layui动态加载多表头的实例
2019/09/05 Javascript
vue调用语音播放的方法
2019/09/27 Javascript
python中的yield使用方法
2014/02/11 Python
python 计算一个字符串中所有数字的和实例
2019/06/11 Python
python创建与遍历List二维列表的方法
2019/08/16 Python
解决Djang2.0.1中的reverse导入失败的问题
2019/08/16 Python
Python使用Opencv实现图像特征检测与匹配的方法
2019/10/30 Python
django admin后管定制-显示字段的实例
2020/03/11 Python
python中upper是做什么用的
2020/07/20 Python
HTML高亮关键字的实现代码
2018/10/22 HTML / CSS
英国的领先快速时尚零售商:In The Style
2019/03/25 全球购物
莱德杯高尔夫欧洲官方商店:Ryder Cup Shop
2019/08/14 全球购物
软件测试题目
2013/02/27 面试题
单位介绍信范文
2014/01/18 职场文书
美丽乡村建设实施方案
2014/03/23 职场文书
入党介绍人评语
2014/05/06 职场文书
电子专业自荐信
2014/07/01 职场文书
导游欢送词
2015/01/31 职场文书
解约证明模板
2015/06/19 职场文书
婚宴父亲致辞
2015/07/27 职场文书
八年级作文之一起的走过日子
2019/09/17 职场文书
Windows Server 2019 安装DHCP服务及相关配置
2022/04/28 Servers