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解析JSON数据的基本方法
Oct 15 Python
Python使用tkinter库实现文本显示用户输入功能示例
May 30 Python
详解Python数据可视化编程 - 词云生成并保存(jieba+WordCloud)
Mar 26 Python
基于梯度爆炸的解决方法:clip gradient
Feb 04 Python
Python爬虫实现vip电影下载的示例代码
Apr 20 Python
基于django 的orm中非主键自增的实现方式
May 18 Python
Python如何转换字符串大小写
Jun 04 Python
Python+OpenCV图像处理——实现轮廓发现
Oct 23 Python
python中绕过反爬虫的方法总结
Nov 25 Python
python Polars库的使用简介
Apr 21 Python
解决Pytorch修改预训练模型时遇到key不匹配的情况
Jun 05 Python
Python selenium绕过webdriver监测执行javascript
Apr 12 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下图片文字混合水印与缩略图实现代码
2009/12/11 PHP
ThinkPHP实现将本地文件打包成zip下载
2014/06/26 PHP
在线编辑器的实现原理(兼容IE和FireFox)
2007/03/09 Javascript
Javascript常考语句107条收集
2010/03/09 Javascript
基于jquery ajax 用户无刷新登录方法详解
2012/04/28 Javascript
Javascript实现返回上一页面并刷新的小例子
2013/12/11 Javascript
jQuery实现手机号码输入提示功能实例
2015/04/30 Javascript
深入解析JavaScript的闭包机制
2015/10/20 Javascript
jQuery实现Tab选项卡切换效果简单演示
2015/11/23 Javascript
javascript数组拍平方法总结
2018/01/20 Javascript
JavaScript作用域、闭包、对象与原型链概念及用法实例总结
2018/08/20 Javascript
使用Vue中 v-for循环列表控制按钮隐藏显示功能
2019/04/23 Javascript
Ant Design Vue 添加区分中英文的长度校验功能
2020/01/21 Javascript
解决vue里a标签值解析变量,跳转页面,前面加默认域名端口的问题
2020/07/22 Javascript
[50:24]VGJ.S vs Pain 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
一个计算身份证号码校验位的Python小程序
2014/08/15 Python
启动Atom并运行python文件的步骤
2018/11/09 Python
Python实现常见的回文字符串算法
2018/11/14 Python
Python小白必备的8个最常用的内置函数(推荐)
2019/04/03 Python
pyqt5数据库使用详细教程(打包解决方案)
2020/03/25 Python
Python限制内存和CPU使用量的方法(Unix系统适用)
2020/08/04 Python
新秀丽拉杆箱美国官方网站:Samsonite美国
2016/07/25 全球购物
汤米巴哈马官方网站:Tommy Bahama
2017/05/13 全球购物
Wedgwood英国官方网站:英式精致骨瓷餐具、礼品与生活精品,源于1759年
2019/09/02 全球购物
什么是组件架构
2016/05/15 面试题
家居设计专业个人自荐信范文
2013/11/26 职场文书
个人自荐信
2013/12/05 职场文书
品质标语大全
2014/06/21 职场文书
市场营销工作计划书
2014/09/15 职场文书
党的群众路线教育实践活动对照检查材料(教师)
2014/09/24 职场文书
企业法人代表证明书
2014/09/27 职场文书
大学生党员个人剖析材料
2014/10/08 职场文书
2014年学生会工作总结范文
2014/11/07 职场文书
2015年保洁工作总结范文
2015/04/28 职场文书
Python max函数中key的用法及原理解析
2021/06/26 Python
世界各国短波电台对东亚播送时间频率表(SW)
2021/06/28 无线电