diff --git a/util.py b/util.py index c57a032c058bb9b0dbfab1e8cdd906eb79dd6347..a8e181b8bffc4881ef805048e02339d343c4a030 100644 --- a/util.py +++ b/util.py @@ -15,15 +15,18 @@ 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 - - cropped_img = # TODO:这是裁剪后的图像 - - - # 结语正负45°之间的某一个角度随机旋转图像,并保持裁剪后的图像大小 - # TODO - rotated_img = # 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] + # 随机旋转图像在正负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 # 返回旋转后的图像 # 这个函数用于处理zip文件中的图像,可以设置缩放范围、生成图像数量、进度回调、线程事件和输出类型 @@ -90,7 +93,7 @@ def process_images_from_zip(zip_path, scale_min=0.5, scale_max=1.5, num_images=1 def load_config(): # 从config.yaml文件中加载配置字典对象 - with open('config.yaml', 'r') as f: + with open('config.yaml', 'rb') as f: config = yaml.safe_load(f) return config # 返回配置字典对象