我有一个包含 49 个切片的 .dcm 文件。我想将该 dcm 文件转换为 mp4,以便我可以看到视频格式的切片。我试过下面的代码,但它在视频中只显示一条白线。请帮忙。代码如下
import pydicom
import os
import cv2
dcm_file = 'path/to/dcmfile
ds = pydiom.dcmread(dcm_file)
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)
num_frames, num_rows, num_cols = pixel.data.shape
fourcc = cv2.VideoWriter_fourcc(*'XVID)
fps = 30.0
videowriter = cv2.VideoWriter('output.avi', fourcc, fps (num_cols, num_rows))
for frame in pixel_data:
frame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
video_writer.write(cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB))
video_writer.release()