增广数据集-图像旋转+标签坐标旋转

import numpy as np
import os
import cv2
from math import *


def rotate_images(path, angle):
    image_dir = path
    pathDir = os.listdir(image_dir)
    for s in pathDir:
        newDir = os.path.join(image_dir, s)
        # print(newDir)
        if os.path.isfile(newDir):
            if os.path.splitext(newDir)[1] == ".jpg":
                # lenth = len(newDir)
                # print(lenth)
                filename = int(os.path.basename(newDir)[:-4]) + 270

                """
                imgs = cv2.imread(newDir)
                imgs = skimage.transform.rotate(imgs, angle)
                # now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
                plt.figure("rotated")
                plt.imshow(imgs)
                plt.axis("off")
                plt.savefig("D:/learn/lianxi/python_cnn/save/"+str((i+17)) + ".jpg")
                height, width = img.shape[:2]
                """
                img = cv2.imread(newDir)
                degree = angle
                # 旋转后的尺寸
                height, width = img.shape[:2]
                heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
                widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

                matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

                matRotation[0, 2] += (widthNew - width) / 2  # 重点在这步,目前不懂为什么加这步
                matRotation[1, 2] += (heightNew - height) / 2  # 重点在这步

                imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

                cv2.imwrite("D:/learn/lianxi/python_cnn/save2/"+str(filename) + ".jpg", imgRotation)


def rotate_txt(path, angle):
    image_dir = path
    print(path)
    pathDir = os.listdir(image_dir)
    print(pathDir)
    for s in pathDir:
        newDir = os.path.join(image_dir, s)
        print(newDir)
        if os.path.isfile(newDir):
            if os.path.splitext(newDir)[1] == ".txt":
                file = open(newDir, 'r+')
                filename = int(os.path.basename(newDir)[:-4]) + 270
                # print("+++++++++++++")
                print(filename)
                for line in file.readlines():
                    line = line.strip()  # 去掉每行头尾空白
                    line_split = line.split()
                    # print(line_split)

                    int_line = [float(x) for x in line_split]
                    a = np.array(int_line)
                    print(a)
                    # 图像的宽高col=a[3],row=a[4],某个像素是(x1=a[1],y1=a[2]),绕着(x2=0.5,y2=0.5)
                    x1 = a[1]
                    y1 = a[4] - a[2]
                    x2 = 0.5
                    y2 = a[4] - 0.5
                    x = (x1 - x2) * cos(angle) - (y1 - y2) * sin(angle) + x2
                    y = (x1 - x2) * sin(angle) + (y1 - y2) * cos(angle) + y2
                    x = x
                    y = a[4] - y
                    """
                    平面的
                    x_center = cos(angle)*(a[1]-0.5)-sin(angle)*(a[2]-0.5) + 0.5
                    y_center = cos(angle)*(a[2]-0.5)+sin(angle)*(a[1]-0.5) + 0.5
                    90度:需要改宽高,互换
                    x_w = a[4]
                    y_h = a[3]
                    """
                    x_w = a[3]
                    y_h = a[4]
                    # new = [line_split[0], x_center, y_center, x_w, y_h]
                    # new_line = [str(x) for x in new]
                    # new_file = open("17_1.txt", "a+")
                    # new_file.writelines(str(new_line)+'\n')
                    # new = [line_split[0], x_center, y_center, x_w, y_h]
                    new = [line_split[0], x, y, x_w, y_h]
                    new_line = [str(x) for x in new]
                    filename1 = str(filename)
                    new_file = open(filename1+'.txt', "a+")
                    lenth = len(new_line)
                    for i in range(0, lenth):
                        if i == 4:
                            new_file.writelines(str(new_line[i]) + '\n')
                        else:
                            new_file.writelines(str(new_line[i]) + " ")


if __name__ == "__main__":

    rotate_images("D:/learn/lianxi/python_cnn/imgs/", 180)
    # rotate_txt("D:/learn/lianxi/python_cnn/imgs/", radians(180))


   

参考博客:
https://blog.csdn.net/guyuealian/article/details/78288131

你可能感兴趣的:(yolo)