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使用Beautiful Soup包编写爬虫时的一些关键点
Jan 20 Python
Python实现全角半角字符互转的方法
Nov 28 Python
基于Python3.6+splinter实现自动抢火车票
Sep 25 Python
详解Python读取yaml文件多层菜单
Mar 23 Python
python实现WebSocket服务端过程解析
Oct 18 Python
Python 3.6打包成EXE可执行程序的实现
Oct 18 Python
Django 实现Admin自动填充当前用户的示例代码
Nov 18 Python
python selenium自动化测试框架搭建的方法步骤
Jun 14 Python
基于python获取本地时间并转换时间戳和日期格式
Oct 27 Python
利用python查看数组中的所有元素是否相同
Jan 08 Python
上帝为你开了一扇窗之Tkinter常用函数详解
Jun 02 Python
详解Python requests模块
Jun 21 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
收集的PHP中与数组相关的函数
2007/03/22 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
学习php中的正则表达式
2014/08/17 PHP
Codeigniter通过SimpleXML将xml转换成对象的方法
2015/03/19 PHP
php反射类ReflectionClass用法分析
2016/05/12 PHP
用nodejs写的一个简单项目打包工具
2013/05/11 NodeJs
javascript屏蔽右键代码
2014/05/15 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
js实现数组冒泡排序、快速排序原理
2016/03/08 Javascript
Node连接mysql数据库方法介绍
2017/02/07 Javascript
gulp加批处理(.bat)实现ng多应用一键自动化构建
2017/02/16 Javascript
JavaScript中正则表达式判断匹配规则及常用方法
2017/08/03 Javascript
Angularjs中的$apply及优化使用详解
2018/07/02 Javascript
VUE+elementui组件在table-cell单元格中绘制微型echarts图
2020/04/20 Javascript
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
windows下python模拟鼠标点击和键盘输示例
2014/02/28 Python
python登录并爬取淘宝信息代码示例
2017/12/09 Python
如何使用VSCode愉快的写Python于调试配置步骤
2018/04/06 Python
深入理解Python异常处理的哲学
2019/02/01 Python
Python可迭代对象操作示例
2019/05/07 Python
python 函数嵌套及多函数共同运行知识点讲解
2020/03/03 Python
基于Python爬取京东双十一商品价格曲线
2020/10/23 Python
termux中matplotlib无法显示中文问题的解决方法
2021/01/11 Python
allbeauty美国:英国在线美容店
2019/03/11 全球购物
可爱的童装和鞋子:Fabkids
2019/08/16 全球购物
Java平台和其他软件平台有什么不同
2015/06/05 面试题
奥巴马连任演讲稿
2014/05/15 职场文书
大学生就业求职信
2014/06/12 职场文书
三问三解心得体会
2014/09/05 职场文书
2015年全国爱眼日活动方案
2015/05/05 职场文书
离婚案件原告代理词
2015/05/23 职场文书
大学学习委员竞选稿
2015/11/20 职场文书
教师正风肃纪心得体会
2016/01/15 职场文书
职场:企业印章管理制度(模板)
2019/10/18 职场文书
python基于opencv批量生成验证码的示例
2021/04/28 Python
Golang表示枚举类型的详细讲解
2021/09/04 Golang