4. Find the particles in the images¶
Time: 15 minutes. You need: the my_run folder from page 2.
What this step does¶
Detection finds the bright spots in each image and measures where their centres are. These spots are called targets. A target is only a candidate: it can be a particle, a reflection, or noise. Later steps decide which are real.
The program does three things for every image:
- It removes the slowly changing background (a high-pass filter), so that a bright wall does not look like a particle.
- It marks every pixel brighter than a threshold.
- It groups neighbouring marked pixels into one target. It keeps only the groups with a sensible size. The target centre is the brightness-weighted mean of its pixels, so it is known to a fraction of a pixel.
In the command line, detection and matching (page 5) run together, in one command. This page looks at the detection half.
Do it¶
Process the five frames. This detects the targets and matches them across cameras:
Then count the targets that were found. The results are in my_run/res/run.zarr:
python -c "
from openptv2.storage.run_store import RunStore
store = RunStore('my_run/res/run.zarr', mode='r')
for frame in range(10000, 10005):
print(frame, [len(store.read_targets(cam, frame)) for cam in range(4)])
"
To look at the targets, draw a red cross on each one in the image of camera 1, frame 10000. The picture is saved as a file that you open with any image viewer:
python -c "
import numpy as np
import imageio.v3 as iio
from openptv2.storage.run_store import RunStore
store = RunStore('my_run/res/run.zarr', mode='r')
im = iio.imread('my_run/img/cam1.10000', extension='.tif').astype(float)
rgb = np.clip(np.stack([im] * 3, axis=-1) * 2.5, 0, 255).astype(np.uint8)
targets = store.read_targets(0, 10000)
for t in targets:
x, y = int(round(t.x)), int(round(t.y))
rgb[max(y - 4, 0):y + 5, x] = (255, 60, 60)
rgb[y, max(x - 4, 0):x + 5] = (255, 60, 60)
iio.imwrite('my_run/overlay_cam1.png', rgb)
print('wrote my_run/overlay_cam1.png:', len(targets), 'targets marked')
"
(The image is brightened 2.5 times so that the dim particles show. Camera 1 is 0 in the code, because the code counts from 0.)
- Open the example as on page 2 (Start → Init / Reload).
- Choose Preprocess → Image coord.
Every camera tab now shows a small blue + on each detected target.

The marks are small. In the picture on this page, look for the blue + signs.
What you should see¶
The first command ends with Batch processing completed successfully. The second prints one line per frame: the frame number,
then the number of targets in cameras 1 to 4:
10000 [108, 106, 117, 140]
10001 [116, 108, 114, 140]
10002 [122, 100, 122, 148]
10003 [129, 99, 118, 136]
10004 [117, 92, 109, 139]
This is what the first image looks like with the targets drawn on it:

Left: 108 targets in camera 1. Right: the zoom on the blue box. Each + is the centre of one target.
Check that it worked¶
- The numbers per camera should be similar from frame to frame. Here they are all between 92 and 148. A camera that suddenly finds half as many targets has a problem: an image that is too dark, a lamp that flickered, a cable that moved.
- Look at the picture, not only at the numbers (
my_run/overlay_cam1.pngon the command line). Every bright dot should have a mark. Dim marks on empty background mean the threshold is too low.
The threshold¶
The threshold decides what counts as bright. Here is the same small piece of image with three different thresholds:

The picture is made with a simple background removal, to show the idea. The program uses its own high-pass filter.
- Too low (left): noise becomes targets. Many false targets.
- Too high (right): faint particles are lost, and the real ones get too small.
- Good (middle): one group for each particle.
In the YAML the threshold is gvthres in the targ_rec section, one number per camera (here 4 grey levels on the filtered
image). The size limits are nnmin and nnmax (pixels in a target), nxmin, nxmax, nymin, nymax (width and height) and
sumg_min (minimum total brightness).
In the window, right-click Run1 in the tree and choose Main parameters.
If it did not work¶
| You see | Cause | What to do |
|---|---|---|
| very few targets | threshold too high, or sumg_min too high |
lower gvthres a little and run again |
| many targets on empty background | threshold too low | raise gvthres |
| long streaks become one target, or one particle becomes two | the size limits do not fit your particles | change nnmax and the width and height limits |
| no targets at all in one camera | wrong image folder or name for that camera | check the img_name entries |
Next: 5. Match across cameras →{ .md-button .md-button--primary }