医学图像肝脏分割(六)---将预测分割图和原图合在一起
这是一个非常有意思的功能,花了一天的时间,写了一个初步的代码。
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 17:39:14 2020@author: xiaob
"""
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSlider
import SimpleITK as sitk
from PIL import Imageimg_path = r'F:\AI_Medical_Dataset\3Dircadb1\train\3Dircadb1.1\PATIENT_DICOM\image_100'
seg_path = r'F:\AI_Medical_Dataset\3Dircadb1\train\3Dircadb1.1\MASKS_DICOM\liver\image_100'
predict_path = r'F:\AI_Medical_Dataset\3Dircadb1\train\3Dircadb1.1\MASKS_DICOM\liver\image_120'img = sitk.ReadImage(img_path)
img = sitk.Cast(sitk.RescaleIntensity(img),sitk.sitkUInt8)
image_array = np.squeeze(sitk.GetArrayFromImage(img))
img_pil = Image.fromarray(image_array)seg = sitk.ReadImage(seg_path)
seg = sitk.Cast(sitk.RescaleIntensity(seg),sitk.sitkUInt8) # sitk.Cast 将图像的像素类型转换为另一种像素类型
seg_array = np.squeeze(sitk.GetArrayFromImage(seg))
seg_pil = Image.fromarray(seg_array)label_over = sitk.LabelOverlay(img, seg)
label_over_array = np.squeeze(sitk.GetArrayFromImage(label_over))predict = sitk.ReadImage(predict_path)
predict = sitk.Cast(sitk.RescaleIntensity(predict),sitk.sitkUInt8)
predict_array = np.squeeze(sitk.GetArrayFromImage(predict))
predict_pil = Image.fromarray(predict_array)# 第一次合并
image = Image.blend(img_pil,seg_pil,0.3)
image_pil = np.asarray(image)
image_pil = Image.fromarray(image_pil)
# 第二次合并
image_label = Image.blend(image_pil, predict_pil,0.3)
plt.subplot(2,4,1)
plt.imshow(image_array, cmap ='gray')
plt.subplot(2,4,2)
plt.imshow(seg_array, cmap = plt.cm.gray)
plt.subplot(2,4,3)
plt.imshow(image, cmap = plt.cm.gray)
plt.subplot(2,4,4)
plt.imshow(image_label)
plt.show()
【结果示例】

【参考】
1、【SimpleITK】分割label覆盖到原图上显示
2、语义分割将分割图和原图合在一起
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
