How can i generate a video from dicom frames with opencv? #1685
-
I want to read the image frames from a dicom file, and use opencv to convert all the frames to a video. My code is as follows: from pydicom import dcmread
import cv2
def conver_dicom_to_video():
dcm_file = "./test/2.dcm"
output = dcm_file.replace(".dcm", ".mp4")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = 2
size = (512, 512)
video = None
dataset = dcmread(dcm_file)
arr = dataset.pixel_array
for i in range(10): # dataset.pixel_array.shape[0]
print(i)
img = arr[i]
# save the frame image to disk first
cv2.imwrite("{}.png".format(i), img)
# read the image from disk
img = cv2.imread("{}.png".format(i))
if i == 0:
# get the actual size of the generated images
size = (img.shape[0], img.shape[1])
video = cv2.VideoWriter(output, fourcc, fps, size)
video.write(img)
video.release() When i save images first, and then use opencv to read the images and write them to a video, it works. However, i think this is not a good way to save images to disk first. I tried to write the frames to the video directly, but i cloud not get the expected video file, the generated .mp4 file was always 1kb in size. The code is as follows: from pydicom import dcmread
import cv2
def conver_dicom_to_video():
dcm_file = "./test/2.dcm"
output = dcm_file.replace(".dcm", ".mp4")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = 2
size = (512, 512)
video = cv2.VideoWriter(output, fourcc, fps, size)
dataset = dcmread(dcm_file)
arr = dataset.pixel_array
for i in range(10): # dataset.pixel_array.shape[0]
print(i)
img = arr[i]
video.write(img)
video.release() Cloud you please give me some help, and is there any way to get a video for the frames directly? Looking forward to your reply! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
I had a quick look at this to see if I could see any obvious issues. The only thing I wondered was whether the unaltered frame is a correct parameter to |
Beta Was this translation helpful? Give feedback.
-
I don't have any experience with OpenCV, but I imagine there is either a flag to tell it you have greyscale data, or a way to convert grey to RGB. Hopefully you can find something with a little digging... |
Beta Was this translation helpful? Give feedback.
-
I got the solution! The parameter 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. |
Beta Was this translation helpful? Give feedback.
-
Glad you solved it, and thanks for posting the code back here so others can find it. |
Beta Was this translation helpful? Give feedback.
-
@yuanzicheng Hello, How to get the duration and write the video in real FPS? I saw you set the FPS to 2. |
Beta Was this translation helpful? Give feedback.
I got the solution! The parameter
isColor
should be set to 0 when calling thecv2.VideoWriter()
, if the frames are greyscale images.Thanks a lot for your kindly help.