diff --git a/util.py b/util.py index c57a032c058bb9b0dbfab1e8cdd906eb79dd6347..09967ed58dc51784dbf36c4b1c92c5ebca420252 100644 --- a/util.py +++ b/util.py @@ -13,16 +13,24 @@ import yaml # 导入yaml模块,用于处理YAML文件 # 函数返回处理后的图像 def random_transform(img, scale_min=0.5, scale_max=1.5): # 定义random_transform函数,用于对图像进行随机变换 h, w = img.shape[:2] # 获取图像的高度和宽度 - + # 1. 把图像进行在scale_min到scale_max范围内的随机缩放,缩放后(从图像中心)裁剪回原始大小 - # TODO + # 随机缩放图像 + scale = random.uniform(scale_min, scale_max) # 生成随机缩放因子 + new_size = (int(w * scale), int(h * scale)) # 计算新的尺寸 + img_resized = cv.resize(img, new_size, interpolation=cv.INTER_LINEAR) # 缩放图像 - cropped_img = # TODO:这是裁剪后的图像 + # 计算裁剪的起始点,以保持图像的中心 + start_x = (new_size[0] - w) // 2 + start_y = (new_size[1] - h) // 2 + cropped_img = img_resized[start_y:start_y + h, start_x:start_x + w] # 结语正负45°之间的某一个角度随机旋转图像,并保持裁剪后的图像大小 - # TODO - rotated_img = # TODO:这是旋转后的图像 + angle = random.uniform(-45, 45) # 生成随机旋转角度 + center = (w // 2, h // 2) # 旋转中心为图像中心 + M = cv.getRotationMatrix2D(center, angle, 1.0) # 获取旋转矩阵 + rotated_img = cv.warpAffine(cropped_img, M, (w, h)) # 应用旋转 return rotated_img # 返回旋转后的图像 @@ -90,7 +98,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 # 返回配置字典对象