代码说明:1、将图像按较长的一边进行等比例缩放,2、固定大小输出并补全。
for imagen in [imagen for imagen in os.listdir(image_path) if os.path.isfile(os.path.join(image_path, imagen))]:
imagenes = os.path.join(image_path, imagen)
print(imagenes)
# img = cv2.resize(cv2.imread(imagenes, cv2.IMREAD_COLOR), (224, 224))
# print(type(img))
#zq 20230207 start1 resize等比例缩放
img =cv2.imread(imagenes, cv2.IMREAD_COLOR)
w, h = img.shape[1], img.shape[0] #宽、高
#print('初始值',w,h)
size_h,size_w=224,224
if h>w:
scale = h/size_h
w = int(w/scale)
h = size_h #图片的宽、高 以高封顶
else:
scale = w/size_w
h = int(h/scale)
w = size_w #图片的宽、高 以高封顶
#等比例缩放 https://www.iotword.com/6350.html
img = cv2.resize(img, (w,h))
# cv2.waitKey(0)
# cv2.imshow('img',img) #查看图片
#填充 https://blog.csdn.net/li17761193051/article/details/125321881
#print('resize后',w,h)
left = int((size_w-w)/2)
right = left
if w+left+right != size_w:
left = size_w - w - right
top = int((size_w-h)/2)
bottom = top
if h + top + bottom != size_h:
top = size_h - h - bottom
#print(top,bottom,left,right)
img = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,value=[255,255,255])
# cv2.imshow('img',img) #查看图片
# cv2.waitKey(0)
#zq 20230207 end1
等比例缩放摘自: https://www.iotword.com/6350.html
填充摘自:填充 https://blog.csdn.net/li17761193051/article/details/125321881