simpleITK读取数据
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt
base_path = "/Users/yxk/Desktop/test/"
image = sitk.ReadImage(base_path+"000000.dcm") # type(image) <class 'SimpleITK.SimpleITK.Image'>
image_array = np.squeeze(sitk.GetArrayFromImage(image)) # type(image_array) ->> <class 'numpy.ndarray'> image_array.shape ->> (512, 512)
plt.imshow(image_array,cmap="gray")
plt.show()
#不加这个cmap="gray"其图像显示为蓝色背景
pydicom读取数据
import pydicom
import numpy as np
import matplotlib.pyplot as plt
base_path = "/Users/yxk/Desktop/test/"
ds = pydicom.dcmread(base_path+"000000.dcm")
# 打印基本信息
print(ds.dir())
print('Patient Name:', ds.PatientName)
print('Patient ID:', ds.PatientID)
print('Study Date:', ds.StudyDate)
print('Modality:', ds.Modality)
# 获取像素数据
print("*"*50)
pixel_data = ds.pixel_array
print("*"*50)
print(pixel_data.shape)
print("*"*50)
print(pixel_data)
print("*"*50)
print(np.max(pixel_data))
print("*"*50)
print(np.min(pixel_data))
print("*"*50)
# 显示图片
plt.imshow(pixel_data, cmap=plt.cm.gray)
plt.show()
结果如下
保存灰度图像(PNG是RGBA格式)
import pydicom
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
base_path = "/Users/yxk/Desktop/test/"
ds = pydicom.dcmread(base_path+"000000.dcm")
pixel_data = ds.pixel_array
plt.imsave("./AABB.png", pixel_data, cmap='gray')
img_path = './AABB.png'
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
# pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))
print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
# #显示图片
image.show()
##
显示图片
import matplotlib.pyplot as plt
display(Image.fromarray(img_arr))
保存成灰度图像(jpg格式是RGB)
import pydicom
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
base_path = "/Users/yxk/Desktop/test/"
ds = pydicom.dcmread(base_path+"000000.dcm")
pixel_data = ds.pixel_array
plt.imsave("./AABB.jpg", pixel_data, cmap='gray')
img_path = './AABB.jpg'
# 读取图片
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
# pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))
print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
# #显示图片
image.show()
##