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 随机数生成的代码的详细分析
May 15 Python
详解使用python的logging模块在stdout输出的两种方法
May 17 Python
PyQt5每天必学之滑块控件QSlider
Apr 20 Python
Python读取txt文件数据的方法(用于接口自动化参数化数据)
Jun 27 Python
对Django中static(静态)文件详解以及{% static %}标签的使用方法
Jul 28 Python
python实现身份证实名认证的方法实例
Nov 08 Python
Python多重继承之菱形继承的实例详解
Feb 12 Python
浅析python表达式4+0.5值的数据类型
Feb 26 Python
python自动打开浏览器下载zip并提取内容写入excel
Jan 04 Python
python压包的概念及实例详解
Feb 17 Python
如何用Python搭建gRPC服务
Jun 30 Python
Python自动操作神器PyAutoGUI的使用教程
Jun 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函数scandir排除特定目录
2014/06/12 PHP
php计算一个文件大小的方法
2015/03/30 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
2016/03/21 PHP
微信自定义菜单的创建/查询/取消php示例代码
2016/08/05 PHP
EarthLiveSharp中cloudinary的CDN图片缓存自动清理python脚本
2017/04/04 PHP
javascript奇异的arguments分析
2010/10/20 Javascript
javascript函数以及基础写法100多条实用整理
2013/01/13 Javascript
用js实现小球的自由移动代码
2013/04/22 Javascript
js代码实现随机颜色的小方块
2015/07/30 Javascript
实例代码详解jquery.slides.js
2015/11/16 Javascript
基于JavaScript创建动态Dom
2015/12/08 Javascript
jquery简单倒计时实现方法
2015/12/18 Javascript
jQuery实现的可编辑表格完整实例
2016/06/20 Javascript
JS访问DOM节点方法详解
2016/11/29 Javascript
Vue.js仿微信聊天窗口展示组件功能
2017/08/11 Javascript
es6系列教程_ Map详解以及常用api介绍
2017/09/25 Javascript
基于Fixed定位的框选功能的实现代码
2019/05/13 Javascript
基于vue实现圆形菜单栏组件
2019/07/05 Javascript
在Vue mounted方法中使用data变量详解
2019/11/05 Javascript
Vue+Node实现商品列表的分页、排序、筛选,添加购物车功能详解
2019/12/07 Javascript
[04:11]DOTA2亚洲邀请赛小组赛第一日 TOP10精彩集锦
2015/01/30 DOTA
win10下Python3.6安装、配置以及pip安装包教程
2017/10/01 Python
Django中Model的使用方法教程
2018/03/07 Python
78行Python代码实现现微信撤回消息功能
2018/07/26 Python
python实现名片管理系统项目
2019/04/26 Python
python使用wxpy实现微信消息防撤回脚本
2019/04/29 Python
python 实现兔子生兔子示例
2019/11/21 Python
django执行原始查询sql,并返回Dict字典例子
2020/04/01 Python
Python文件时间操作步骤代码详解
2020/04/13 Python
Python SMTP配置参数并发送邮件
2020/06/16 Python
HTML5中的postMessage API基本使用教程
2016/05/20 HTML / CSS
师范生自荐信
2013/10/27 职场文书
汽车维修专业毕业生的求职信分享
2013/12/04 职场文书
生产助理岗位职责
2014/06/18 职场文书
个人职业及收入证明
2014/10/13 职场文书
中学生综合素质自我评价
2015/03/06 职场文书