处理wlk_right数据集
This commit is contained in:
parent
704c24d79d
commit
388f5ed277
23
datasets_pro_code/png_mask_trans.py
Normal file
23
datasets_pro_code/png_mask_trans.py
Normal file
@ -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("全部转换完成!")
|
@ -4,19 +4,15 @@ import os
|
|||||||
from osgeo import gdal
|
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()
|
all_labels = set()
|
||||||
|
|
||||||
for file in os.listdir(mask_dir):
|
for file in os.listdir(mask_dir):
|
||||||
if file.lower().endswith('.tif'):
|
if file.lower().endswith('.png'):
|
||||||
tif_path = os.path.join(mask_dir, file)
|
img = Image.open(os.path.join(mask_dir, file))
|
||||||
dataset = gdal.Open(tif_path)
|
img = np.array(img)
|
||||||
if dataset is None:
|
unique = np.unique(img)
|
||||||
print(f"无法打开: {tif_path}")
|
|
||||||
continue
|
|
||||||
band = dataset.ReadAsArray()
|
|
||||||
unique = np.unique(band)
|
|
||||||
all_labels.update(unique)
|
all_labels.update(unique)
|
||||||
|
|
||||||
print("所有mask中出现过的标签数字:", sorted(all_labels))
|
print("所有mask中出现过的标签数字:", sorted(all_labels))
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
input_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val.txt"
|
input_path = r"E:\RSdata\wlk_right_448\val.txt"
|
||||||
output_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val_no.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:
|
with open(input_path, "r", encoding="utf-8") as fin, open(output_path, "w", encoding="utf-8") as fout:
|
||||||
for line in fin:
|
for line in fin:
|
||||||
fout.write(line.strip().replace(".tif", "") + "\n")
|
fout.write(line.strip().replace(".jpg", "") + "\n")
|
Loading…
Reference in New Issue
Block a user