tensorflow:
def np_scale_pad(img, label, output_shape): """ 将图片缩放至指定大小,加pad,如果有标注也对标注进行缩放 :param img:图像数据 :param label: 标记数据,能乘以比例即可。 :param output_shape: 输出形状 :return:image, label """ output_height, output_width = output_shape[0:2] target_scale = output_width / output_height img_scale = img.shape[1] / img.shape[0] if img_scale > target_scale: # 按宽度缩放 new_size = output_width, int(round(output_width / img_scale)) scale = output_width / img.shape[1] else: new_size = int(round(output_height * img_scale)), output_height scale = output_height / img.shape[0] resized_img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR) padded_img = np.pad( resized_img, [[0, output_height - new_size[1]], [0, output_width - new_size[0]], [0, 0]], mode='constant') if label is None: return padded_img else: label = np.round(label * scale).astype(np.float32) return padded_img, label
pytorch:
import torch.nn.functional as F def pad_to_square(img, pad_value): c, h, w = img.shape dim_diff = np.abs(h - w) # (upper / left) padding and (lower / right) padding pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2 # Determine padding pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0) # Add padding img = F.pad(img, pad, "constant", value=pad_value) return img, pad
个人感觉还是pytorch好呀,啥都给弄好了