用SimpleITK查看医学图像大小与通道数

import SimpleITK as sitk
from matplotlib import pyplot as plt
 
def showNii(img):
    for i in range(img.shape[0]):
        plt.imshow(img[i,:,:],cmap='gray')
        plt.show()
        
itk_img = sitk.ReadImage('./hamican/cancer/images_train/images/00b0847ed013c1c8a20e36bd45e7cbf825b5de366ee69cc9a289442919e583e5/1.jpg')
itk_img1 = sitk.ReadImage('./hamican/cancer/images_train/images/0fd6381eb57fdc43ad600f3a0fcd9e7e1946da62e3d730554dc2d610c09a2518/1.tif')
#img = sitk.GetArrayFromImage(sitk.ReadImage(itk_img)) 
img = sitk.GetArrayFromImage(itk_img1)
print(img.shape)    # (155, 240, 240) 表示各个维度的切片数量
#showNii(img)
import cv2
from PIL import Image
import numpy as np
from skimage.io import  imread
import SimpleITK as sitk
from matplotlib import pyplot as plt

filepath = './hamican/cancer/images_train/images/00b0847ed013c1c8a20e36bd45e7cbf825b5de366ee69cc9a289442919e583e5/1.jpg'

cv2_im = cv2.imread('./hamican/cancer/images_train/images/00b0847ed013c1c8a20e36bd45e7cbf825b5de366ee69cc9a289442919e583e5/1.jpg')
print('cv2_im shape ',cv2_im.shape) # (height, width, ch)

im = Image.open(filepath)
print('PIL image size', im.size) # (width, height, ch)
pil_im = np.asarray(im)
print('pil_im shape ',pil_im.shape) # (height, width, ch)

sk_im = imread(filepath)
print('sk_im shape', sk_im.shape) # (height, width, ch)

# pydicom: dcm_slice = pydicom.dcmread(filename)
#          slice_data = dcm_slice.pixel_array   shape:(height, width)
#          若想更改原始像素size或其他操作,必须写回至原始二进制属性PixelData 中
#          dcm_slice.PixelData = dcm_slice.pixel_array.tobytes()
# SimpleITK: 
itk_im = sitk.ReadImage(filepath)  #size:(width, height, depth)
print(itk_im)    
np_im = sitk.GetArrayFromImage(itk_im) #shape:(depth, height, width)
print(np_im) 

你可能感兴趣的:(python)