diff --git a/util.py b/util.py index c57a032c058bb9b0dbfab1e8cdd906eb79dd6347..e16060a441287ca2576fb1a01b4f91e1d9b1f400 100644 --- a/util.py +++ b/util.py @@ -15,14 +15,17 @@ def random_transform(img, scale_min=0.5, scale_max=1.5): # 定义random_transfo h, w = img.shape[:2] # 获取图像的高度和宽度 # 1. 把图像进行在scale_min到scale_max范围内的随机缩放,缩放后(从图像中心)裁剪回原始大小 - # TODO + scale_factor = random.uniform(scale_min, scale_max) + scaled_img = cv.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv.INTER_LINEAR) + new_h, new_w = scaled_img.shape[:2] + start_h = max(0, (new_h - h) // 2) + start_w = max(0, (new_w - w) // 2) + cropped_img = scaled_img[start_h:start_h + h, start_w:start_w + w] - cropped_img = # TODO:这是裁剪后的图像 - - - # 结语正负45°之间的某一个角度随机旋转图像,并保持裁剪后的图像大小 - # TODO - rotated_img = # TODO:这是旋转后的图像 + # 随机旋转图像在正负45°之间,并保持裁剪后的图像大小 + angle = random.uniform(-45, 45) + rotation_matrix = cv.getRotationMatrix2D((w / 2, h / 2), angle, 1) + rotated_img = cv.warpAffine(cropped_img, rotation_matrix, (w, h)) return rotated_img # 返回旋转后的图像