I have a DICOMDIR file with a lot of dcm files in it. I have extracted the .dcm file with Instance Number 1. It has 49 slices/frames. I wnat to convert those frames into an mp4 format. My code gives and output but the video is still and has a yellow hue overlayed over the video. How can I resolve this.
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()