python opencv旋转图像(保持图像不被裁减)

yipeiwu_com6年前Python基础

本文实例为大家分享了python opencv旋转图像的具体代码,保持图像不被裁减,供大家参考,具体内容如下

# -*- coding:gb2312 -*-
import cv2
from math import *
import numpy as np

img = cv2.imread("3-2.jpg")

height,width=img.shape[:2]

degree=45
#旋转后的尺寸
heightNew=int(width*fabs(sin(radians(degree)))+height*fabs(cos(radians(degree))))
widthNew=int(height*fabs(sin(radians(degree)))+width*fabs(cos(radians(degree))))

matRotation=cv2.getRotationMatrix2D((width/2,height/2),degree,1)

matRotation[0,2] +=(widthNew-width)/2 #重点在这步,目前不懂为什么加这步
matRotation[1,2] +=(heightNew-height)/2 #重点在这步

imgRotation=cv2.warpAffine(img,matRotation,(widthNew,heightNew),borderValue=(255,255,255))

cv2.imshow("img",img)
cv2.imshow("imgRotation",imgRotation)
cv2.waitKey(0)

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python和C/C++交互的几种方法总结

Python和C/C++交互的几种方法总结

前言 python作为一门脚本语言,其好处是语法简单,很多东西都已经封装好了,直接拿过来用就行,所以实现同样一个功能,用Python写要比用C/C++代码量会少得多。但是优点也必然也伴随...

Python出现segfault错误解决方法

本文分析了Python出现segfault错误解决方法。分享给大家供大家参考,具体如下: 最近python程序在运行过程中偶尔会引发系统segfault的错误,而且是在不定期不同代码段时...

spyder常用快捷键(分享)

最近在学习tensorflow框架,在ubuntu下用到python的一个ide --spyder,以下是常用快捷键 Ctrl+1:注释/撤销注释 Ctrl+4/5:块注释/撤销块注释...

python字符串加密解密的三种方法分享(base64 win32com)

1. 最简单的方法是用base64: 复制代码 代码如下:import base64 s1 = base64.encodestring('hello world')s2 = base64...

关于pandas的离散化,面元划分详解

pd.cut pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_low...