-
Notifications
You must be signed in to change notification settings - Fork 369
Handling Movies
Movies can be load either singularly or in chuncks.
Load single movie:
import caiman as cm
single_movie = cm.load('example_movies/demoMovie.tif')
print(single_movie.shape)
or load multiple movies and concatenate in time:
import caiman as cm
file_names = ['example_movies/demoMovie.tif','example_movies/demoMovie.tif'] # for the sake of the example we repeat the same movie
movies_chained = cm.load_movie_chain(file_names)
print(movies_chained.shape)
One can specify several parameters while loading. For instance frame rate, or if only some portion of the movies needs to be loaded, and so on. Check the documentation.
Both functions returns a movie object. The movie object can also be constructed giving as input one 3D array (time x x_dimension x y_dimension). Example.
import caiman as cm
movie_random = cm.movie(np.random.random([1000,100,100]))
Movies can be saved in different format (.mat, .tif, .hdf5, etc). In order to save just call the save command with the appropriate file extension.
movie_random.save('movie_random.tif')
It is also possible to save in a memory mappable format. This is an advanced topic that is dealt with in the demos in the root folder.
One can also play very efficiently movies with the play function. The play function allows to modulate the exposure, the magnification, the playing frame rate, and adding an offset (sometimes movies are too negative or too positive thus hindering good visualization)
Example:
movies_chained.play(gain = 2., magnification = 2, fr = 100)
Ways a movie can be visualized are, for instance:
import pylab as pl
pl.imshow(np.mean(movies_chained,0))
pl.imshow(np.std(movies_chained,0))
pl.plot(np.mean(movies_chained, axis = (1,2)))
In this sense it is also very convenient the correlation image
c_img = movies_chained.local_correlations(eight_neighbours = True, swap_dim = False)
pl.imshow(c_img)
For some types of files the tiff reader swap a dimension, for unknown reasons. Therefore, sometime you might have to change the parameter swap_dim, especially if the correlation image does not make any sense.
At all the effect the movie object behaves exactly like a numpy array. It can be summed, multiplied, divided, etc... This behavior is very versatile. The are few functions that cannot be implemented as an array, for instance concatenation. For that operation there is a special function, cm.concatenate:
movies_chained = cm.concatenate([single_movie,single_movie] , axis = 0)
Sometime it is very convenient to downsample or upsample the movies across some dimensions. We have implemented a very efficient way of doing so, based on the opencv library. Below an example putting it all together:
movies_chained = cm.concatenate([single_movie,single_movie] , axis = 1).resize(1,1,.5).play(gain = 2., magnification = 2, fr = 50)