This Python sample may be slow depending on the event rate of the scene and the configuration of the algorithm. We provide it to allow quick prototyping. For better performance, look at the corresponding C++ sample.

Particle Size Measurement using Python

The Analytics API provides algorithms to both count and estimate the size of fast moving objects.

The sample shows how to use the python bindings of Metavision Analytics SDK to count, estimate the size and display the objects passing in front of the camera.

We expect objects to move from top to bottom, as in free-fall. But, command-line arguments allow to rotate the camera 90 degrees clockwise in case of particles moving horizontally in FOV or to specify if objects are going upwards instead of downwards. An object is detected when it crosses one of the horizontal lines (by default, 6 lines spaced 20 pixels apart are used). The number of lines and their positions can be specified using command-line arguments. Detections over the lines are then combined together to get a size estimate when the object has crossed the last line.

The source code of this sample can be found in <install-prefix>/share/metavision/sdk/analytics/python_samples/metavision_psm when installing Metavision SDK from installer or packages. For other deployment methods, check the page Path of Samples.

Expected Output

Metavision Particle Size Measurement sample visualizes the events (from moving objects), the lines on which objects are counted, the detected particles, their estimated sizes and the total object counter:

Setup & requirements

To accurately count and estimate the size of objects, it is very important to fulfill some conditions:

  • the camera should be static and the object in focus

  • there should be good contrast between the background and the objects (using a uniform backlight helps to get good results)

  • set the camera to have minimal background noise (for example, remove flickering lights)

  • the events triggered by an object passing in front of the camera should be clustered as much as possible (i.e. no holes in the objects to avoid multiple detections)

Also, we recommend to find the right objective/optics and the right distance to objects, so that an object size seen by the camera is at least 5 pixels. This, together with your chosen optics, will define the minimum size of the objects you can count.

Finally, depending on the speed of your objects (especially for high-speed objects), you might have to tune the sensor biases to get better data (make the sensor faster and/or less or more sensitive).

How to start

To start the sample based on the live stream from your camera, run:





To start the sample based on recorded data, provide the full path to a RAW file (here, we use a file from our Sample Recordings):


python3 -i 195_falling_particles.raw


python -i 195_falling_particles.raw

To check for additional options:


python3 -h


python -h

Code Overview


Metavision Particle Size Measurement sample implements the following pipeline:


Optional Pre-Processing Filters/Algorithms

To improve the quality of initial data, some pre-processing filters can be applied upstream of the algorithm:


These filters are optional: experiment with your setup to get the best results.

Particle Size Measurement Algorithm

This is the main algorithm in this sample. The algorithm is configured to count objects while they’re passing from top to bottom in front of the camera. To create an instance of metavision_sdk_analytics.PsmAlgorithm, we first need to gather some configuration information, such as the approximate size of the objects to count, their speed and their distance from the camera, to find the right algorithm parameters.

Even though a specific accumulation time might provide a well-defined 2D shape, the algorithm doesn’t directly have access to it because it’s only seeing events through the lines of interest. Ideally, we would like the object to advance one pixel at a time, so that in the end the algorithm could process all the information in this well-defined 2D form. This is however not compatible with a large accumulation time. The accumulation time must therefore be decoupled from the time interval between two line processings. That’s why we have two temporal parameters:

  • Precision time: Time interval between two line processings. Should roughly be equal to the inverse of the object speed (pix/us)

  • Accumulation time: Temporal size of the event-buffers accumulated on the line for a line processing. Should be large enough so that the object shape is well-defined

The lines should be close enough so that there’s no ambiguity when matching together detections done on several lines.

Once we have a valid calibration, we can create an instance of metavision_sdk_analytics.PsmAlgorithm.

The metavision_sdk_analytics.PsmAlgorithm relies on the use of lines of interest to count and estimate the size of the objects passing in front of the camera and produces metavision_sdk_analytics.LineParticleTrackingOutput and metavision_sdk_analytics.LineClusterWithId as output. A metavision_sdk_analytics.LineParticleTrackingOutput contains a global counter and object sizes as well as their trajectories. The global counter is incremented when an object has been successfully tracked over several lines of interest.

The algorithm is implemented in an asynchronous way which allows to retrieve new estimations at a fixed refresh rate rather than getting them for each processed buffer of events. Not only is this more efficient for visualization purpose but it’s also easier to process results only when a new particle has been detected.

Frame Generation

At this step, we generate an image that will be displayed when the sample is running. In this frame are displayed:

  • the events

  • the lines of interest used by the algorithm

  • the global counter

  • the reconstructed object contours and the estimated sizes

The metavision_sdk_core.OnDemandFrameGenerationAlgorithm class allows to buffer input events (i.e. metavision_sdk_core.OnDemandFrameGenerationAlgorithm.process_events()) and generate an image on demand (i.e. metavision_sdk_core.OnDemandFrameGenerationAlgorithm.generate()). Once the event image has been generated, following helpers are called to add overlays:

As the output images are generated at the same frequency as the metavision_sdk_analytics.LineParticleTrackingOutput produced by the metavision_sdk_analytics.PsmAlgorithm, the image generation is done in the metavision_sdk_analytics.PsmAlgorithm’s output callback:

def psm_cb(ts, tracks, line_clusters):
    events_frame_gen_algo.generate(ts, output_img)

    counting_drawing_helper.draw(ts=ts, count=tracks.global_counter, image=output_img)
    detection_drawing_helper.draw(image=output_img, line_clusters=line_clusters)
    tracking_drawing_helper.draw(ts=ts, image=output_img, tracks=tracks)
    particle_sizes = []
    for track in tracks:

    if particle_sizes:
        print(f"At {ts}, the counter is {tracks.global_counter}. New particle sizes (in pix): ["
              + ", ".join(particle_sizes) + "]")

    if args.out_video:

The event processing is done while iterating over metavision_core.event_io.EventsIterator:

    # Process events
    for evs in mv_iterator:
        # Dispatch system events to the window

        # Process events
        if filtering_algorithms:
            filtering_algorithms[0].process_events(evs, events_buf)
            for filter in filtering_algorithms[1:]:

        if window.should_close():


Finally, the generated frame is displayed on the screen. The following image shows an example of output:

Expected Output from Metavision Particle Size Measurement Sample