python opencv旋转图片的使用方法


Posted in Python onJune 04, 2021

背景

在图像处理中,有的时候会有对图片进行角度旋转的处理,尤其是在计算机视觉中对于图像扩充,旋转角度扩充图片是一种常见的处理。这种旋转图片的应用场景也比较多,比如用户上传图片是竖着的时候,不好进行处理,也需要对其进行旋转,以便后续算法处理。常见的旋转处理有两种方式,一种是转化为numpy矩阵后,对numpy矩阵进行处理,另外一种是使用opencv自带的函数进行各种变换处理,以实现旋转角度的结果。

原始图像:

python opencv旋转图片的使用方法

opencv函数

旋转中常用的函数有以下几个函数

cv2.transpose: 对图像矩阵进行转置处理

img = cv2.imread(origin_img_path)
img_transpose = cv2.transpose(img)
cv2.imshow('transpose', img_transpose)
cv2.waitKey(0)

python opencv旋转图片的使用方法

cv2.flip : 对图像矩阵进行翻转处理,参数可以设置为1,0,-1,分别对应着水平翻转、垂直翻转、水平垂直翻转。

img = cv2.imread(origin_img_path)
img_flip = cv2.flip(img, 1)
cv2.imshow('flip', img_flip)
cv2.waitKey(0)

python opencv旋转图片的使用方法

cv2.getRotationMatrix2D: 构建旋转矩阵M,后续旋转时候只需要与旋转矩阵进行乘积即可完成旋转操作

旋转矩阵M

python opencv旋转图片的使用方法

img = cv2.imread(origin_img_path)
rows, cols = img.shape
# 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# 可以通过设置旋转中心,缩放因子以及窗口大小来防止旋转后超出边界的问题
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)

cv2.warpAffine: 对图像进行仿射变换,一般进行平移或者旋转操作

img = cv2.imread(origin_img_path)
cv2.warpAffine(img, M,(lengh,lengh),borderValue=(255,255,255))  # M为上面的旋转矩阵

numpy函数

numpy实现旋转一般是使用numpy.rot90对图像进行90度倍数的旋转操作

官方介绍:

numpy.rot90(m, k=1, axes=(0, 1))[source]

Rotate an array by 90 degrees in the plane specified by axes.

Rotation direction is from the first towards the second axis.

k: Number of times the array is rotated by 90 degrees.

关键参数k表示旋转90度的倍数,k的取值一般为1、2、3,分别表示旋转90度、180度、270度;k也可以取负数,-1、-2、-3。k取正数表示逆时针旋转,取负数表示顺时针旋转。

旋转90度

逆时针

  • 使用opencv函数的转置操作+翻转操作实现旋转
  • 使用numpy.rot90实现
def rotateAntiClockWise90(img_file):  # 逆时针旋转90度
	img = cv2.imread(img_file)
    trans_img = cv2.transpose(img)
    img90 = cv2.flip(trans_img, 0)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90
    
def totateAntiClockWise90ByNumpy(img_file):  # np.rot90(img, -1) 逆时针旋转90度
    img = cv2.imread(img_file)
    img90 = np.rot90(img, -1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

python opencv旋转图片的使用方法

顺时针

def rotateClockWise90(self, img_file):
	img = cv2.imread(img_file)
    trans_img = cv2.transpose( img )
    img90 = cv2.flip(trans_img, 1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

def totateClockWise90ByNumpy(img_file):  # np.rot90(img, 1) 顺时针旋转90度
    img = cv2.imread(img_file)
    img90 = np.rot90(img, 1)
    cv2.imshow("rotate", img90)
    cv2.waitKey(0)
    return img90

python opencv旋转图片的使用方法

旋转180度、270度

使用numpy.rot90实现旋转180度、270度

180度

img180 = np.rot90(img, 2)
cv2.imshow("rotate", img180)
cv2.waitKey(0)

python opencv旋转图片的使用方法

270 度

img270 = np.rot90(img, 3)
cv2.imshow("rotate", img270)
cv2.waitKey(0)

python opencv旋转图片的使用方法

旋转任意角度,以任意色值填充背景

import cv2
from math import *
import numpy as np
 
# 旋转angle角度,缺失背景白色(255, 255, 255)填充
def rotate_bound_white_bg(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)
    # -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
    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
    # borderValue 缺失背景填充色彩,此处为白色,可自定义
    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))
    # borderValue 缺省,默认是黑色(0, 0 , 0)
    # return cv2.warpAffine(image, M, (nW, nH))
 
img = cv2.imread("dog.png")
imgRotation = rotate_bound_white_bg(img, 45)
 
cv2.imshow("img",img)
cv2.imshow("imgRotation",imgRotation)
cv2.waitKey(0)

45度

python opencv旋转图片的使用方法

60度

python opencv旋转图片的使用方法

参考

cv2.getRotationMatrix2D博客介绍

cv2.warpAffine 博客介绍

numpy.rot90

旋转任意角度

到此这篇关于python opencv旋转图片的使用方法的文章就介绍到这了,更多相关python opencv旋转图片内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中使用ElementTree解析XML示例
Jun 02 Python
Python 中的 else详解
Apr 23 Python
安装Python的教程-Windows
Jul 22 Python
JS设计模式之责任链模式实例详解
Feb 03 Python
简单实现python数独游戏
Mar 30 Python
python 列表删除所有指定元素的方法
Apr 19 Python
python调用百度语音识别实现大音频文件语音识别功能
Aug 30 Python
一百行python代码将图片转成字符画
Feb 19 Python
python实现推箱子游戏
Mar 25 Python
Django Form and ModelForm的区别与使用
Dec 06 Python
Python tkinter 下拉日历控件代码
Mar 04 Python
python os.rename实例用法详解
Dec 06 Python
Python还能这么玩之用Python修改了班花的开机密码
Anaconda安装pytorch及配置PyCharm 2021环境
python如何利用cv2模块读取显示保存图片
Jun 04 #Python
Python实现socket库网络通信套接字
Jun 04 #Python
python cv2图像质量压缩的算法示例
Jun 04 #Python
高考要来啦!用Python爬取历年高考数据并分析
单身狗福利?Python爬取某婚恋网征婚数据
You might like
非常不错的MySQL优化的8条经验
2008/03/24 PHP
ECSHOP在PHP5.5及高版本上报错的解决方法
2015/08/31 PHP
使用PHP+AJAX让WordPress动态加载文章的教程
2015/12/11 PHP
使用jQuery的ajax功能实现的RSS Reader 代码
2009/09/03 Javascript
javascript的onchange事件与jQuery的change()方法比较
2009/09/28 Javascript
js 遍历对象的属性的代码
2011/12/29 Javascript
javascript自动改变文字大小和颜色的效果的小例子
2013/08/02 Javascript
angularjs学习笔记之双向数据绑定
2015/09/26 Javascript
AngularJS中的API(接口)简单实现
2016/07/28 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
Vue服务端渲染和Vue浏览器端渲染的性能对比(实例PK )
2017/03/31 Javascript
详解Angular2 关于*ngFor 嵌套循环
2017/05/22 Javascript
jQuery EasyUI开发技巧总结
2017/09/26 jQuery
在ES5与ES6环境下处理函数默认参数的实现方法
2018/05/13 Javascript
利用Promise自定义一个GET请求的函数示例代码
2019/03/20 Javascript
小程序实现短信登录倒计时
2019/07/12 Javascript
微信小程序之侧边栏滑动实现过程解析(附完整源码)
2019/08/23 Javascript
js实现固定区域内的不重叠随机圆
2019/10/24 Javascript
云服务器部署Node.js项目的方法步骤(小白系列)
2020/03/23 Javascript
javascript实现拼图游戏
2021/01/29 Javascript
使用Python中的cookielib模拟登录网站
2015/04/09 Python
python中lambda与def用法对比实例分析
2015/04/30 Python
Python实现的对本地host127.0.0.1主机进行扫描端口功能示例
2019/02/15 Python
python实现LBP方法提取图像纹理特征实现分类的步骤
2019/07/11 Python
Django1.11自带分页器paginator的使用方法
2019/10/31 Python
python实现交并比IOU教程
2020/04/16 Python
python判断一个变量是否已经设置的方法
2020/08/13 Python
python Matplotlib数据可视化(1):简单入门
2020/09/30 Python
美国椅子和沙发制造商:La-Z-Boy
2020/10/25 全球购物
新员工欢迎词
2014/01/12 职场文书
给老师的检讨书
2014/02/11 职场文书
少先队学雷锋活动月总结
2014/03/09 职场文书
2015元旦主持词开场白和结束语
2014/12/14 职场文书
英语教师个人总结
2015/02/09 职场文书
教师节大会主持词
2015/07/06 职场文书
微信小程序中wxs文件的一些妙用分享
2022/02/18 Javascript