在深度学习中,模型的输入size通常是正方形尺寸的,比如300 x 300这样.直接resize的话,会把图像拉的变形.通常我们希望resize以后仍然保持图片的宽高比.
例如:
可以利用copyMakeBorder和resize配合达到我们的目的.
import cv2
def resize_keep_aspectratio(image_src,dst_size):
src_h,src_w = image_src.shape[:2]
print(src_h,src_w)
dst_h,dst_w = dst_size
#判断应该按哪个边做等比缩放
h = dst_w * (float(src_h)/src_w)#按照w做等比缩放
w = dst_h * (float(src_w)/src_h)#按照h做等比缩放
h = int(h)
w = int(w)
if h <= dst_h:
image_dst = cv2.resize(image_src,(dst_w,int(h)))
else:
image_dst = cv2.resize(image_src,(int(w),dst_h))
h_,w_ = image_dst.shape[:2]
print(h_,w_)
top = int((dst_h - h_) / 2);
down = int((dst_h - h_+1) / 2);
left = int((dst_w - w_) / 2);
right = int((dst_w - w_+1) / 2);
value = [0,0,0]
borderType = cv2.BORDER_CONSTANT
print(top, down, left, right)
image_dst = cv2.copyMakeBorder(image_dst, top, down, left, right, borderType, None, value)
return image_dst
image_src = cv2.imread("/home/sc/disk/data/bdd-data/bdd_data/bdd100k/images/10k/train/0a0a0b1a-7c39d841.jpg")
dst_size = (720,720)
image = resize_keep_aspectratio(image_src,dst_size)
cv2.imshow("aaa",image)
print(image.shape)
if 27 == cv2.waitKey():
cv2.destroyAllWindows()
首先判断应该用w,h哪个方向的长度做等比缩放,缩放到合适的尺寸后,在用copyMakeBorder对剩余像素进行填充.深度学习中通常用灰度值128进行边界的填充.以文章开头的图片为例,处理后得到的图片: