该工具包可用于任务或者数据集增强视网膜眼底图像
源码地址:github
A. Gaudio, A. Smailagic, A. Campilho, “Enhancement of Retinal Fundus Images via Pixel Color Amplification,” In: Karray F., Campilho A., Wang Z. (eds) Image Analysis and Recognition. ICIAR 2020. Lecture Notes in Computer Science, vol 11663. Springer, Cham (accepted)
介绍
IETK-Ret - Image Enhancement Toolkit for Retinal Fundus Images
这个存储库包含了对视网膜眼底图像有用的增强方法的集合,重点是像素彩色放大。这些方法作为眼底图像分析和机器学习的预处理步骤非常有用。它们适用于各种各样的图像领域,但重点是视网膜眼底图像。
不多说了,教程
1、pip install ietk-ret
2、import
from matplotlib import pyplot as plt
from ietk import methods
from ietk import util
from ietk.data import IDRiD
3、load data
# load a fundus image, normalized into 0-1 range (not 0-255)
# such as one from the IDRiD dataset (assuming you already have it on disk)
dset = IDRiD('./data/IDRiD_segmentation')
img_id, img, labels = dset.sample()
print("using image", img_id)
或者如果是dcm类型的
import SimpleITK as sitk
def read_single_dicom_by_sitk(filepath):
image = sitk.ReadImage(filepath)
image_data = sitk.GetArrayFromImage(image)
return np.squeeze(image_data,axis = 0)
img = read_single_dicom_by_sitk(image_path)
4、归一化
def normalize_to_01(arr):
arr = arr.astype(np.float64)
# 计算数组的最小值和最大值
min_val = np.min(arr)
max_val = np.max(arr)
if min_val == max_val:
normalized_arr = np.zeros(arr.shape, dtype=np.float64)
else:
normalized_arr = (arr - min_val) / (max_val - min_val)
return normalized_arr
img = normalize_to_01(img)
5、裁剪眼底图像,得到焦点区域
I = img.copy()
I, fg = util.center_crop_and_get_foreground_mask(I)
6、用增强方法增强图像
enhanced_img = methods.brighten_darken(I, 'A+B+C+D', focus_region=fg)
# enhanced_img = (enhanced_img-enhanced_img.min())/(enhanced_img.max()-enhanced_img.min())
enhanced_img2 = methods.sharpen(I, bg=~fg)
7、反归一化存储
def map_to_0_255(original_array):
# 找到数组中的最小值和最大值
min_value = np.min(original_array)
max_value = np.max(original_array)
# 检查最大值和最小值是否相同,避免除以零
if max_value == min_value:
raise ValueError("所有数值都相同,无法进行映射")
# 初始化映射后的数组
# mapped_array = []
mapped_array = 255 * ((original_array - min_value) / (max_value - min_value))
def dcm_process(filepath, new_array, scale, out_path):
# 替换, 修改tag, 保存
new_array = map_to_0_255(new_array)
new_array = new_array.astype(np.uint8)
im=Image.fromarray(np.uint8(new_array)) # numpy 转 image类
im.show()
im.convert('RGB').save("test2.jpg", format='jpeg') # 复制了三个三通道,三个通道的值相同
dcm_process(image_path, enhanced_img2, 1, output_path)
如果要存为dcm,请参考我之前的多通道存储dcm的教程
另外,增强模式有很多种,可以调节methods.brighten_darken(I, 'A+B+C+D', focus_region=fg)参数,参数如下