主要是通過查看pydicom讀取dicom文件的輸出查找存儲視頻的array和圖片的長和寬
try:
dataset = pydicom.dcmread(dicom_file_path)
except:
print('unreadable dcm file!')
continue
try:
patient_id = dataset[0x0010, 0x0020].value
except:
print('no patient id in dcm!')
continue
#單個dcm文件可能保存了多個video
#len(dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020]則代表保存video的個數
#store_array_idx代表保存視頻的索引
for store_array_idx in np.arange(len(dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020].value)):
#檢測存儲保存視頻長和寬的list(e.g. [612, 278, 0, 2])是否在(0x7fe1, 0x1086) in dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026]是否存在
#保存視頻長和寬的list(e.g. [640, 512, 0, 2])一般出現在最後幾個tag當中,都是連續出現,所以從後開始搜尋,可能在一個video中出現多組長寬組合
#hw_begin代表第一個長和寬list出現在倒數第幾個開始(由於可能由多組list)
hw_begin = 0
for i in np.arange(1, 11, 1):
if (0x7fe1, 0x1086) in dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026][-i].keys():
hw_begin = i
break
if hw_begin == 0:
continue
#hw_end代表最後一個長和寬list出現在倒數第幾個
for i in np.arange(hw_begin, 11, 1):
if (0x7fe1, 0x1086) in dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026][-i].keys() and (0x7fe1, 0x1086) not in dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026][
-(i + 1)].keys():
hw_end = i
break
#在dcm文件存儲的第store_array_idx個的video可以由多個存儲array的tag生成
#len(dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1036].value)即爲存儲當前video組成的array的個數
#一個video對應多個tag的array,順序按照tag的順序將多個array組合即能生成當前的整個video
num_private_arrays = len(
dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1036].value)
private_arrays = dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1036]
num_imgs_total = 0
height_list = []
width_list = []
store_array_idx_existence_check = 0
for idx in np.arange(num_private_arrays):
if idx == 0:
save_dir = ... + '_' + str(store_array_idx) + '/'
os.makedirs(save_dir)
#這裏保存時,由於一個的dcm存儲多個video,那以該dcm的名字+'_' + str(store_array_idx)作爲文件夾名存儲多個video
for j in np.arange(hw_begin, (hw_end + 1), 1):
height_temp = dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026][-j][0x7fe1, 0x1086][0]
width_temp = dataset[0x7fe1, 0x1001][0][0x7fe1, 0x1010][0][0x7fe1, 0x1020][store_array_idx][0x7fe1, 0x1026][-j][0x7fe1, 0x1086][1]
# 檢測array是否存在
if (0x7fe1, 0x1060) not in private_arrays[idx].keys():
continue
if len(private_arrays[idx][0x7fe1, 0x1060].value) % (height_temp * width_temp) == 0:
#array中元素的個數不一定整除長寬之積(height_temp * width_temp)
height = height_temp
width = width_temp
num_imgs = np.int(len(private_arrays[idx][0x7fe1, 0x1060].value) / (height * width))
#將存儲uint8元素的array轉成存儲numpy元素的array
imgs_array = np.array(np.frombuffer(private_arrays[idx][0x7fe1, 0x1060].value, dtype='uint8')).reshape(num_imgs, width, height)
print(len(private_arrays[idx][0x7fe1, 0x1060].value), height, width, num_imgs)
for img_idx in np.arange(num_imgs_total, (num_imgs_total + num_imgs), 1):
cv2.imwrite(save_dir + "Out_%04d.bmp" % (img_idx),imgs_array)
num_imgs_total += num_imgs