有些数据集给的掩码是全黑图片,需要将全黑的掩码剔除,保留有标签的掩码。
DDR-dataset 眼底图像处理
from PIL import Image
import cv2
import os
def extract_mask_and_original(mask_path, original_path, output_folder):
# 读取黑白掩码图片和同名原图图片
mask_image = Image.open(mask_path).convert("L")
original_image = Image.open(original_path)
has_white_pixel = any(pixel > 0 for pixel in mask_image.getdata())
if has_white_pixel:
# 提取同名的原图图片
original_filename = os.path.basename(mask_path).replace(".tif", ".jpg")
original_path = os.path.join(input_image_folder, original_filename)
# 保存掩码图片和原图图片
output_mask_path = os.path.join(output_folder_mask, os.path.basename(mask_path))
output_original_path = os.path.join(output_folder_image, original_filename)
mask_image.save(output_mask_path)
original_image.save(output_original_path)
print(f"已提取掩码和原图:{output_mask_path},{output_original_path}")
else:
print(f"掩码中不存在白色像素:{mask_path}")
print("提取完成")
# 设置输入文件夹和输出文件夹
input_mask_folder = r"E:\yanjiu\datasets\DDR_segmentation\valid\mask"
input_image_folder = r"E:\yanjiu\datasets\DDR_segmentation\valid\image"
output_folder_mask = r"E:\yanjiu\datasets\DDR_segmentation\valid\mask_new"
output_folder_image =r"E:\yanjiu\datasets\DDR_segmentation\valid\image_new"
# 遍历输入文件夹中的掩码图片
for mask_filename in os.listdir(input_mask_folder):
if mask_filename.endswith(".tif"):
mask_path = os.path.join(input_mask_folder, mask_filename)
original_filename = mask_filename.replace(".tif", ".jpg")
original_path = os.path.join(input_image_folder, original_filename)
extract_mask_and_original(mask_path, original_path, output_folder_mask)