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设计】。

相关文章

python 将数据保存为excel的xls格式(实例讲解)

python提供一个库 xlwt ,可以将一些数据 写入excel表格中,十分的方便。贴使用事例如下。 #引入xlwt模块(提前pip下载好) import xlwt #使用wor...

Python制作词云图代码实例

Python制作词云图代码实例

词云图是将词汇按照频率的高低显示不同大小而形成的图,可以一目了然地看出关键词。下面是词云图的python代码~ #导入需要模块 import jieba import numpy a...

python 的列表遍历删除实现代码

python的列表list可以用for循环进行遍历,实际开发中发现一个问题,就是遍历的时候删除会出错,例如 l = [1,2,3,4] for i in l: if i != 4:...

Python解惑之整数比较详解

前言 在 Python 中一切都是对象,毫无例外整数也是对象,对象之间比较是否相等可以用==,也可以用is。 ==和is操作的区别是: is比较的是两个对象的id值是否相等,也就是...

基于Python_脚本CGI、特点、应用、开发环境(详解)

基于Python_脚本CGI、特点、应用、开发环境(详解)

CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提...