Python Tutorial

Note

This tutorial is currently in preparation. Please check back later.

This tutorial demonstrates how to use the monochrome Python library to visualize videos and other data in the Monochrome viewer.

Basic Usage

First, import the necessary libraries:

import numpy as np
import monochrome as mc
import optimap as om

Here we use optimap to download a sample video as a numpy array.

filename = om.download_example_data("VF_Rabbit_1.npy")
video = om.load_video(filename)

Display the video in Monochrome using mc.show():

mc.show(video, name="Example Recording")

You can display multiple videos simultaneously. They will play in sync if they have the same length:

# Converting video to np.float32 with values between 0 and 1
video2 = om.video.normalize(video)
print(f"Video shape: {video2.shape}, dtype: {video2.dtype}")

# vmin and vmax are optional and used to set the minimum and maximum values of the colormap
mc.show(video2, name="Another Recording", cmap="viridis", vmin=0.1, vmax=0.9)
mc.show(video2[0], name="First Frame", cmap="gray", vmin=0.1, vmax=0.9)

You can load videos directly from a file path using mc.show() or mc.show_file():

mc.show(filename)  # Equivalent to mc.show_file(filename)

Advanced Usage

Overlays

You can add a video as an overlay to another video using mc.show_layer(). This is useful for comparing different videos or visualizing different aspects of the same video. The overlay will be blended with the parent video using the specified opacity function.

# Create a synthetic overlay
overlay = video.copy()
overlay[:, 32:96, 32:96] = np.nan  # Create a transparent region

# Display the overlay
mc.show_layer(overlay, parent="Example Recording", name="Overlay", cmap="hsv", opacity=0.5)

Optical Flow

You can visualize optical flow using mc.show_flow(). This function takes a 4D numpy array of shape (T, H, W, 2) where T is the number of frames, H and W are the height and width of the video, and the last dimension represents the x and y components of the flow vectors.

First, let’s create some synthetic optical flow data: