欢迎光临
我们一直在努力

什么ct要4000基于深度学习的CT图像肺结节自动检测技术二——训练数据处理

def normalize_hu(image):

"""
将输入图像的像素值(-4000 ~ 4000)归一化到0~1之间
:param image 输入的图像数组
:return: 归一化处理后的图像数组
"""

MIN_BOUND = -1000.0
MAX_BOUND = 400.0
image = (image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
image[image > 1] = 1.
image[image < 0] = 0.
return image


def load_patient_images(src_dir, wildcard="*.*", exclude_wildcards=[]):
"""
读取一个病例的所有png图像,返回值为一个三维图像数组
:param image 输入的一系列png图像
:return: 三维图像数组
"""

src_img_paths = glob.glob(src_dir + wildcard)
for exclude_wildcard in exclude_wildcards:
exclude_img_paths = glob.glob(src_dir + exclude_wildcard)
src_img_paths = [im for im in src_img_paths if im not in exclude_img_paths]
src_img_paths.sort()
images = [cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) for img_path in src_img_paths]
images = [im.reshape((1,) + im.shape) for im in images]
res = np.vstack(images)
return res


def save_cube_img(target_path, cube_img, rows, cols):
"""
将3维cube图像保存为2维图像,方便勘误检查
:param 二维图像保存路径, 三维输入图像
:return: 二维图像
"""

assert rows * cols == cube_img.shape[0]
img_height = cube_img.shape[1]
img_width = cube_img.shape[1]
res_img = np.zeros((rows * img_height, cols * img_width), dtype=np.uint8)

for row in range(rows):
for col in range(cols):
target_y = row * img_height
target_x = col * img_width
res_img[target_y:target_y + img_height,
target_x:target_x + img_width] = cube_img[row * cols + col]

cv2.imwrite(target_path, res_img)


if __name__ == '__main__':
dicom_dir = './data/dicom_demo/'

slices = load_patient(dicom_dir) # 读取dicom文件的元数据(dicom tags)

pixel_spacing = slices[0].PixelSpacing # 获取dicom的spacing值
pixel_spacing.append(slices[0].SliceThickness)
print('The dicom spacing : ', pixel_spacing)

image = get_pixels_hu_by_simpleitk(dicom_dir) # 提取dicom文件中的像素值
# 标准化不同规格的图像尺寸, 统一将dicom图像缩放到1mm:1mm:1mm的尺度
image = rescale_patient_images(image, pixel_spacing, 1.00)
for i in tqdm(range(image.shape[0])):
img_path = "./temp_dir/dcm_2_png/img_" + str(i).rjust(4, '0') + "_i.png"

org_img = normalize_hu(image[i]) # 将像素值归一化到[0,1]区间

cv2.imwrite(img_path, org_img * 255) # 保存图像数组为灰度图(.png)

# 加载上一步生成的png图像
pngs = load_patient_images("./temp_dir/dcm_2_png/", "*_i.png")
# 输入人工标记的结节位置: coord_x, coord_y, coord_z
cube_img = get_cube_from_img(pngs, 272, 200, 134, 64)
print(cube_img)
save_cube_img('./temp_dir/chapter3_3dcnn_img_X.png', cube_img, 8, 8)
赞(0)
未经允许不得转载:上海聚慕医疗器械有限公司 » 什么ct要4000基于深度学习的CT图像肺结节自动检测技术二——训练数据处理

登录

找回密码

注册