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基础教程之正则表达式基本语法以及re模块
Mar 25 Python
浅谈python和C语言混编的几种方式(推荐)
Sep 27 Python
mac系统安装Python3初体验
Jan 02 Python
TensorFlow搭建神经网络最佳实践
Mar 09 Python
使用Python监控文件内容变化代码实例
Jun 04 Python
python 列表降维的实例讲解
Jun 28 Python
flask-restful使用总结
Dec 04 Python
Pyinstaller打包.py生成.exe的方法和报错总结
Apr 02 Python
python3用urllib抓取贴吧邮箱和QQ实例
Mar 10 Python
解决python运行效率不高的问题
Jul 20 Python
python pygame 愤怒的小鸟游戏示例代码
Feb 25 Python
Pytest实现setup和teardown的详细使用详解
Apr 17 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扩展函数
2006/10/09 PHP
Windows下PHP的任意文件执行漏洞
2006/10/09 PHP
PHP5 的对象赋值机制介绍
2011/08/02 PHP
PHP substr()函数参数解释及用法讲解
2017/11/23 PHP
PHP使用mysqli同时执行多条sql查询语句的实例
2019/03/22 PHP
phpstorm最新激活码分享亲测phpstorm2020.2.3版可用
2020/11/22 PHP
关于使用runtimeStyle属性问题讨论文章
2007/03/08 Javascript
ExtJS Ext.MessageBox.alert()弹出对话框详解
2010/04/02 Javascript
jQuery中add实现同时选择两个id对象
2010/10/22 Javascript
jQuery中.live()方法的用法深入解析
2013/12/30 Javascript
Javascript 完美运动框架(逐行分析代码,让你轻松了运动的原理)
2015/01/23 Javascript
同步异步动态引入js文件的几种方法总结
2016/09/23 Javascript
JavaScript计算值然后把值嵌入到html中的实现方法
2016/10/29 Javascript
JSP防止网页刷新重复提交数据的几种方法
2016/11/19 Javascript
js数组与字符串常用方法总结
2017/01/13 Javascript
微信小程序左滑删除效果的实现代码
2017/02/20 Javascript
[53:15]Mineski vs iG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python实现返回数组中第i小元素的方法示例
2017/12/04 Python
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
2018/01/11 Python
Django框架教程之正则表达式URL误区详解
2018/01/28 Python
python装饰器代替set get方法实例
2019/12/19 Python
pytorch实现Tensor变量之间的转换
2020/02/17 Python
Python日志syslog使用原理详解
2020/02/18 Python
使用Python和百度语音识别生成视频字幕的实现
2020/04/09 Python
Python从文件中读取数据的方法步骤
2020/11/18 Python
不服从上级领导安排的检讨书
2014/09/14 职场文书
2014领导班子四风剖析对照检查材料思想汇报
2014/09/20 职场文书
综治维稳工作汇报
2014/10/27 职场文书
个人年终总结怎么写
2015/03/09 职场文书
2015年财政局工作总结
2015/05/21 职场文书
走进毛泽东观后感
2015/06/04 职场文书
python如何正确使用yield
2021/05/21 Python
Mysql数据库命令大全
2021/05/26 MySQL
go web 预防跨站脚本的实现方式
2021/06/11 Golang
CentOS MySql8 远程连接实战
2022/04/19 MySQL
Python如何加载模型并查看网络
2022/07/15 Python