From bc845b45afa186d2909f5237178ff500a0f7f72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Wed, 27 Nov 2024 17:01:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=83=8F=E9=A2=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 实验六/pretrain.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/实验六/pretrain.py b/实验六/pretrain.py index e69de29..56360e5 100644 --- a/实验六/pretrain.py +++ b/实验六/pretrain.py @@ -0,0 +1,34 @@ +import cv2 as cv +import os +import tqdm + + +# 获取目录下所有文件的路径 +def get_img_names(directory): + file_paths = [] + for root, dirs, files in os.walk(directory): + for file in files: + file_paths.append(os.path.join(root, file)) + return file_paths + + +# 导入并预处理 +def pretrain(img_path, save_path): + img = cv.imread(img_path, cv.IMREAD_GRAYSCALE) + _,img = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU) + img = cv.blur(img, (3, 3)) + + os.makedirs(os.path.dirname(save_path), exist_ok=True) + + cv.imwrite(save_path, img) + +if __name__ == '__main__': + trains_paths=get_img_names('DataImages-Train/') + for train_path in tqdm.tqdm(trains_paths,desc='预处理训练文件中:'): + save_path=os.path.join('cache/pretrains/train', os.path.basename(train_path)) + pretrain(train_path,save_path) + + tests_paths=get_img_names('DataImages-Test/') + for test_path in tqdm.tqdm(tests_paths,desc='预处理测试文件中:'): + save_path=os.path.join('cache/pretrains/test', os.path.basename(test_path)) + pretrain(test_path,save_path) \ No newline at end of file