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 opencv将图片转为灰度图的方法示例

python opencv将图片转为灰度图的方法示例

使用opencv将图片转为灰度图主要有两种方法,第一种是将彩色图转为灰度图,第二种是在使用OpenCV读取图片的时候直接读取为灰度图。 将彩色图转为灰度图 import cv2 im...

浅谈Django自定义模板标签template_tags的用处

浅谈Django自定义模板标签template_tags的用处

自定义模板标签,过滤器。英文翻译是Customtemplatetagsandfilters。customfilter自定义过滤器今天不在我的记录范围之内,以后用到再看官方文档也不迟。 *...

Python中实现结构相似的函数调用方法

python的dict用起来很方便,可以自定义key值,并通过下标访问,示例如下: 复制代码 代码如下: >>> d = {'key1':'value1', ... '...

python实现远程通过网络邮件控制计算机重启或关机

python实现远程通过网络邮件控制计算机重启或关机

需求分析: python实现一个远程通过网络邮件控制计算机重启或关机的项目。 程序流程: 过程分析: 要实现本功能,首先要直到在本地如何通过Python控制计算机的重启和关机,然...

Python定义二叉树及4种遍历方法实例详解

Python定义二叉树及4种遍历方法实例详解

本文实例讲述了Python定义二叉树及4种遍历方法。分享给大家供大家参考,具体如下: Python & BinaryTree 1. BinaryTree (二叉树) 二叉树是有限个元素的...