图像数据增强-图像补边
一、背景
在进行目标检测时,当我们的数据集中图像的长宽比不一致时,如果我们这时候将所有图片都缩放到相同的固定尺寸,这将会使得检测目标的形状的不确定性变大。如果此时将resize后的图片输入到目标检测网络中,将增大了目标检测的难度。
二、对图像进行补边操作
1 import cv2 2 import numpy as np 3 #pading image 4 whration=3 #定义缩放后图像的长宽比 5 6 def padding(image,anno){ 7 ori_height,ori_width=image.shape[:2] 8 blank_image = np.zeros((ori_height,ori_width,3),image.dtype) 9 if ori_width/ori_height < whration: 10 padimg_height=ori_height 11 padimg_width = padimg_height * whration 12 blank_image = np.zeros((padimg_height,padimg_width,3),image.dtype) 13 blank_image[:,(padimg_width-ori_width)//2 : ori_width + (padimg_width-ori_width)//2,:]= image 14 anno[:,1]=(anno[:,1] * ori_width +(padimg_width-ori_width)//2 ) // padimg_width 15 anno[:,3]=anno[:,3]*ori_width//padimg_width 16 else : 17 padimg_height = padimg_width / whration 18 padimg_width = ori_width 19 blank_image = np.zeros((padimg_height,padimg_width,3),image.dtype) 20 blank_image[(padimg_height-ori_height)//2:ori_height+(padimg_height-ori_height)//2,:,:] = image 21 anno[:,2]=(anno[:,2] * ori_height +(padimg_height-ori_height)//2 ) // padimg_height 22 anno[:,3]=anno[:,3]*ori_height//padimg_height 23 return blank_image,anno 24 25 }
三、代码运行效果如下图所示