Skip to content

How can i generate a video from dicom frames with opencv? #1685

Answered by yuanzicheng
yuanzicheng asked this question in Q&A
Discussion options

You must be logged in to vote

I got the solution! The parameter isColor should be set to 0 when calling the cv2.VideoWriter(), if the frames are greyscale images.

from pydicom import dcmread
import cv2


def convert_dicom_to_video():
    dcm_file = "./test/2.dcm"
    
    output = dcm_file.replace(".dcm", ".mp4")
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    fps = 10
    size = (512, 512)
    video = cv2.VideoWriter(output, fourcc, fps, size, isColor=0)
    
    dataset = dcmread(dcm_file)
    for i in range(dataset.pixel_array.shape[0]):
        img = dataset.pixel_array[i]
        video.write(img)
    video.release()

Thanks a lot for your kindly help.

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@yuanzicheng
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by mrbean-bremen
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@yuanzicheng
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1684 on August 26, 2022 15:05.