utils_videos

Helpers for videos


source

play_video

 play_video (video, figsize=(5, 5), fps=10)

Displays a stack of images as a video inside jupyter notebooks.

Type Default Details
video ndarray Stack of images.
figsize tuple (5, 5) Canvas size of the video.
fps int 10 Video frame rate.
Returns Video object Returns a video player with input stack of images.

source

convert_uint8

 convert_uint8 (vid, with_vips=False)

Converts a stack of images in to 8bit pixel format.

This is a helper function for transform_to_video

Type Default Details
vid ndarray Stack of images.
with_vips bool False
Returns ndarray Image stack in 8bit.

source

psf_width

 psf_width (NA=1.46, wavelength=5e-07, resolution=1e-07)

Computes the PSF full width at half maximum (FWHM).

This is a helper function for transform_to_video

Type Default Details
NA float 1.46 Numerical aperture.
wavelength float 5e-07 Wavelength.
resolution float 1e-07 Resolution of the camera.
Returns int PSF width in pixels.

source

func_poisson_noise

 func_poisson_noise ()

Applies poisson noise to an image.

This is a custom DeepTrack feature, and a helper function for transform_to_video


source

mask

 mask (circle_radius, particle_list=[])

Computes binary masks for particles in microscopy videos.

This is a custom DeepTrack feature, and a helper function for transform_to_video.

transform_to_video


source

transform_to_video

 transform_to_video (trajectory_data, particle_props={}, optics_props={},
                     background_props={}, get_vip_particles=[],
                     with_masks=False, save_video=False, path='',
                     motion_blur_generator=None)

Transforms trajectory data into microscopy imagery data.

Trajectories generated through phenomenological models in andi-datasets are imaged under a Fluorescence microscope to generate 2D timelapse videos.

Type Default Details
trajectory_data ndarray Generated through models_phenom. Array of the shape (T, N, 2) containing the trajectories.
particle_props dict {} Dictionary containing the properties of particles to be simulated as keyword arguments. Valid keys are:

particle_intensity’ : array_like[int, int]
Intensity distribution of particles within a frame given as mean and standard deviations.

intensity_variation’ : int
Intensity variation of particles in subsequent frames given as standard deviation.

z’ : float
Particle positions with respect to the focal plane in pixel units defined by the pixel size in optics_props. For example, particles will be at focus when z=0.

refractive_index’ : float
Refractive index of particle.
optics_props dict {} Dictionary containing the properties of microscope as keyword arguments. Valid keys are:

NA’: float
Numerical aperture of the microscope.

wavelength’ : float
Wavelength of light in meters.

resolution’ : float
Effective pixel size of the camera in meters.

magnification’ : float
Magnification of the optical system.

refractive_index_medium’ : float
Refractive index of the medium sorrounding the particles.

output_region’: array_like[int, int, int, int]
ROI of the image to output.
Given in the format : [x, y, x + width, y + height].
background_props dict {} Dictionary containing properties related to background intensity as keyword arguments. Valid keys are:

background_mean’ : int
Mean background intensity.

backgound_std’ : int
Standard deviation of the background intesity with subsequent frames of a video.
get_vip_particles list [] List of particles for which the masks are needed in the output.
with_masks bool False If True, particle masks are returned in the output along with the video.
If False (default), only the video is returned in the output.
save_video bool False If True, the generated video will be saved at the given path.
path str File path for saving the video, the path should be given along the video format.
For example: ‘path’ = ‘./video.mp4’ will save the video in the current folder.
motion_blur_generator NoneType None
Returns tuple | ndarray Output type I

If with_masks = True,
The function returns a tuple containing:
masks : ndarray
video : ndarray
Note: If get_vip_particles is a non-empty list, the masks will contain only the vip particle masks.

Output type II

If with_masks = False,
The function returns:
video : ndarray
Note: If get_vip_particles is a non-empty list, the first frame in the output will be the masks of the given vip particles in the first frame, else (default) the output will be a ndarray of just the video.

Usage example

Please check tutorials for more detailed examples

Import packages

from andi_datasets.models_phenom import models_phenom

Generating trajectories and passing them through transform_to_video to generate videos

trajs_test, _ = models_phenom().single_state(N = 50, T = 100, L = 128, Ds = 1)
video, masks = transform_to_video(trajs_test,
                                  with_masks=True,
                                  get_vip_particles=np.arange(55).tolist(),
                                  particle_props = {"z": 0},
                                  background_props = {"background_mean": 0,
                                                      "background_std": 0}, 
                                 optics_props = {'origin': [0,0,20,20]})

Play generated video in jupyter notebook

play_video(video)

Plot the first frames of the video and the masks

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
img2 = ax1.imshow(video[1], cmap="gray")
ax1.set_title("Frame 0")
ax2.imshow(masks[1], cmap="gray")
ax2.set_title("Mask 0")
Text(0.5, 1.0, 'Mask 0')

Overlay trajectories on the first frame of the video

plt.figure(figsize=(5, 5))
plt.imshow(video[0], cmap="gray", zorder = -1)
for traj in np.moveaxis(trajs_test, 0, 1):
    plt.plot(traj[:,1], traj[:,0], alpha=0.2)
plt.xlim(0,L); plt.ylim(0,L)
plt.show()

Import TIFF video

/home/gorka/miniconda3/envs/andi/lib/python3.10/site-packages/fastcore/docscrape.py:225: UserWarning: Unknown section Raises
  else: warn(msg)

source

import_tiff_video

 import_tiff_video (tiff_file_path)

Import a TIFF video file as a NumPy array.

This function reads a multi-frame TIFF file and converts each frame into a NumPy array. All frames are stacked along a new axis, resulting in a 3D array if the frames are 2D (or a 4D array if the frames are 3D).

Type Details
tiff_file_path str The file path of the TIFF video file.
Returns numpy.ndarray A NumPy array containing the video frames stacked along a new axis.
The shape of the array is (N, M, O, …) where N is the number of frames,
and M, O, … are the dimensions of each frame.