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 相关文章推荐
在Django中限制已登录用户的访问的方法
Jul 23 Python
Request的中断和ErrorHandler实例解析
Feb 12 Python
python获取文件路径、文件名、后缀名的实例
Apr 23 Python
Python闭包函数定义与用法分析
Jul 20 Python
对python读写文件去重、RE、set的使用详解
Dec 11 Python
Django框架模板的使用方法示例
May 25 Python
查看端口并杀进程python脚本代码
Dec 17 Python
在tensorflow中实现屏蔽输出的log信息
Feb 04 Python
在python里使用await关键字来等另外一个协程的实例
May 04 Python
Python logging日志模块 配置文件方式
Jul 12 Python
Vs Code中8个好用的python 扩展插件
Oct 12 Python
基于Python采集爬取微信公众号历史数据
Nov 27 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 fsockopen中多线程问题的解决办法[翻译]
2011/11/09 PHP
simplehtmldom Doc api帮助文档
2012/03/26 PHP
浅析使用Turck-mmcache编译来加速、优化PHP代码
2013/06/20 PHP
PHP中使用curl入门教程
2015/07/02 PHP
Yii2基于Ajax自动获取表单数据的方法
2016/08/10 PHP
PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析
2017/07/03 PHP
javascript入门·对象属性方法大总结
2007/10/01 Javascript
javascript之typeof、instanceof操作符使用探讨
2013/05/19 Javascript
js使下拉列表框可编辑不止是选择
2013/12/12 Javascript
Select标签下拉列表二级联动级联实例代码
2014/02/07 Javascript
Javascript学习笔记之函数篇(四):arguments 对象
2014/11/23 Javascript
gulp-htmlmin压缩html的gulp插件实例代码
2016/06/06 Javascript
详解JavaScript中this的指向问题
2017/01/20 Javascript
bootstrap table表格插件使用详解
2017/05/08 Javascript
js实现数组和对象的深浅拷贝
2017/09/30 Javascript
Vue多种方法实现表头和首列固定的示例代码
2018/02/02 Javascript
webpack中使用iconfont字体图标的方法
2018/02/22 Javascript
vue v-for循环重复数据无法添加问题解决方法【加track-by='索引'】
2019/03/15 Javascript
在vue中使用echars实现上浮与下钻效果
2019/11/08 Javascript
Django URL传递参数的方法总结
2016/08/28 Python
Django入门使用示例
2017/12/12 Python
django中模板的html自动转意方法
2018/05/27 Python
python模糊图片过滤的方法
2018/12/14 Python
谈谈python垃圾回收机制
2020/09/27 Python
django使用channels实现通信的示例
2020/10/19 Python
css3实现input输入框颜色渐变发光效果代码
2014/04/02 HTML / CSS
印尼值得信赖的在线交易网站:Bukalapak
2019/03/11 全球购物
公共汽车、火车和飞机票的通用在线预订和销售平台:INFOBUS
2019/11/30 全球购物
英智兴达软件测试笔试题
2016/10/12 面试题
采购文员岗位职责
2013/11/20 职场文书
圣诞节红领巾广播稿
2014/02/03 职场文书
应届大学生自荐书
2014/06/17 职场文书
员工团队活动方案
2014/08/28 职场文书
大学生职业生涯规划大赛作品(精品)
2014/09/17 职场文书
2014年十一国庆节爱国演讲稿
2014/09/23 职场文书
Spring中的使用@Async异步调用方法
2021/11/01 Java/Android