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度后的结果,含图片和标签。

猜你喜欢

转载自blog.csdn.net/suiyingy/article/details/126628154