赞
踩
对目录下图片所有图片进行染色归一化
需要安装Staintools库, 若提示出现No modules spams,window用户请自行百度搜索安装攻略,Linux系统直接pip install spams即可
- import os
- import cv2
- import staintools
- from staintools.stain_normalizer import StainNormalizer
- import numpy as np
-
-
- target_stain_path = "/home/ivan/project_TransUNet/data/Gland1/test/images/testA_52.bmp" # 替换为目标染色图像的路径
- target = staintools.read_image(target_stain_path)
-
- # 设置图像文件夹路径
- image_folder_path = "/home/ivan/project_TransUNet/data/Gland/test2/images"
-
- # 新建保存归一化后图像的文件夹
- output_folder_path = "/home/ivan/project_TransUNet/data/Gland/test2/images"
- os.makedirs(output_folder_path, exist_ok=True)
-
- # 遍历图像文件夹中的所有图像文件
- for filename in os.listdir(image_folder_path):
- if filename.endswith('.bmp'):
- file_path = os.path.join(image_folder_path, filename)
-
- image = staintools.read_image(file_path)
- target = staintools.LuminosityStandardizer.standardize(target)
- image = staintools.LuminosityStandardizer.standardize(image)
- normalizer = staintools.StainNormalizer(method='vahadane')
- normalizer.fit(target)
- normalized_image = normalizer.transform(image)
- # 保存归一化后的图像
- save_path = os.path.join(output_folder_path, filename)
- cv2.imwrite(save_path, normalized_image)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。