Hoffman2:Scripts:gen movie.py

From Center for Cognitive Neuroscience
Revision as of 22:39, 26 August 2024 by Hwang (talk | contribs) (→‎Usage/Help)
Jump to navigation Jump to search

Purpose

To convert a 3D MRI image in nifti format to a movie.

Setup Environment

To use this script, you need to load Anaconda module first and use conda environment ccn_py37
ccn_py37 contains nibabel which is need to read nifti files. ffmpeg is also needed for this script to run.

 module load anaconda3
 conda activate /u/project/CCN/apps/conda/rh7/ccn_py37
 module load ffmpeg/5.0.1

Usage/Help

To see the usage of this script

./gen_movie.py --help usage: gen_movie.py [-h] -i INPUT [-o OUTDIR] [-b BIN] [-w WIN]

optional arguments:

 -h, --help            show this help message and exit
 -i INPUT, --input INPUT
                       Full path to input nii file
 -o OUTDIR, --outdir OUTDIR
                       Output directory for movie file
 -d DIM, --dim DIM     Create movie with specific dimension, i.e. options: 0 or 1 or 2. defaul: 2
 -b BIN, --bin BIN     Number of bins used in histogram. i.e. 100
 -w WIN, --win WIN     Window range. i.e. "0.05 0.995"

Example

/u/project/ccn/apps/scripts/gen_movie.py -i /path/to/T1w_brain.nii.gz -o /path/to/output/ --win "0.05 0.995" --bin 100

In this example, the script will read the input file from /path/to/T1w_brain.nii.gz and create a "movie" folder under /path/to/output/ and generate a movie file from the input file under movie/ folder, named as mri_movie.mp4. So you will find the output file at /path/to/output/movie/mri_movie.mp4.

Also in this case, the script will apply a histogram with 100 bins on the MRI data, the data value lower than 0.05 or higher than 0.995 will be replaced by windowing method. The window range and numbers of bins can be adjusted with --win and --bin options.