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

yipeiwu_com6年前Python基础

最近在做深度学习时需要用到图像处理相关的操作,在度娘上找到的图片旋转方法千篇一律,旋转完成的图片都不是原始大小,很苦恼,于是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设计】。

相关文章

selenium+python 对输入框的输入处理方法

最近自己在做项目的自动化测试(公司无此要求),在用户管理模块做修改用户信息时,脚本已经跑成功,并且的确做了update操作,但是自己登陆页面检查,信息却没有被修改,再次确定系统该模块的编...

Flask Web开发入门之文件上传(八)

Flask Web开发入门之文件上传(八)

本章我们介绍Flask Web开发中涉及的文件上传模块 定义后台接收处理逻辑 # http://flask.pocoo.org/docs/0.12/patterns/fileuplo...

Python检查ping终端的方法

菜鸟一枚,写着试了试,虽说有点杂乱,但还是能用,我是在linux下运行的 大致说下过程: 1、把需要ping的网段中所有ip存到数组中(我是放到数组中了,其实直接for循环,一个个的也行...

pandas修改DataFrame列名的方法

在做数据挖掘的时候,想改一个DataFrame的column名称,所以就查了一下,总结如下: 数据如下: >>>import pandas as pd >&g...

python笔记(1) 关于我们应不应该继续学习python

以前面试的时候会被问到,linux熟不熟呀?对于这种问题:我总会尴尬地回答,“额..了解一点”。   然而,我大学毕业的时候,连linux的虚拟机都没装过,更别提系统熟不熟悉了。虽然我了...