提取 DICOMDIR 切片并转换为 mp4 文件

问题描述 投票:0回答:0

我有一个 DICOMDIR 文件,里面有很多 dcm 文件。我提取了实例编号为 1 的 .dcm 文件。它有 49 个切片/帧。我想将这些帧转换为 mp4 格式。我的代码给出并输出,但视频静止不动,并且在视频上覆盖了黄色色调。我该如何解决这个问题。

import numpy as np
import pydicom
import os
import cv2

dicomdir_path = 'dcm_files/DICOMDIR'
dicomdir = pydicom.dcmread(dicomdir_path)

record = None
for record in dicomdir.DirectoryRecordSequence:
    if record.DirectoryRecordType =='IMAGE' and record.InstanceNumber == 1:
        break

filename = os.path.join(os.path.dirname('dcm_files/DICOMDIR'), record.ReferencedFileID[1])
ds = pydicom.dcmread(filename)

num_frames = ds.NumberOfFrames

pixel_data = ds.pixel_array

if len(pixel_data.shape) > 2:
    pixel_data = pixel_data[0]

pixel_data = cv2.normalize(pixel_data, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
print(pixel_data.shape)

num_rows, num_cols, _ = pixel_data.shape

pixel_data = pixel_data.reshape((num_rows, num_cols, 3))
pixel_data = [pixel_data] * num_frames
pixel_data = np.array(pixel_data)

print(pixel_data.shape)

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
fps = ds.CineRate
video_writer = cv2.VideoWriter('Output/output.mp4', fourcc, fps, (num_cols, num_rows))

for i in range(num_frames):
    frame = pixel_data[i]
    if len(frame.shape) == 2:
        frame_bgr = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
    else:
        frame_bgr = frame
    video_writer.write(frame)

video_writer.release()
python video-processing dicom pydicom medical-imaging
© www.soinside.com 2019 - 2024. All rights reserved.