From 072c13cf49741c446ad982bf23ca08475fe17387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=81=86=E8=AF=97=E5=90=9B?= <14437996+Tishijun@user.noreply.gitee.com> Date: Sun, 2 Jun 2024 15:45:21 +0000 Subject: [PATCH] update util.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 遆诗君 <14437996+Tishijun@user.noreply.gitee.com> --- util.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/util.py b/util.py index c57a032..e16060a 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 # 返回旋转后的图像 -- Gitee