Friday, February 10, 2017

Vidéos: How to load large videos?

When working with videos, it is difficult to process large files because of the memory limits in ImageJ. Here are few tips to try to circumvent this limitation.

We assume that you have already converted your video in a series of images with a tool like ffmpeg [see post].

1. Configuration

The first step is to increase the maximum memory that can use ImageJ.

1.1. In Linux
Edit the file entitled run  located in ImageJ folder:

java -Xmx512m -jar ij.jar

512m — meaning 512 megabytes — corresponds to the maximum size of the memory allocation and is rather small compared to the memory installed in a computer (laptop, for example). You may increase the amount of memory allocated to ImageJ to 2048 or more (use a multiple of 1024) depending of your configuration.

java -Xmx2048m -jar ij.jar

Save the modified file and restart ImageJ by typing in your console:

1.2. In Other OS
Run ImageJ and go to Edit > Preferences > Memory & Threads, and increase the amount of memory. Restart ImageJ.

2. Manipulating large stacks

2.1. Load "virtual" stack
After checking - and modifying your configuration, if required - it is faster and more convenient to load all your images as a virtual stack in File > Import > Image Sequence..., check the Virtual Stack option (Fig.1).

Fig.1: Import Sequence Dialog Box. The virtual stack option is checked.
In a virtual stack, all the images (video frames) are not in memory, you can display them but cannot make any processing. This option is specially useful to determine the first and last frames of interest in your video.

2.2. Load video chunks
If you cannot load all the images of your video because of memory issues, you may load parts of your video and convert the stack in 8-bit leading to a stack using 3 times less memory than the equivalent RGB stack. Save each stack in tif format. Reload all the temporary stacks and concatenate them with Image > Stacks > Tools > Concatenate...

2.3. Skip Frames
If you cannot load all the frames, the last solution is to increase the value of Increment in the Import Sequence dialog box (see Fig. 1). A value of 2 — for example — means that you skip one frame out of two.

2.4. Play with options of ffmpeg
If you cannot load all the frames, one solution is to convert the video into 8-bit images (aka frames). This is possible in ffmpeg [Link] with the option -pix_fmt.

ffmpeg -i my_video.mp4 -f image2 -pix_fmt gray frames/frame%05d.png
2.5. Post-processing
Once imported, it is sometimes required to save memory for further processing:
  • Convert your stack in 8-bit (Image > Type > 8-bit).
  • Split the color channels (Image > Color > Split Channels ) and only keep the best one. 
  • Focus to the region of interest by cropping the stack (Image > Crop).

<<  Previous: Import Image Series  Next:  >>

3. Other crazybiocomputing posts

Further readings are available in ...
  • Video Series  [Link]
  • Image Processing TOC [Link]

No comments:

Post a Comment