python 图片批量数据增强

图像旋转

90、180、270度旋转

import scipy 
from scipy import misc 
import os 
import time 
import glob 
from scipy import ndimage 

def get_image_paths(folder): 
    return glob.glob(os.path.join(folder, '*.png')) 

def create_read_img(filename): 
    im = misc.imread(filename) 
    img_rote_90 = ndimage.rotate(im, 90)  
    scipy.misc.imsave(filename[:-4]+'_90.png',img_rote_90) 
    
    img_rote_180 = ndimage.rotate(im, 180) 
    scipy.misc.imsave(filename[:-4]+'_180.png',img_rote_180) 

    img_rote_270 = ndimage.rotate(im, 270) 
    scipy.misc.imsave(filename[:-4]+'_270.png',img_rote_270) 
    print(filename)
img_path = '/media/wxy/000F8E4B0002F751/test/' 
imgs = get_image_paths(img_path) 
#print (imgs) 

for i in imgs: 
    create_read_img(i)

镜像翻转
根据原始图像名称进行翻转

import cv2
import os

for name in os.listdir("./HR_image/"):
    if len(name)==23:
        image = cv2.imread("./HR_image/"+name)
        h_flip = cv2.flip(image, 1) #左右
        cv2.imwrite("./HR_image/"+name[:-4]+"_flip_h.png", h_flip)
        w_flip = cv2.flip(image, 0) #上下
        cv2.imwrite("./HR_image/"+name[:-4]+"_flip_w.png", w_flip)
        print(name)

同时增强

from PIL import Image
import os 
import glob 

def get_image_paths(folder): 
    return glob.glob(os.path.join(folder, '*.png')) 

def create_read_img(filename): 
    #读取图像
    im = Image.open(filename)

    out_h = im.transpose(Image.FLIP_LEFT_RIGHT)
    out_w = im.transpose(Image.FLIP_TOP_BOTTOM)
    out_90 = im.transpose(Image.ROTATE_90)
    out_180 = im.transpose(Image.ROTATE_180)
    out_270 = im.transpose(Image.ROTATE_270)
    
    out_h.save(filename[:-4]+'_h.png')
    out_w.save(filename[:-4]+'_w.png')
    out_90.save(filename[:-4]+'_90.png')
    out_180.save(filename[:-4]+'_180.png')
    out_270.save(filename[:-4]+'_270.png')
    print(filename)
    
img_path = '/media/wxy/000F8E4B0002F751/test/' 
imgs = get_image_paths(img_path) 

for i in imgs: 
    create_read_img(i)

多线程图像增强

import time
import threadpool
import os
from PIL import Image

name = ["/media/wxy/000F8E4B0002F751/test/"+name_ for name_ in os.listdir("./test")]

def create_read_img(filename):
    # 读取图像
    im = Image.open(filename)
    out_h = im.transpose(Image.FLIP_LEFT_RIGHT)
    out_w = im.transpose(Image.FLIP_TOP_BOTTOM)
    out_90 = im.transpose(Image.ROTATE_90)
    out_180 = im.transpose(Image.ROTATE_180)
    out_270 = im.transpose(Image.ROTATE_270)

    out_h.save(filename[:-4] + '_h.png')
    out_w.save(filename[:-4] + '_w.png')
    out_90.save(filename[:-4] + '_90.png')
    out_180.save(filename[:-4] + '_180.png')
    out_270.save(filename[:-4] + '_270.png')
    print(filename)

start_time = time.time()
pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(create_read_img, name)
[pool.putRequest(req) for req in requests]
pool.wait()
print ('%d second'% (time.time()-start_time))

随机旋转,裁剪,加噪:

import cv2 
import numpy as np 
import os.path 
import random 
import math 

def rotate(img,angle):
    height = img.shape[0] 
    width = img.shape[1] 
    if angle%180 == 0: 
        scale = 1 
    elif angle%90 == 0: 
        scale = float(max(height, width))/min(height, width) 
    else: 
        scale = math.sqrt(pow(height,2)+pow(width,2))/min(height, width) 

    rotateMat = cv2.getRotationMatrix2D((width/2, height/2), angle, scale) 
    rotateImg = cv2.warpAffine(img, rotateMat, (width, height)) 
    return rotateImg 

def tfactor(img): 
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 
    hsv[:,:,0] = hsv[:,:,0]*(0.75+ np.random.random()*0.5)
    hsv[:,:,1] = hsv[:,:,1]*(0.75+ np.random.random()*0.5)
    hsv[:,:,2] = hsv[:,:,2]*(0.75+ np.random.random()*0.5) 
    img = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    return img 

file_dir = "./data/" 
classes = {"LAKE"} 
i = 0 
for index,name in enumerate(classes): 
    class_path = file_dir + name + "/" 
    for img_name in os.listdir(class_path): 
        img_path = class_path + img_name 
        image = cv2.imread(img_path) 
        tfimg = tfactor(image) 
        rotateAngle = random.randrange(0,10) 
        rotateImg = rotate(tfimg,rotateAngle) 
        H,W,Channels = tfimg.shape 
        y = H//2+20 
        x = W//2+20 
        winW = random.randrange(80,x-20) 
        winH = random.randrange(80,y-20) 

        cropImg = rotateImg[int(y-winH):int(y + winH),int(x-winW):int(x + winW)] 

        cv2.imwrite('./data/LAKE/'+img_name[:-4]+'_cropRotatetf_{:04d}.jpg'.format(i),cropImg) 


你可能感兴趣的:(python)