主要是通过查看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