python图像增强之随机翻转或随机旋转
假设输入的图像数据为img,标签label组成为classid、x、y、w、h,因此labels维度为Nx5。假设旋转前的坐标和尺寸为x0、y0、w0、h0。x、y、w、h均为归一化后坐标,即已经除以图像自身的宽高。以下操作需要用到numpy矩阵操作和random随机操作。引入随机的概念主要是为了便于进行随机增强,即随机翻转或随机旋转。
1、随机翻转
random.random()会随机产生0~1之间的数,np.fliplr()表示左右水平翻转,left-right。水平翻转对于标签主要影响的是x坐标,即x=1-x0,labels[:, 1] = 1 - labels[:, 1]。np.flipud()表示上下垂直翻转,up-down。垂直翻转对于标签主要影响的是y坐标,即y=1-y0,labels[:, 2] = 1 - labels[:, 2]。
if random.random() < hyp['fliplr']:
img = np.fliplr(img)
labels[:, 1] = 1 - labels[:, 1]
if random.random() < hyp['flipud']:
img = np.flipud(img)
labels[:, 2] = 1 - labels[:, 2]
2、随机旋转
2.1、旋转90度
旋转90度是以图像左上角为旋转中心,逆时针旋转。np.rot90()表示旋转90度。旋转后x=y0、y=1-x0、w=h0、h=w0。
img = np.rot90(img)
labels = labels[:, [0, 2, 1, 4, 3]]
labels[:, 2] = 1 - labels[:, 2]
2.2、旋转180度
旋转180度是以图像左上角为旋转中心,逆时针旋转两次,每次分别旋转90度。旋转后x=1-x0、y=1-y0、w=w0、h=h0。labels[:, 1:3] = 1 - labels[:, 1:3]
img = np.rot90(img)
img = np.rot90(img)
labels[:, 1:3] = 1 - labels[:, 1:3]
2.3、旋转270度
旋转270度是以图像左上角为旋转中心,逆时针旋转三次,每次分别旋转90度。旋转后x=1-y0、y=x0、w=h0、h=w0。
img = np.rot90(img)
img = np.rot90(img)
img = np.rot90(img)
labels = labels[:, [0, 2, 1, 4, 3]]
labels[:, 1] = 1 - labels[:, 1]
3、测试程序与结果
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 31 22:24:52 2022@author: Administrator
"""import cv2
import numpy as np
def show_image(image, labels, h, w, winname):image = image.copy().astype(np.uint8)for l in labels:x1 = int((l[1]-l[3]/2) * w)y1 = int((l[2]-l[4]/2) * h)x2 = int((l[1]+l[3]/2) * w)y2 = int((l[2]+l[4]/2) * h)# print(image.shape, (x1, y1), (x2, y2))cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 3)cv2.imshow(winname, image)if __name__ == '__main__':image0 = cv2.imread('lena.jpg')h, w = image0.shape[:2]labels0 = np.array([[0, 50., 50, 20, 20], [0, 150, 150, 30, 30]])labels0[:, 1::2] = labels0[:, 1::2] / wlabels0[:, 2::2] = labels0[:, 2::2] / himage = image0.copy()show_image(image, labels0, h, w, 'raw img')#fliplrimage = image0.copy()labels = labels0.copy()image = np.fliplr(image)labels[:, 1] = 1 - labels[:, 1]show_image(image, labels, h, w, 'fliplr')#flipudimage = image0.copy()labels = labels0.copy()image = np.flipud(image)labels[:, 2] = 1 - labels[:, 2]show_image(image, labels, h, w, 'flipud')#rot90image = image0.copy()labels = labels0.copy()image = np.rot90(image)labels = labels[:, [0, 2, 1, 4, 3]]labels[:, 2] = 1 - labels[:, 2]show_image(image, labels, h, w, 'rot90')#rot180image = image0.copy()labels = labels0.copy()image = np.rot90(image)image = np.rot90(image)labels[:, 1:3] = 1 - labels[:, 1:3]show_image(image, labels, h, w, 'rot180')#rot270image = image0.copy()labels = labels0.copy()image = np.rot90(image)image = np.rot90(image)image = np.rot90(image)labels = labels[:, [0, 2, 1, 4, 3]]labels[:, 1] = 1 - labels[:, 1]show_image(image, labels, h, w, 'rot270')cv2.waitKey(0)
下图分别是原图、左右水平翻转、上下垂直翻转、逆时针旋转90度、逆时针旋转180度、逆时针旋转270度后的结果,含图片和标签。
python图像增强之随机翻转或随机旋转相关推荐
- Python,OpenCV鼠标事件进行矩形、圆形的绘制(随机颜色、随机半径)
Python,OpenCV鼠标事件进行矩形.圆形的绘制(随机颜色.随机半径) 1. 效果图 2. 源码 参考 这篇博客将介绍鼠标事件,并介绍鼠标事件矩形.圆形的绘制: 所有的鼠标事件(左键按下.左键释 ...
- html文字自动上翻,jQuery超酷文字随机翻转变换动画特效
Flapper是一款效果非常酷的文字随机翻转变换动画jQuery特效.它的效果类似于机场或车站显示航班或车次的文字变换效果,每个文字都随机变换几次,然后才显示某个设定好的文字. 该文字特效可以显示文字 ...
- 【机器学习基础】数学推导+纯Python实现机器学习算法26:随机森林
Python机器学习算法实现 Author:louwill Machine Learning Lab 自从第14篇文章结束,所有的单模型基本就讲完了.而后我们进入了集成学习的系列,整整花了5篇文章的篇 ...
- 519. 随机翻转矩阵
519. 随机翻转矩阵 给你一个 m x n 的二元矩阵 matrix ,且所有值被初始化为 0 .请你设计一个算法,随机选取一个满足 matrix[i][j] == 0 的下标 (i, j) ,并将 ...
- Unity开发《一起来捉妖》教程 | 3.随机妖怪位置及旋转提示
洪流学堂,让你快人几步.你好,我是郑洪智. 洪流学堂公众号回复捉妖,可以获取本教程的源码工程. 小新:"智哥,我找了个小精灵的模型,嘿嘿,不过我经常运行的时候找不到模型在哪,怎么办?&quo ...
- java水平翻转矩阵_Java实现 LeetCode 519 随机翻转矩阵
519. 随机翻转矩阵 题中给出一个 n 行 n 列的二维矩阵 (n_rows,n_cols),且所有值被初始化为 0.要求编写一个 flip 函数,均匀随机的将矩阵中的 0 变为 1,并返回该值的位 ...
- python权重是什么意思_在python带权重的列表中随机取值的方法
1 random.choice python random模块的choice方法随机选择某个元素 foo = ['a', 'b', 'c', 'd', 'e'] from random import ...
- 对图像进行随机翻转和裁剪
opencv: cv2.flip 进行图像翻转,数据增强 cv2.flip(image, axis) axis Anno 1 水平翻转 0 垂直翻转 -1 水平垂直翻转 import numpy as ...
- python中生成随机整数,随机小数,0-1之间的小数
python中生成随机整数,随机小数,0-1之间的小数 1.生成随机整数 import randomprint(random.randint(1,100))#生成1到100之间的随机整数 2.生成0- ...
最新文章
- mysql带IN关键字的查询
- docker容器管理 php,基于Docker的PHP开发环境
- netty系列之:使用netty搭建websocket服务器
- 使用脚本动态操作 SVG 文档
- VMAXe资源配置只用4分钟
- 装修时水电如何开槽?沟槽如何封堵?有哪些防止沟槽开裂的方法
- C++学习之路: 智能指针入门
- 漏洞检测方法如何选?详解源代码与二进制SCA检测原理
- Routing a Marathon Race
- @ font-face 引入本地字体文件
- 前大厂员工谈中美企业区别,中企不用单元测试,仅靠QA检查代码?
- hadoop集群的搭建与配置(2)
- Qt QDialog简介
- 老码农的2019这一年——
- android随机抽奖代码_Android自己定义效果——随机抽奖
- 自定义view绘制太极图案
- Linux查看及测试网络
- 秋招知识点总结-Redis数据库
- 【矩阵计算】QR分解-基于Householder变换
- 全程无坑手撸k8s集群