def rescale_image(frame, swin_h, swin_w, color=[255, 0, 0]):
"""Rescale image to size of working window.
Parameters
---------
frame : numpy.ndarray
given image or frame
swin_h : int
width of the working window
swin_w : int
height of the working window
color : list
The background color of appended border
"""
# new_frame = frame.copy()
# try to save memory
new_frame = frame
frame_h = frame.shape[0]
frame_w = frame.shape[1]
# if the ratio is different, then append border
if (float(swin_h)/float(swin_w)) != (float(frame_h)/float(frame_w)):
# do something
if (float(frame_h)/float(frame_w)) > (float(swin_h)/float(swin_w)):
w_append = int((frame_h*swin_w-swin_h*frame_w)/swin_h)
new_frame = cv2.copyMakeBorder(src=new_frame, top=0, bottom=0,
left=w_append/2, right=w_append/2,
borderType=cv2.BORDER_CONSTANT,
value=color)
elif (float(frame_h)/float(frame_w)) < (float(swin_h)/float(swin_w)):
h_append = int((swin_h*frame_w-frame_h*swin_w)/swin_w)
new_frame = cv2.copyMakeBorder(src=new_frame, top=h_append/2,
bottom=h_append/2, left=0, right=0,
borderType=cv2.BORDER_CONSTANT,
value=color)
new_frame = cv2.resize(new_frame, (swin_w, swin_h),
interpolation=cv2.INTER_AREA)
return new_frame
评论列表
文章目录