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] / w
    labels0[:, 2::2] = labels0[:, 2::2] / h
    image = image0.copy()
    show_image(image, labels0, h, w, 'raw img')
    
    #fliplr
    image = image0.copy()
    labels = labels0.copy()
    image = np.fliplr(image)
    labels[:, 1] = 1 - labels[:, 1]
    show_image(image, labels, h, w, 'fliplr')
    
    #flipud
    image = image0.copy()
    labels = labels0.copy()
    image = np.flipud(image)
    labels[:, 2] = 1 - labels[:, 2]
    show_image(image, labels, h, w, 'flipud')
    
    
    #rot90
    image = 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')
    
    #rot180
    image = 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')
    
    #rot270
    image = 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度后的结果,含图片和标签。

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding的叶子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值