用于分割的训练数据和mask同时数据增强

愁死了,想同时对img和mask做一个上下左右旋转,竟然找不到一份能用的代码。我提供一份算了。 

'''
@Author: haoMax
@Github: https://github.com/liuzehao
@Blog: https://blog.csdn.net/liu506039293
@Date: 2019-10-10 11:15:18
@LastEditTime: 2019-11-15 16:44:15
@LastEditors: haoMax
@Description: 
'''
# -*- coding: utf-8 -*-
 
import cv2
from math import *
import numpy as np
import os
# 旋转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 缺省,默认是黑色(0, 0 , 0)
    # return cv2.warpAffine(image, M, (nW, nH))
img_path='./images/training'
label_path='./annotations/training'
rotate=[90,180,270]
rotate_name=["1000","2000","3000"]
imglist=os.listdir(img_path)
labellist=os.listdir(label_path)
for i in range(len(imglist)):
    imgpath=os.path.join(img_path,imglist[i])
    labelpath=os.path.join(label_path,labellist[i])
 
    (imgfilename,_) = os.path.splitext(imglist[i])
    (labelfilename,_) = os.path.splitext(labellist[i])
    # print(filename)
    img=cv2.imread(imgpath)
    label=cv2.imread(labelpath,0)
    for t in range(len(rotate)): 
        imgRotation = rotate_bound_white_bg(img, rotate[t])
        labelRotation= rotate_bound_white_bg(label, rotate[t])
        
        rename_img=imgfilename+rotate_name[t]+'.jpg'
        rename_label=labelfilename+rotate_name[t]+'.png'
        
        rename_path_img=os.path.join(img_path,rename_img)
        rename_path_label=os.path.join(label_path,rename_label)
 
        print(rename_path_img)
        print(rename_path_label)
        cv2.imwrite(rename_path_img,imgRotation)
        cv2.imwrite(rename_path_label,labelRotation)
# img = cv2.imread("./logs/bill_214.png")
# imgRotation = rotate_bound_white_bg(img, 90)
# cv2.imwrite("./logs/bill_212_rotatec.jpg",imgRotation) 
# cv2.imshow("img",img)
# cv2.imshow("imgRotation",imgRotation)
# cv2.waitKey(0)

之后重命名:

'''
@Author: haoMax
@Github: https://github.com/liuzehao
@Blog: https://blog.csdn.net/liu506039293
@Date: 2019-11-15 15:30:01
@LastEditTime: 2019-11-15 17:01:30
@LastEditors: haoMax
@Description: 
'''
import os
def func(i):
    t=i.find('-')
    return (i[t+1:-4])
def get_all_files(bg_path):
    files = []

    for f in os.listdir(bg_path):
        if os.path.isfile(os.path.join(bg_path, f)):
            files.append(os.path.join(bg_path, f))
        else:
            files.extend(get_all_files(os.path.join(bg_path, f)))
    files.sort(key=func)#排序从小到大
    return files
files_path="images2/training"
file_type=".jpg"
file_flag=1
files=get_all_files(files_path)
for i in files:
    # print(i)
    # src=os.path.join(files_path,i)
    file_path=os.path.join(files_path,"train-"+str('%04d'%file_flag)+file_type)
    print(file_path)
    os.rename(i,file_path)
    file_flag=file_flag+1

 

你可能感兴趣的:(AR深度学习项目)