Skip to content

Tracking Fish

Dave Walker edited this page Nov 1, 2026 · 2 revisions

FishTrack offers two ways to follow fish through a video: Conventional CV, which follows visual appearance, and SAM 2, which predicts the pixels belonging to selected fish. Tracking can start from manual regions or Grounding DINO proposals; Conventional CV can also acquire moving fish during processing. Neither method guarantees that the same individual will be followed correctly throughout a clip.

The two tracking approaches

Conventional CV

The conventional method uses OpenCV to follow the visual appearance of a fish. It looks for a region resembling the fish it has already seen, while using motion estimates and checks on position, size and appearance to reject implausible changes. Once a fish has been acquired, the tracker can sometimes continue following it even when fresh motion detections are unavailable.

A track can begin with a manually drawn rectangle, a reviewed Grounding DINO proposal, or automatic motion acquisition. These are different ways of finding a fish to follow: Grounding DINO proposes boxes only on the first processed frame, while the conventional motion detector looks for independently moving regions during processing. Neither Grounding DINO nor motion acquisition is itself the appearance tracker.

Distinguishing Fish Movement from Camera Movement

Underwater footage often moves because the camera moves, not because everything in the scene is swimming. To estimate this background movement, FishTrack first blurs successive frames, then uses Lucas–Kanade optical flow to follow up to 500 Shi–Tomasi corners—recognisable image details—between frames. A method called RANSAC estimates how the image has shifted, rotated or changed scale so the frames can be aligned.

The documented alignment checks require at least eight valid features, at least 50% inliers (features agreeing with the estimated movement), and an inter-frame scale between 0.8 and 1.25.

FishTrack then compares the aligned previous frame with the current one. It thresholds the differences, tidies the resulting mask using morphological opening and closing, excludes image borders introduced by alignment, and filters candidate regions by area and shape.

A further check compares optical flow inside each candidate with flow in a surrounding area. This helps distinguish a moving fish from a patch of reef moving across the image because of the camera. By default, the candidate needs a coherent residual movement of at least 2 source pixels per frame, and that movement must be at least twice the local background-flow variability.

Sudden scene or lighting changes can produce misleading differences. FishTrack treats a frame as an abrupt change when more than 65% of aligned pixels differ by over 60 grayscale levels; this heuristic can reset tracks. If camera alignment fails, new motion detections are normally suppressed, although already-established visual tracks may continue.

Acquiring and Associating Tracks

A moving region is not immediately treated as a confirmed track. The default setting requires three consecutive detections (--min-hits 3) before its annotation is drawn. This helps reduce fleeting detections caused by noise or moving background details.

To connect observations between frames, FishTrack matches candidate positions one-to-one with existing tracks, accounting for estimated camera movement. The default maximum permitted position disagreement is 60 pixels (--max-distance). Tracks may remain available for up to five consecutive missing frames (--max-missing) before expiring.

Following a Fish by Appearance

After motion acquisition—or immediately after a manual or Grounding DINO starting selection—a sufficiently textured object can be followed by OpenCV's CSRT appearance tracker. Broadly, CSRT searches for an image region that resembles the object it has been following, rather than requiring the motion detector to rediscover it in every frame.

FishTrack also checks predicted movement, distance from the expected position, whether the region remains in the frame, unexpected size changes and similarity to the fish's initial colour appearance (using HSV colour information). The CSRT response threshold defaults to 0.035; this is an algorithm response value, not a probability that the fish has been identified correctly.

When a track is being followed visually rather than supported by a fresh motion detection, FishTrack adds a * after its displayed tracking ID. A carried-forward outline or tracked rectangle approximates the animal's location and shape; unlike a SAM 2 mask, it is not a new pixel-level segmentation. Conventional motion masks represent detected movement rather than reliable silhouettes. If the tracker cannot recover within the missing-frame allowance, the track expires.

Limitations of Conventional CV

Coral texture, low-contrast fish, changes in viewing angle, overlapping animals, parallax (nearby and distant reef features moving differently as the camera moves), and abrupt turns can all confuse motion detection or appearance tracking. The tracker may reject a correct observation, lose the fish or drift onto a similar-looking object.

The pipeline is configurable and useful for experimentation, but it has not been validated as a reliable fish detector. See Finding Fish for the different starting-region methods and Diagnostics and Troubleshooting for interpreting observed failures.


SAM 2: Following the Fish's Shape

SAM 2, developed by Meta, is a video segmentation model. FishTrack uses the SAM 2.1 video predictor to estimate which pixels belong to each selected fish as the video progresses.

More Than Moving a Rectangle

A conventional tracker may move and resize a box, or carry an earlier outline into a new position. SAM 2 instead predicts a new segmentation mask for each frame. A mask identifies the pixels the model believes belong to the fish, allowing its predicted outline to change as the fish turns or bends.

These outlines can be more closely shaped to the fish, but they remain model predictions rather than verified silhouettes.

Selecting a Fish to Follow

SAM 2 requires a starting region. You can draw a rectangle manually or accept a rectangle proposed by Grounding DINO. The accepted selections establish the initial tracking IDs; SAM 2 then attempts to follow those objects through subsequent frames.

Grounding DINO's role ends with the initial proposals. SAM 2 performs the tracking, not Grounding DINO. See Finding Fish for the selection and review process.

What the Model Remembers

SAM 2 uses information from earlier frames to inform later predictions. FishTrack retains the selected IDs as the model predicts masks across the clip.

Sometimes the model produces no mask for a selected fish. A mask may reappear later under the same ID, but that does not prove that the original individual has been found again. Similar fish, occlusion and changes in appearance can all lead to errors.

Model Choices and Preparation

The default SAM 2 model variant is Tiny from the SAM 2.1 family. Other documented choices are small, base-plus and large; each requires a matching configuration and checkpoint. The Tiny checkpoint is approximately 156 MB.

The complete FishTrack installation script prepares the standard models. Once installed, inference takes place locally: video footage is not uploaded for processing.

Clip Length and Memory Requirements

SAM 2 needs an explicit clip duration or frame limit. The application permits up to 240 decoded frames in a run. Longer clips are rejected rather than automatically split, because splitting would interrupt the model's memory of earlier frames.

For processing, FishTrack temporarily prepares high-quality JPEGs for the model and lossless PNGs for rendering. Those files, together with the model's state, may require substantial temporary storage and up to several gigabytes of RAM.

Outlines and Tracking Results

FishTrack draws a SAM 2 outline when the predicted mask is large enough. Empty masks and masks smaller than --sam2-min-area (default 40 pixels) are not drawn. A later frame may again show a mask with the original ID.

SAM 2 has its own tracking logic. Conventional CV settings—including camera-motion compensation, CSRT response thresholds, minimum detection hits and missing-frame tolerances—do not control SAM 2.

Diagnostics and Handling Failures

When enabled, SAM 2 diagnostics include predicted mask images and sam2-statistics.csv with per-frame object status, mask area and bounding box information. See Diagnostics and Troubleshooting for the status values (visible, no_mask and below_min_area) and how to interpret them.

FishTrack checks for missing or out-of-order model predictions. On cancellation or failure, it cleans up temporary staged frames and the incomplete output video.


Which Tracking Method Should I Try?

Conventional CV and SAM 2 are best understood as different experiments on the same problem, rather than a simple ranking of which is better. The best choice depends on what you want to investigate, the footage you have and the resources available.

Aspect Conventional CV SAM 2
Main approach Follows visual appearance, checks movement and can acquire moving fish automatically Uses a video segmentation model with memory of earlier frames
How tracking starts Manual rectangle, reviewed Grounding DINO proposal or automatic motion acquisition Manual rectangle or reviewed Grounding DINO proposal
Typical annotation Tracked rectangle or approximate, carried-forward contour Newly predicted segmentation mask for each frame
Pretrained model weights CSRT requires no pretrained checkpoint Requires a checkpoint matched to the SAM 2 model size
Length of experiment No corresponding 240-frame limit documented Up to 240 decoded frames
Common difficulties Tracking drift, missed detections and changing appearance Incorrect identities, missing masks and greater memory or processing demands

For a comparatively lightweight, explainable experiment, Conventional CV is a useful starting point. If you particularly want an outline that can change with the fish's shape as it swims, SAM 2 is designed for that task. Neither method guarantees that it will continue following the correct individual.

Comparing the Methods Fairly

To investigate the differences yourself, use the same video interval and starting rectangles with both methods. Compare their outputs frame by frame, recording how long regions remain visible, where annotations disappear, whether tracking seems to drift, and how much processing time and memory each method requires.

Remember: more annotations are not necessarily more correct annotations. A tracker that stops when uncertain may be more useful for some investigations than one that continues to follow the wrong object.

For a reproducible comparison, use the same starting selections, record your settings and consult Diagnostics and Troubleshooting when analysing failures.

Clone this wiki locally