From 388f5ed277938a273a333f3e499f9fc736dfe548 Mon Sep 17 00:00:00 2001 From: weixin_46229132 Date: Fri, 30 May 2025 09:26:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86wlk=5Fright=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datasets_pro_code/png_mask_trans.py | 23 +++++++++++++++++++++++ datasets_pro_code/read_mask.py | 14 +++++--------- datasets_pro_code/remove_tif.py | 6 +++--- 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 datasets_pro_code/png_mask_trans.py diff --git a/datasets_pro_code/png_mask_trans.py b/datasets_pro_code/png_mask_trans.py new file mode 100644 index 0000000..1cf3e80 --- /dev/null +++ b/datasets_pro_code/png_mask_trans.py @@ -0,0 +1,23 @@ +import os +import numpy as np +from PIL import Image +from osgeo import gdal + +src_dir = r"E:\RSdata\wlk_right_448\mask_png_ori" +dst_dir = r"E:\RSdata\wlk_right_448\mask_png" +os.makedirs(dst_dir, exist_ok=True) + +for file in os.listdir(src_dir): + if file.lower().endswith('.png'): + img = Image.open(os.path.join(src_dir, file)) + mask = np.array(img) + + # 替换像素值 + mask = mask.copy() + mask[mask == 15] = 255 + + png_path = os.path.join(dst_dir, os.path.splitext(file)[0] + ".png") + Image.fromarray(mask.astype(np.uint8)).save(png_path) + print(f"已保存: {png_path}") + +print("全部转换完成!") diff --git a/datasets_pro_code/read_mask.py b/datasets_pro_code/read_mask.py index 1e8fa2a..2c6c7e2 100644 --- a/datasets_pro_code/read_mask.py +++ b/datasets_pro_code/read_mask.py @@ -4,19 +4,15 @@ import os from osgeo import gdal -mask_dir = r"E:\datasets\wlk_right_448\mask" # 修改为你的mask文件夹路径 +mask_dir = r"E:\RSdata\wlk_right_448\mask_png_ori" # 修改为你的mask文件夹路径 all_labels = set() for file in os.listdir(mask_dir): - if file.lower().endswith('.tif'): - tif_path = os.path.join(mask_dir, file) - dataset = gdal.Open(tif_path) - if dataset is None: - print(f"无法打开: {tif_path}") - continue - band = dataset.ReadAsArray() - unique = np.unique(band) + if file.lower().endswith('.png'): + img = Image.open(os.path.join(mask_dir, file)) + img = np.array(img) + unique = np.unique(img) all_labels.update(unique) print("所有mask中出现过的标签数字:", sorted(all_labels)) diff --git a/datasets_pro_code/remove_tif.py b/datasets_pro_code/remove_tif.py index 63563d1..0e46f1f 100644 --- a/datasets_pro_code/remove_tif.py +++ b/datasets_pro_code/remove_tif.py @@ -1,6 +1,6 @@ -input_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val.txt" -output_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val_no.txt" +input_path = r"E:\RSdata\wlk_right_448\val.txt" +output_path = r"E:\RSdata\wlk_right_448\val_no.txt" with open(input_path, "r", encoding="utf-8") as fin, open(output_path, "w", encoding="utf-8") as fout: for line in fin: - fout.write(line.strip().replace(".tif", "") + "\n") \ No newline at end of file + fout.write(line.strip().replace(".jpg", "") + "\n") \ No newline at end of file