Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_videofile jumps #71

Closed
SalvatoreScaramuzzino opened this issue Sep 28, 2014 · 7 comments
Closed

write_videofile jumps #71

SalvatoreScaramuzzino opened this issue Sep 28, 2014 · 7 comments

Comments

@SalvatoreScaramuzzino
Copy link

Hi to all

i'm trying to create a pipeline for frame-by-frame analysis of medical images (cineangiographies)

i wrote a little script that reads a DICOM file that contains an angiograpy and generates the corresponding .avi file

from moviepy.editor import *
import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as stk
import dicom
import os
import shutil

dicomfilePath = "/home/salvatore/Didattica/data/IMG00001"
outputVideoPath = "/home/salvatore/Videos/"

fileDicom = stk.ReadImage(dicomfilePath)
infoDicom = dicom.read_file(dicomfilePath)

matrice = stk.GetArrayFromImage(fileDicom)

nframes = matrice.shape[0]
cine_fps = infoDicom.CineRate
patient_id = infoDicom.PatientID
study_id = infoDicom.StudyID
altro = infoDicom.InstanceNumber

# i create a temporary directory
directory = "/home/salvatore/Documents/appoggio"
if not os.path.exists(directory):
    os.makedirs(directory)

# then i save all frames
for i in range(0,nframes):
    plt.imsave(directory + "/frame" + str(i) + ".tiff", matrice[i,:,:], cmap=plt.cm.gray)

# using such frames to create a Sequence Clip
newVideo = ImageSequenceClip(directory,fps=cine_fps)

# write to video file
newVideo.write_videofile(outputVideoPath + patient_id + ".avi", fps=cine_fps , codec='png' ) 

# delete dir
shutil.rmtree(directory)

i have this "jumps" in my final video

https://www.youtube.com/watch?v=VfinQD6buZA&feature=youtu.be

ideas?

@Zulko
Copy link
Owner

Zulko commented Sep 28, 2014

I am not sure but I think the issue is that the frames are taken in aphanumerical order by ImageSequenceClip, and the way you wrote it "frame2.tiff" will be between "frame19.tiff" and "frame20.tiff", which explains the jumps. If you rename it to "frame02.tiff" it will work.

So you should use plt.imsave(directory+"/frame%02d.tiff"%i, matrice[i,:,:], etc.)

@Zulko
Copy link
Owner

Zulko commented Sep 28, 2014

On a side note, maybe you could avoid writing everything as image files, because ImageSequenceClip can be fed directly by numpy arrays. Theoretically you could pass "matrice" as as arguments directly, but I am not sure what their format is. So what is matrice made of ? Numbers between 0 and 1, or numbers between 0 and 255 ?

@SalvatoreScaramuzzino
Copy link
Author

I previously tried to pass "matrice" to ImageSequenceClip but it doesn't work

matrice is a numpy matrix from 0 to 255

thanks very much for early reply... i will give u a feedback as soon as possible

@Zulko
Copy link
Owner

Zulko commented Sep 28, 2014

Yeah ImageSequenceClip only accepts RGB pictures. You should try to convert the levels in matrice into RGB, like that (works on my computer):

matrice2 = [ np.dstack(3*[im]).astype("uint8") for im in matrice]
clip = mpy.ImageSequenceClip(matrice2, fps=cine_fps)

@SalvatoreScaramuzzino
Copy link
Author

it works perfectly!

it now write from directory of frames and also from numpy shaped as you wrote

thank you very much for this wonderful library :)

@Zulko
Copy link
Owner

Zulko commented Sep 28, 2014

Cool ! Maybe you can remove the video you put on youtube now, it makes a bad publicity for the library ;)

@SalvatoreScaramuzzino
Copy link
Author

sure :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants