旋转:
def get_image_rotation(image): #通用写法,即使传入的是三通道图片依然不会出错 height, width = image.shape[:2] center = (width // 2, height // 2) rotation = random.randint(-20,20) #得到旋转矩阵,第一个参数为旋转中心,第二个参数为旋转角度,第三个参数为旋转之前原图像缩放比例 M = cv2.getRotationMatrix2D(center, -rotation, 0.7) #进行仿射变换,第一个参数图像,第二个参数是旋转矩阵,第三个参数是变换之后的图像大小 image_rotation = cv2.warpAffine(image, M, (width, height)) return image_rotation
缩放:
#得到缩放后的图片 def get_image_scale(image): height, width = image.shape[:2] width_random = random.randint(-3, 3) width_scale = 1.0 + width_random / 10.0 height_random=random.randint(1,6) height_scale=1.0+height_random/10.0 #print("width_scale:%f,height_scale:%f" %(width_scale,height_scale)) image_resize = cv2.resize(image, (int(width_scale * width), int(height)), interpolation=cv2.INTER_CUBIC) height_resize,width_resize=image_resize.shape #print('image_binary height: %d width :%d' %(height,width)) #print('image_scale height: %d %d' %(height_resize,width_resize)) return image_resize