生活随笔
收集整理的這篇文章主要介紹了
将多个png或者jpg图片转为dicom或者nii格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
dicom和nii很復雜。
在網上找了好幾個小時,很少有自己能夠解釋清楚的。
單個圖片轉dcm
推薦下載這個軟件:
dicom-converter官網
缺點:只能把一張圖片轉化為nii格式
python代碼轉:
主要之前使用matlab 對numpy數組存放方式不是很了解
.應該是
[z
,x
,y
]這樣在itksnamp上看就對了
import SimpleITK
as sitk
import glob
import numpy
as np
from PIL
import Image
import cv2
import matplotlib
.pyplot
as plt
def save_array_as_nii_volume(data
, filename
, reference_name
= None):"""save a numpy array as nifty imageinputs:data: a numpy array with shape [Depth, Height, Width]filename: the ouput file namereference_name: file name of the reference image of which affine and header are usedoutputs: None"""img
= sitk
.GetImageFromArray
(data
)if(reference_name
is not None):img_ref
= sitk
.ReadImage
(reference_name
)img
.CopyInformation
(img_ref
)sitk
.WriteImage
(img
, filename
)image_path
= './oriCvLab/testCvlab/img/'
image_arr
= glob
.glob
(str(image_path
) + str("/*"))
image_arr
.sort
()print(image_arr
, len(image_arr
))
allImg
= []
allImg
= np
.zeros
([165, 768,1024], dtype
='uint8')
for i
in range(len(image_arr
)):single_image_name
= image_arr
[i
]img_as_img
= Image
.open(single_image_name
)img_as_np
= np
.asarray
(img_as_img
)allImg
[i
, :, :] = img_as_np
save_array_as_nii_volume
(allImg
, './testImg.nii.gz')
print(np
.shape
(allImg
))
img
= allImg
[:, :, 55]
多個圖片轉dcm
這里需要使用python或者dcmtk庫,進行更深入細節的編程,但是需要對這兩種格式本身有比較深入的了解。
總結
以上是生活随笔為你收集整理的将多个png或者jpg图片转为dicom或者nii格式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。