图像样本预处理
import cv2
import numpy as np
import os
import random
import matplotlib. pyplot as plt
from global_var import *
from math import *
data_root_path = "/home/tarena/MagneticTile/"
不切边旋转
def remote ( img, angle) :
h, w = img. shape[ : 2 ]
h_new = int ( w * fabs( sin( radians( angle) ) ) + h * fabs( cos( radians( angle) ) ) )
w_new = int ( h * fabs( sin( radians( angle) ) ) + w * fabs( cos( radians( angle) ) ) )
matRotation = cv2. getRotationMatrix2D( ( w / 2 , h / 2 ) , angle, 1 )
matRotation[ 0 , 2 ] += ( w_new - w) / 2
matRotation[ 1 , 2 ] += ( h_new - h) / 2
imgRotation = cv2. warpAffine( img, matRotation, ( w_new, h_new) , borderValue= ( 255 , 255 , 255 ) )
return imgRotation
def do_rotate ( im, angle, center= None , scale= 1.0 ) :
"""
图像旋转变换
:param im: 原始图像数据
:param angle: 旋转角度
:param center: 旋转中心,如果为None则以原图中心为旋转中心
:param scale: 缩放比例,默认为1
:return: 返回旋转后的图像
"""
h, w = im. shape[ : 2 ]
if center is None :
center = ( w / 2 , h / 2 )
M = cv2. getRotationMatrix2D( center, angle, scale)
rotated = cv2. warpAffine( im, M, ( w, h) )
return rotated
def rotate_all ( ) :
dirs = os. listdir( data_root_path)
for d in dirs:
dir_path = os. path. join( data_root_path, d)
if not os. path. isdir( dir_path) :
continue
sub_dir_path = os. path. join( dir_path, "Imgs" )
imgs = os. listdir( sub_dir_path)
for img_file in imgs:
img_full_path = os. path. join( sub_dir_path, img_file)
print ( img_full_path)
im = cv2. imread( img_full_path)
pos = img_file. find( "." )
name = img_file[ 0 : pos]
suffix = img_file[ pos: ]
for i in range ( 1 , 8 ) :
img_new = remote( im, 45 * i)
img_new_name = "%s_rotate_%d%s" % ( name, i, suffix)
cv2. imwrite( os. path. join( sub_dir_path, img_new_name) , img_new)
print ( "save ok:" , os. path. join( sub_dir_path, img_new_name) )
if __name__ == "__main__" :
rotate_all( )
print ( "图像预处理结束" )