fmd_web_logo

Fmd C++ library

v1.0.6

Table of contents

Overview

Fmd (Flow Motion Detector) C++ library is designed for automatic detection of moving objects on videos captured by a moving camera. The library is implemented in C++ (C++17 standard). It is based on dense optical flow with automatic backend selection (OpenCV Farneback CUDA preferred, with NVIDIA VPI OFA / NVENC as a Jetson hardware fallback and OpenCV Farneback CPU as the universal fallback), 4-DOF affine ego-motion compensation, two parallel anomaly detectors over the residual flow (local N-sigma and two-window local contrast), an ensemble step with direction-coherence verification, and an IoU/centroid temporal tracker with hysteresis confirmation. Each instance of the Fmd C++ class processes the frames of one video stream in sequence: the optical flow, the ego-motion model and the tracker use the previous frames. The library inherits its interface from the ObjectDetector (provides interface for object detector) class, offering flexible and customizable parameters. The library depends on open source OpenCV 4.x (linked; Apache 2.0 license for OpenCV 4.5.0 and newer, BSD 3-Clause license for older versions), optionally NVIDIA VPI 3.x (linked when found, proprietary NVIDIA license, hardware-accelerated dense optical flow on Jetson platforms), and the Logger library. The source code of the ObjectDetector library (with the Frame and ConfigReader libraries it depends on) and of the Logger library is not part of the repository (see Build and connect to your project). The library is designed mainly for moving cameras, where conventional background-subtraction approaches fail because the background is non-stationary. It detects independently-moving foreground objects against complex, panning / translating backgrounds. The library uses C++17 standard.

Fmd is general-purpose software: it is not tied to any sector or application, and the developer of a program decides what the program does with it.

Versions

Table 1 - Library versions.

Version Release date What’s new
1.0.0 06.06.2026 First version.
1.0.1 14.07.2026 Performance optimization. Added Dual TV-L1 optical-flow backend (TYPE=3) and FRAME_BUFFER_SIZE optical-flow temporal gap.
1.0.2 31.07.2026 SENSITIVITY remapped to a normalized 0..1 scale (higher = more sensitive) with a re-tuned default — see setParam method. Detection quality improvements. New demo and benchmark applications — see Applications.
1.0.3 12.10.2026 - Submodules updated.
1.0.4 21.10.2026 - Sub-repositories removed: the libraries Fmd depends on are cloned next to the repository or added by the parent project.
- Validation of frames, masks, parameter values and commands: NaN and out-of-range values are rejected, exceptions do not leave the library.
- Hardened build flags; the library is built for the baseline instruction set of the target, without host-specific instructions and fast-math.
- CUSTOM_1 and CUSTOM_3 values <= 0 select the internal defaults; CUSTOM_3 is limited to 1000.
- Compiler warnings fixed, code style and documentation updated.
1.0.5 25.10.2026 - The library links the thread library (CMake target Threads::Threads): programs built on a C library older than glibc 2.34 no longer need -pthread.
1.0.6 21.11.2026 - CMake: hardening flags are checked with -Werror, the build with Clang 15 for AArch64 has no warnings.

Library files

The library is supplied as source code. The user is provided with a set of files in the form of a CMake project (repository). The repository structure is shown below:

CMakeLists.txt --------------------- Main CMake file of the library.
src -------------------------------- Folder with library source code.
    CMakeLists.txt ----------------- CMake file of the library.
    Fmd.h -------------------------- Main library header file.
    FmdVersion.h ------------------- Header file with library version.
    FmdVersion.h.in ---------------- Service CMake file to generate version header.
    Fmd.cpp ------------------------ C++ implementation file (pImpl wrapper).
    impl --------------------------- Folder with pipeline implementation.
        FmdImpl.h ------------------ Orchestrator class header.
        FmdImpl.cpp ---------------- Orchestrator implementation.
        FlowEngine.h --------------- Optical flow backend abstraction header.
        FlowEngine.cpp ------------- VPI / Farneback backend selection logic.
        MotionCompensator.h -------- 4-DOF affine ego-motion compensation header.
        MotionCompensator.cpp ------ Ego-motion compensation implementation.
        AnomalyNSigma.h ------------ Local N-sigma anomaly detector header.
        AnomalyNSigma.cpp ---------- Local N-sigma anomaly detector implementation.
        AnomalyLocalContrast.h ----- Local-contrast anomaly detector header.
        AnomalyLocalContrast.cpp --- Local-contrast anomaly detector implementation.
        Ensemble.h ----------------- Ensemble verification and scoring header.
        Ensemble.cpp --------------- Ensemble verification and scoring implementation.
        Clustering.h --------------- Hierarchical motion-feature clustering header.
        Clustering.cpp ------------- Hierarchical clustering implementation.
        TemporalTracker.h ---------- IoU/centroid temporal tracker header.
        TemporalTracker.cpp -------- Temporal tracker implementation.
        FlowUtils.h ---------------- Shared flow utilities header.
        FlowUtils.cpp -------------- Shared flow utilities implementation.
example ---------------------------- Folder for example application.
    CMakeLists.txt ----------------- CMake file of example application.
    main.cpp ----------------------- Source C++ file of example application.
demo ------------------------------- Folder for demo application (viewer).
    CMakeLists.txt ----------------- CMake file of demo application.
    main.cpp ----------------------- Source C++ file of demo application.
benchmark -------------------------- Folder for benchmark application.
    CMakeLists.txt ----------------- CMake file of benchmark application.
    main.cpp ----------------------- Source C++ file of benchmark application.
scripts ---------------------------- Folder with helper shell scripts.
    build_opencv_cuda.sh ----------- Builds OpenCV with the CUDA optical-flow module.
    perf-run.sh -------------------- Raises Jetson clocks (optional, Jetson only).
static ----------------------------- Folder with static assets (logo, etc.).

The three applications are built automatically when Fmd is the top-level CMake project and are skipped when it is included as a subrepository. See Applications.

Key features and capabilities

Table 2 - Key features and capabilities.

Parameter and feature Description
Programming language C++ (standard C++17).
Supported OS Compatible with any operating system that supports the C++ compiler (C++17 standard) and OpenCV 4.x (Linux on x86-64, Jetson and other ARM platforms, Windows, macOS). Built and tested on Linux (x86-64, aarch64) and Windows (x64). VPI is used on Linux (Jetson) only. The optical-flow backend chosen at runtime depends on hardware availability — see Hardware and platform support.
Shape of detected objects The library is able to detect moving objects of any shape. The minimum and maximum height and width of the objects to be detected are set by the user in the library parameters.
Supported pixel formats RGB24, BGR24, GRAY, YUV24, YUYV, UYVY, NV12, NV21, YV12, YU12. The library uses the GRAY format for processing. If the pixel format of the input frame is different from GRAY, the library pre-converts it to GRAY.
Maximum and minimum video frame size The width and the height of video frames (and of the detection mask) must be 1..8192 pixels; detect() returns FALSE for other sizes. The size of the video frames has a significant impact on the computation time and on the memory used.
Coordinate system The algorithm uses a window coordinate system with the zero point in the upper left corner of the video frame.
Calculation speed Processing time per frame scales roughly linearly with pixel count and depends on the platform and the selected optical-flow backend. Reference measurement on a Jetson Orin NX Super (MAXN_SUPER, production Farneback CUDA backend): ≈13 ms at 640x512 (≈75 FPS), ≈42 ms per megapixel, so sustained 30 FPS holds up to about 1024x768. The VPI backend is a no-CUDA fallback and is slower (31-63 ms at 640x512). Measure your own platform with the benchmark application.
Discreteness of computed coordinates The algorithm calculates the object bounding box for each detected object. The increment for position and size of the bounding box is 1 pixel.
Detection algorithm Dense optical flow → 4-DOF affine ego-motion compensation → two parallel anomaly detectors over the residual flow → ensemble verification (direction coherence + magnitude uniformity) → IoU/centroid temporal tracker with hysteresis confirmation. Object velocity is reported in a camera-stabilized reference frame (ego-motion removed).
Working conditions Designed mainly for moving cameras. The 4-DOF (translation + rotation + uniform scale) ego-motion model handles typical camera pan / roll / forward motion. Rapidly changing camera depth (strong zooming, fast forward translation through cluttered scene) reduces accuracy. It is recommended to use the example application to evaluate the quality of the algorithm in specific situations.

Hardware and platform support

The library compiles and runs on any platform that provides a C++17 toolchain and OpenCV 4.x. Optical-flow performance, however, depends on which hardware-accelerated backends are available at build time. The CMake configuration detects them automatically and sets the corresponding preprocessor flags inside the Fmd target:

CMake check Preprocessor flag Code paths enabled
find_package(vpi 3.0) FMD_HAVE_VPI=1 VPI OFA / NVENC dense optical flow.
TARGET opencv_cudaoptflow FMD_HAVE_OPENCV_CUDA=1 cv::cuda::FarnebackOpticalFlow (GPU).
opencv2/optflow.hpp present FMD_HAVE_OPENCV_OPTFLOW=1 cv::optflow::DualTVL1OpticalFlow (TYPE=3).
(none required) — cv::calcOpticalFlowFarneback (CPU).

When a flag is not defined the corresponding backend code is replaced with a stub that returns “backend not compiled in”, so the same source tree builds cleanly on any platform — the runtime selection logic simply skips unavailable paths.

The optical-flow backend is selected automatically by the first optical flow computation (the detect() call number FRAME_BUFFER_SIZE + 1 after a start or a reset) in the following preference order:

Priority Backend Notes
1 Farneback CUDA OpenCV CUDA Farneback. Preferred when opencv_cudaoptflow is built — it dominates VPI on both speed and quality.
2 VPI OFA Jetson Orin dedicated Optical Flow Accelerator. No-CUDA fallback.
3 VPI NVENC Encoder-based dense optical flow (older Jetson). No-CUDA fallback.
4 Farneback CPU Universal fallback. Always available.

Table — typical platform matrix.

Platform VPI CUDA Farneback CPU Farneback Auto-pick
NVIDIA Jetson Orin — JetPack 6 OFA (HW) optional, requires custom OpenCV build with CUDA yes Farneback CUDA if built, else VPI OFA
Older Jetson (Xavier, Nano-Maxwell) — JetPack 5 NVENC (HW) optional yes Farneback CUDA if built, else VPI NVENC
x86 Linux + NVIDIA GPU + OpenCV CUDA build no yes yes Farneback CUDA
x86 Linux + system OpenCV (no CUDA) no no yes Farneback CPU
Windows + NVIDIA GPU + OpenCV CUDA build no yes yes Farneback CUDA
Windows + prebuilt OpenCV no no yes Farneback CPU
macOS / ARM Linux (non-Jetson) no no yes Farneback CPU

To force a specific backend, set the TYPE parameter through setParam:

TYPE value Meaning
-1 Auto (default).
0 VPI (OFA / NVENC, whichever works).
1 OpenCV Farneback (CPU).
2 OpenCV Farneback (CUDA).
3 OpenCV Dual TV-L1. Requires the opencv_contrib optflow module; falls back to Farneback CPU when it is not built. Never chosen by Auto.

getParam(TYPE) returns the actually selected backend after the first optical flow computation: 0 for any VPI backend, 1 for Farneback CPU, 2 for Farneback CUDA, 3 for Dual TV-L1, -1 if no backend is initialized (before the first optical flow computation, and after a reset of the pipeline until the next optical flow computation; the pipeline is reset by the first detect() call that processes a frame after RESET, OFF, a change of TYPE or of FRAME_BUFFER_SIZE, and by a change of the frame size). If VPI is requested (TYPE 0) and is not available, no optical flow is computed: detect() returns TRUE with an empty objects list and getParam(TYPE) returns -1.

Supported pixel formats

The Frame library (a dependency of the ObjectDetector library) contains the Fourcc enum which defines supported pixel formats (Frame.h file). Fmd processes RAW pixel formats only (the compressed JPEG, H264 and HEVC entries are not supported). The library uses the intensity (GRAY) channel for processing; if the input frame is in another supported format it is pre-converted to GRAY. Fourcc enum declaration:

enum class Fourcc
{
    /// RGB 24bit pixel format.
    RGB24 = MAKE_FOURCC_CODE('R', 'G', 'B', '3'),
    /// BGR 24bit pixel format.
    BGR24 = MAKE_FOURCC_CODE('B', 'G', 'R', '3'),
    /// YUYV 16bits per pixel format.
    YUYV  = MAKE_FOURCC_CODE('Y', 'U', 'Y', 'V'),
    /// UYVY 16bits per pixel format.
    UYVY  = MAKE_FOURCC_CODE('U', 'Y', 'V', 'Y'),
    /// Grayscale 8bit.
    GRAY  = MAKE_FOURCC_CODE('G', 'R', 'A', 'Y'),
    /// YUV 24bit per pixel format.
    YUV24  = MAKE_FOURCC_CODE('Y', 'U', 'V', '3'),
    /// NV12 pixel format.
    NV12  = MAKE_FOURCC_CODE('N', 'V', '1', '2'),
    /// NV21 pixel format.
    NV21  = MAKE_FOURCC_CODE('N', 'V', '2', '1'),
    /// YU12 (YUV420) - Planar pixel format.
    YU12 = MAKE_FOURCC_CODE('Y', 'U', '1', '2'),
    /// YV12 (YVU420) - Planar pixel format.
    YV12 = MAKE_FOURCC_CODE('Y', 'V', '1', '2'),
    /// JPEG compressed format.
    JPEG  = MAKE_FOURCC_CODE('J', 'P', 'E', 'G'),
    /// H264 compressed format.
    H264  = MAKE_FOURCC_CODE('H', '2', '6', '4'),
    /// HEVC compressed format.
    HEVC  = MAKE_FOURCC_CODE('H', 'E', 'V', 'C')
};

Bytes layout of supported RAW pixel formats. Example of a 4x4 pixels image.

yuvYUV24 grayGRAY
yuyvYUYV uyvyUYVY
nv12NV12 nv21NV21
yu12YU12 yv12YV12

For planar luminance formats (GRAY, NV12, NV21, YV12, YU12) Fmd copies the Y plane without color conversion and does not read the chroma data — these are the most efficient input formats.

Library principles

The object detection algorithm consists of the following sequential steps executed on every detect() call:

  1. Acquire the source video frame and convert it to GRAY format (grayscale).
  2. Compute dense optical flow between the previous and the current grayscale frame. The backend is selected automatically (VPI OFA / NVENC / Farneback CUDA / Farneback CPU).
  3. Estimate a 4-DOF affine model (translation + rotation + uniform scale) of the camera ego-motion. The model is fit on spatially-distributed corner feature points with two-stage outlier rejection (exclusion regions from confirmed tracks + textured-background median flow).
  4. Compute the residual flow — per-pixel optical flow with the affine prediction subtracted. Pixels belonging to the static background have near-zero residual; independently moving objects produce non-zero residual.
  5. Run two anomaly detectors over the residual flow in parallel:
    • Local N-sigma: per-pixel adaptive threshold over a sliding window of the residual magnitude.
    • Local Contrast: two-window approach — magnitude minus its local mean, then an adaptive threshold on the resulting contrast map.
  6. Combine the two detector masks via an ensemble step: per-component verification by flow direction coherence and magnitude uniformity (hard filter), followed by additive scoring ((NSigma weight + LocalContrast weight) × cluster factor) and merge of co-directional overlapping detections.
  7. Pass the verified detections to an IoU/centroid temporal tracker with hysteresis confirmation and a hard motion check. Positions are accumulated in a camera-stabilized reference frame derived from the per-frame affine ego-motion, so velocity and displacement measurements isolate object motion from camera motion.
  8. The resulting confirmed objects can be retrieved using the getObjects() method. The pipeline is one frame deep: after detect() of frame N they are the objects of frame N-1 (see detect method).

The library is available as source code. To use the library as source code, developers must incorporate the library files into their project. The usage sequence is:

  1. Include the library files in the project.
  2. Create an instance of the Fmd class. For multiple parallel cameras, create multiple instances.
  3. If necessary, modify the default library parameters using the setParam(…) method.
  4. Create a Frame class object for the input frame.
  5. Call the detect(…) method to identify objects on each frame.
  6. Retrieve the detected objects using the getObjects() method.

Fmd class description

Fmd class declaration

Fmd class declared in Fmd.h file. Fmd inherits from the ObjectDetector interface and lives in the rapidpixel namespace. All public methods are thread-safe: they can be called concurrently from different threads on one object (concurrent detect() calls are serialized). The methods do not throw exceptions: an internal failure (for example out of memory) is returned as the failure value of the method; only the constructor throws std::bad_alloc if the memory runs out. Class declaration:

class Fmd : public ObjectDetector
{
public:

    /// Get library version.
    static std::string getVersion();

    /// Class constructor.
    Fmd();

    /// Class destructor.
    ~Fmd() override;

    /// The detector is not copyable or movable.
    Fmd(const Fmd&) = delete;
    Fmd(Fmd&&) = delete;
    Fmd& operator= (const Fmd&) = delete;
    Fmd& operator= (Fmd&&) = delete;

    /// Init object detector with the given parameters.
    bool initObjectDetector(ObjectDetectorParams& params) override;

    /// Set parameter value.
    bool setParam(ObjectDetectorParam id, float value) override;

    /// Get parameter value.
    float getParam(ObjectDetectorParam id) override;

    /// Get full parameters snapshot.
    void getParams(ObjectDetectorParams& params) override;

    /// Get list of detected objects from the last detect() call.
    std::vector<Object> getObjects() override;

    /// Execute command.
    bool executeCommand(ObjectDetectorCommand id) override;

    /// Perform detection on a single video frame.
    bool detect(rapidpixel::Frame& frame) override;

    /// Set detection mask. Pixels with value 0 are ignored.
    bool setMask(rapidpixel::Frame mask) override;

    /// Decode binary command and execute it.
    bool decodeAndExecuteCommand(uint8_t* data, int size) override;
};

getVersion method

The getVersion() method returns string of current version of Fmd class. Method declaration:

static std::string getVersion();

Method can be used without Fmd class instance:

std::cout << "Fmd class version: " << rapidpixel::Fmd::getVersion() << std::endl;

Console output:

Fmd class version: 1.0.6

initObjectDetector method

The initObjectDetector(…) method initializes the detector with a complete ObjectDetectorParams structure. Every supported field (see Table 3) is checked with the validation rules of setParam(...) first; only if all of them are valid they are applied in the order of the table, and initString and classNames are stored. Method declaration:

bool initObjectDetector(ObjectDetectorParams& params) override;
Parameter Description
params ObjectDetectorParams structure (see ObjectDetectorParams class description).

Returns: TRUE if the parameters were applied, FALSE if a field has an invalid value (the detector is not changed then).

Calling it is optional: a newly constructed Fmd already holds the defaults of Table 3 and is ready to detect. Use this method when you want to apply your own configuration — the values in the structure are yours to choose. A default-constructed ObjectDetectorParams carries the generic defaults of the shared interface, which do not suit Fmd: sensitivity is 0.04 (almost nothing is detected) and type is 0 (VPI: where VPI is not available no optical flow is computed and nothing is detected). Start from getParams(…) of a constructed detector or set these fields explicitly.

setParam method

The setParam(…) method sets a new parameter value. Method declaration:

bool setParam(ObjectDetectorParam id, float value) override;
Parameter Description
id Parameter ID according to the ObjectDetectorParam enum (see ObjectDetectorParam enum).
value Parameter value. Valid range depends on the parameter ID — see Table 3 below.

Returns: TRUE if the parameter was set, FALSE if the id is unsupported, the value is not valid (NaN, out of the valid range of Table 3, or a value of an integer parameter outside of the int range) or the parameter is read-only (PROCESSING_TIME_MCS). If FALSE is returned the detector is not changed.

Table 3 - Fmd parameter mapping. Some standard fields of ObjectDetectorParams have a Fmd-specific interpretation (noted in the Notes column). Integer parameters use the integer part of the value (for example 2.7 sets 2).

Parameter Access Valid range Default Notes
LOG_MODE read / write 0..3 0 Logger destination. 0 - disable, 1 - file only, 2 - console only, 3 - console + file. The library logs only messages of the optical-flow backend selection. A log file is written only after the application has configured the file output of the Logger library (rapidpixel::Logger::setSaveLogParams(...)).
FRAME_BUFFER_SIZE read / write 1..16 1 Fmd-specific meaning: optical-flow temporal gap. Dense flow is computed between the current frame and the frame N steps back (Fmd buffers the last N grayscale frames). 1 (default) = consecutive frames. N>1 widens the baseline so slow objects whose per-frame displacement sits near the flow noise floor become detectable — this is what the parameter exists for. It is scene-dependent, see Choosing FRAME_BUFFER_SIZE. Values below 1 are rejected, values above 16 set 16. Changing it re-primes the frame history, so the next few frames produce no output.
MIN_OBJECT_WIDTH read / write 1..8192 4 Output filter: drop detected objects with width < value.
MAX_OBJECT_WIDTH read / write 1..8192 128 Output filter: drop detected objects with width > value.
MIN_OBJECT_HEIGHT read / write 1..8192 4 Output filter: drop detected objects with height < value.
MAX_OBJECT_HEIGHT read / write 1..8192 128 Output filter: drop detected objects with height > value.
MIN_X_SPEED read / write 0.0.. 0.0 Output filter: drop detected objects with \|vX\| < value (pixels/frame, in stabilized reference frame).
MAX_X_SPEED read / write 0.0.. 30.0 Output filter: drop detected objects with \|vX\| > value.
MIN_Y_SPEED read / write 0.0.. 0.0 Output filter: drop detected objects with \|vY\| < value.
MAX_Y_SPEED read / write 0.0.. 30.0 Output filter: drop detected objects with \|vY\| > value.
MIN_DETECTION_PROBABILITY read / write 0.0..1.0 0.0 Stored but not applied: Fmd reports Object::p = 1.0 for confirmed tracks.
X_DETECTION_CRITERIA read / write 1..256 20 Score-sum threshold to confirm a track. The tracker’s internal confirmThreshold is set to max(X_DETECTION_CRITERIA, Y_DETECTION_CRITERIA).
Y_DETECTION_CRITERIA read / write 1..256 20 See X_DETECTION_CRITERIA.
RESET_CRITERIA read / write 1..256 3 A confirmed track is dropped when it has more consecutive unmatched frames than this value (tracker’s maxMissed); a track that is not confirmed yet is dropped after more than 3.
SENSITIVITY read / write 0.0..1.0 0.70 Normalized detection sensitivity, higher = more sensitive (more detections, more false positives). Internally it is mapped linearly onto the N-sigma threshold shared by both anomaly detectors: nSigma = 6.0 - value * 5.0, so 0.0 is the strictest useful setting and 1.0 the most sensitive. The default 0.70 (N-sigma 2.5) is the operating point the library is tuned at. See Choosing SENSITIVITY.
SCALE_FACTOR read / write 1 1 Fmd does not downsample internally. Only the value 1 is accepted; use upstream resizing if you need lower-resolution processing.
NUM_THREADS read / write 0.. 1 Accepted for interface compatibility; Fmd relies on OpenCV / VPI internal threading, so this value is not consulted directly.
PROCESSING_TIME_MCS read only — — Last detect() call duration, microseconds.
TYPE read / write -1..3 -1 Fmd-specific meaning: optical-flow backend. -1 - auto, 0 - VPI, 1 - Farneback CPU, 2 - Farneback CUDA, 3 - Dual TV-L1 (requires the opencv_contrib optflow module; falls back to Farneback CPU otherwise, and is never chosen by Auto; it runs on the OpenCL GPU device of OpenCV when one is available, and OpenCV then keeps a cache of the compiled OpenCL kernels, on Linux by default in ~/.cache/opencv). Reading this parameter returns the actually selected backend after the first optical flow computation (see Hardware and platform support).
MODE read / write 0/1 1 0 - off (detect returns true with empty objects), 1 (any value with a non-zero integer part) - on. Switching off triggers a pipeline reset.
CUSTOM_1 / CUSTOM_2 / CUSTOM_3 read / write CUSTOM_1, CUSTOM_2: any; CUSTOM_3: <= 1000 0.0 Fmd-specific tuning levers, each applied only when set > 0 (values <= 0 select the internal default): CUSTOM_1 — minimum net displacement, pixels (default 3): the stabilized-frame motion floor a track must exceed before it can confirm (suppresses quasi-stationary clutter); CUSTOM_2 — cluster reference area used by the co-moving-group penalty, pixels (default 150; smaller = stronger penalty); CUSTOM_3 — score-history window length, frames (default 13; the integer part is used; longer rewards sustained detections over brief flickers). NaN is rejected.

Choosing SENSITIVITY

SENSITIVITY is the one parameter worth tuning per scene. It sets how far a local flow residual has to stand out from its neighbourhood before it can become a detection, so it trades recall against false positives:

SENSITIVITY Internal N-sigma Behaviour
0.0 6.0 Strictest. Only very prominent motion survives; slow / small objects are lost.
0.4 4.0 Conservative — few false positives, reduced recall.
0.64 2.8 Slightly stricter than the default.
0.70 2.5 Default. The operating point the library is tuned and validated at.
0.8 2.0 More sensitive; useful when a known object is being missed and the scene is clean.
1.0 1.0 Most sensitive. Expect a large number of false positives.

Recommended practice: start at the default, then move in steps of 0.05 while watching both the objects of interest and the false-positive rate on your own footage — the demo application has a live slider for exactly this.

Note that the response is not monotonic in false positives. Around the default, a more sensitive setting can produce fewer false positives: the extra true detections give the clustering and the tracker a consistent object to lock onto, instead of confirming short-lived clutter. On some test videos the default gives more recall, fewer false positives and more sustained tracks than the stricter 0.64; on videos with strong static clutter the opposite may hold, so verify on your own material rather than assuming a direction.

A newly constructed Fmd already uses the default above, so you only need to touch this parameter to tune for a scene. Note that a default-constructed ObjectDetectorParams carries the generic defaults of the shared interface rather than these — sensitivity there is 0.04, which maps to a threshold almost nothing passes — so set the field explicitly when you configure the detector through initObjectDetector.

Choosing FRAME_BUFFER_SIZE

FRAME_BUFFER_SIZE is the optical-flow temporal gap, and it exists for one specific problem: slow objects. An object that moves only a fraction of a pixel between two consecutive frames produces a residual flow below the noise floor of the flow field itself, so no threshold can separate it from the background. Computing the flow against the frame N steps back multiplies that displacement by N and lifts the object above the noise.

The gain can be large, but it is scene-dependent and not free — the wider baseline also accumulates more ego-motion and parallax, so background residuals grow with it. On the test videos, raising the gap from 1 to 3 on the clip with the slowest, smallest objects more than tripled recall (0.17 → 0.60) and turned an intermittent track into a sustained one, while on clips whose objects already move a pixel or more per frame the same setting cut recall roughly in half and multiplied false positives by two to four.

Practical guidance:

Situation Suggested gap
Objects visibly move between consecutive frames (a pixel or more per frame) 1 (default)
Objects creep — a few tenths of a pixel per frame (small apparent motion, high frame rate) 2..3
Extremely slow relative motion, static-looking object on a moving background 4..8, expect many more false positives

Choose it per scene, not globally: the demo application has a live Flow gap slider, so the quickest way to pick a value is to watch your own footage while moving it. Keep in mind that a higher gap also delays the first output after a start, a reset or a change of this parameter, because the frame history has to refill.

getParam method

The getParam(…) method retrieves a parameter value. Method declaration:

float getParam(ObjectDetectorParam id) override;
Parameter Description
id Parameter ID according to ObjectDetectorParam enum.

Returns: the parameter value, or -1 if the id is unsupported. TYPE returns the selected optical-flow backend (see Hardware and platform support), PROCESSING_TIME_MCS the duration of the last detect() call.

getParams method

The getParams(…) method copies the full ObjectDetectorParams snapshot, including the most recent objects list and the processing time of the last detect() call. The field type holds the requested backend (-1 for auto); getParam(TYPE) returns the selected one. Method declaration:

void getParams(ObjectDetectorParams& params) override;
Parameter Description
params Output ObjectDetectorParams structure.

executeCommand method

The executeCommand(…) method executes a control command. Method declaration:

bool executeCommand(ObjectDetectorCommand id) override;
Parameter Description
id Command ID according to ObjectDetectorCommand enum (see ObjectDetectorCommand enum).

Returns: TRUE if the command was accepted, FALSE otherwise.

Table 4 - Command behavior.

Command Description
RESET Resets the optical-flow engine, the ego-motion compensator, the temporal tracker, and the cached previous frame. The next detect() call will be a priming call (returns TRUE with an empty objects list).
ON Enables the detector (equivalent to setParam(MODE, 1)).
OFF Disables the detector (equivalent to setParam(MODE, 0)). While disabled, detect() returns TRUE but reports an empty objects list; a tracker reset is also scheduled so that re-enabling produces a clean state.

detect method

The detect(…) method performs detection on a single video frame. Method declaration:

bool detect(rapidpixel::Frame& frame) override;
Parameter Description
frame Source video frame (Frame class). Supported pixel formats: GRAY, NV12, NV21, YV12, YU12 (only the Y plane is read), BGR24, RGB24, YUV24, UYVY, YUYV. Width and height must be 1..8192 pixels. size must be at least the size of the data that is read: width × height for the formats with a Y plane, width × height × 2 for UYVY and YUYV, width × height × 3 for BGR24, RGB24 and YUV24. data must not be nullptr and must point to at least size bytes (the library cannot check this). The data is not changed.

Returns: TRUE if the frame was processed successfully (note: returns TRUE on the very first frame too, with an empty objects list — that frame is used to prime the optical-flow buffer) or if the detector is off (MODE 0, empty objects list). Returns FALSE on an invalid frame (see above; the frame history is not changed then) or if the processing failed, for example because the memory ran out (the pipeline restarts from scratch with the next frame, as after RESET). After FALSE the objects list is empty.

After detect() the list of confirmed objects is available via getObjects().

One frame of output latency. Internally the pipeline is one frame deep: the optical flow of frame N is computed on a worker while the rest of the pipeline processes frame N-1, which is what makes the library keep up on embedded platforms. As a consequence getObjects() after detect(frame N) returns the objects of frame N-1. Every returned object carries the frame it belongs to in Object::frameId, so consumers that key on frameId need no change. Code that overlays the boxes on “the frame just passed in” will draw them one frame ahead of their objects — overlay them on the previous frame instead (the example application shows the pattern). The very first detect() call has nothing pending yet and returns an empty list.

setMask method

The setMask(…) method sets a detection mask. Pixels of the mask equal to 0 are ignored — detections whose centers fall inside those regions are dropped before tracking. Method declaration:

bool setMask(rapidpixel::Frame mask) override;

Note. A band of 40 pixels along every border of the frame (frames larger than 80 × 80 pixels) is excluded from the residual flow, because the optical flow is unreliable at the frame edges: an object inside this band is not detected, an object that crosses the edge of the band is detected with its part outside of the band. Other regions that produce false positives (overlays, OSD, fixed borders inside the image) are not excluded automatically — use this mask to exclude them.

Parameter Description
mask Binary mask (Frame object). Supported pixel formats: GRAY, NV12, NV21, YV12, YU12. Only the Y plane is read. Width and height must be 1..8192 pixels and size must be at least width × height. The mask is passed by value: the copy constructor of Frame copies size bytes of the data before the call (data must point to at least size bytes), and a mask without data (nullptr) arrives as an all-zero mask. The mask is scaled to the size of the frames; a mask without non-zero pixels excludes the whole frame.

Returns: TRUE if the mask was accepted, FALSE on an invalid mask or an unsupported pixel format (the previous mask is kept then).

decodeAndExecuteCommand method

The decodeAndExecuteCommand(…) method decodes a binary command (produced by ObjectDetector::encodeSetParamCommand(...) or ObjectDetector::encodeCommand(...)) and executes it via setParam or executeCommand. Method declaration:

bool decodeAndExecuteCommand(uint8_t* data, int size) override;
Parameter Description
data Pointer to a buffer with the encoded command. Must point to at least size bytes.
size Size of the buffer in bytes.

Returns: TRUE if the command was decoded and executed, FALSE otherwise. ObjectDetector::decodeCommand(...) checks the pointer, the version, the exact size of the command and the IDs; the decoded value is checked by setParam(…).

Data structures

ObjectDetectorCommand enum

enum class ObjectDetectorCommand
{
    RESET = 1,
    ON,
    OFF
};

See executeCommand method.

ObjectDetectorParam enum

enum class ObjectDetectorParam
{
    LOG_MODE = 1,
    FRAME_BUFFER_SIZE,
    MIN_OBJECT_WIDTH,
    MAX_OBJECT_WIDTH,
    MIN_OBJECT_HEIGHT,
    MAX_OBJECT_HEIGHT,
    MIN_X_SPEED,
    MAX_X_SPEED,
    MIN_Y_SPEED,
    MAX_Y_SPEED,
    MIN_DETECTION_PROBABILITY,
    X_DETECTION_CRITERIA,
    Y_DETECTION_CRITERIA,
    RESET_CRITERIA,
    SENSITIVITY,
    SCALE_FACTOR,
    NUM_THREADS,
    PROCESSING_TIME_MCS,
    TYPE,
    MODE,
    CUSTOM_1,
    CUSTOM_2,
    CUSTOM_3
};

See Table 3 for the Fmd-specific meaning of each parameter.

Object structure

struct Object
{
    int id{0};            // stable track ID
    int frameId{0};       // input frame.frameId of the frame the object was detected on
    int type{0};          // object class — always 0 for Fmd (no classifier)
    int width{0};         // bounding box width, pixels
    int height{0};        // bounding box height, pixels
    int x{0};             // bounding box top-left x, pixels (image coords)
    int y{0};             // bounding box top-left y, pixels
    float vX{0.0f};       // horizontal velocity, pixels/frame, stabilized
    float vY{0.0f};       // vertical velocity, pixels/frame, stabilized
    float p{0.0f};        // detection probability (always 1.0 for Fmd confirmed tracks)
};

The vX and vY components are measured in a camera-stabilized reference frame: the per-frame affine ego-motion estimated by the detector is removed before differencing positions, so they reflect motion of the object relative to the static scene, not relative to the moving camera. The reference frame is the first frame after a start or a reset, and the velocities are in its pixels: when the camera zooms (or moves forward), the scale since that frame scales the velocities and the motion floor of the confirmation (CUSTOM_1), so a long sustained zoom makes slow objects appear slower and harder to confirm (a RESET starts a new reference frame).

The box (x, y, width, height) is in the pixels of the input frame. While a confirmed track is not detected (for up to RESET_CRITERIA frames) its box moves with the last measured velocity, so a box can lie partly outside of the frame (x or y can be negative): clip it to the frame before use.

ObjectDetectorParams class description

ObjectDetectorParams is defined by the ObjectDetector interface library. See its documentation for the complete declaration, the JSON_READABLE macro, and the encode() / decode() serialization methods. Fmd maps the subset of fields listed in Table 3 onto its internal pipeline knobs; fields that are not in that table are stored verbatim but do not influence the algorithm.

Build and connect to your project

Before compiling you have to install OpenCV for your system. VPI is optional and is detected automatically at configure time when its CMake config is on the search path.

Installation on Linux:

sudo apt-get install -y build-essential cmake libopencv-dev

For hardware-accelerated dense optical flow on NVIDIA Jetson Orin platforms, install VPI 3.x from the JetPack repository. On x86 Linux desktops VPI is typically not used for dense flow, so the library falls back to Farneback automatically.

Jetson note. The fastest path on Jetson is still Farneback CUDA (see Hardware and platform support) — VPI is the fallback when OpenCV is built without CUDA. On a Jetson the default governor also keeps some engines below their maximum, which costs both latency and repeatability; scripts/perf-run.sh (needs sudo) raises CPU/GPU/EMC clocks via jetson_clocks and additionally pins the OFA and VIC engines that jetson_clocks leaves alone. It changes sysfs only, does not touch nvpmodel, and resets on reboot. This matters most for a VPI deployment, where it cuts 17-30% of the per-frame time.

Installation on Windows:

  1. Download a prebuilt OpenCV 4.x release for Windows and extract it to any folder (recommended path C:/libs/openCV_4.x.x).
  2. Add path to OpenCV to Windows system variables. Settings → System → Advanced system settings → Environment Variables.
  3. Create a new variable in System variables with name OpenCV_DIR and path to your OpenCV build folder (for example C:/libs/openCV_4.12.0/build) and save changes. With Visual Studio 2026 the configuration in the build folder does not recognize the compiler and CMake stops with “Found OpenCV Windows Pack but it has no binaries compatible with your configuration”: then set OpenCV_DIR to the folder of the libraries, for example C:/libs/openCV_4.12.0/build/x64/vc16/lib.
  4. Optionally add the folder of the DLLs (for example C:/libs/openCV_4.12.0/build/x64/vc16/bin) to PATH so applications find opencv_*.dll at runtime.
  5. A Windows reboot is sometimes required.

VPI is not officially available on Windows; on Windows the library always uses one of the Farneback backends.

Enabling the CUDA optical-flow backend (recommended):

The production backend is OpenCV Farneback CUDA (opencv_cudaoptflow module). Stock distributions — apt libopencv-dev and the prebuilt Windows release — do not include the CUDA modules, so with them the library silently falls back to CPU Farneback. To get the GPU backend, build OpenCV ≥ 4.9.0 (4.8 fails to build the CUDA modules against CUDA 12.x) together with opencv_contrib and the cudaoptflow module.

Prerequisites: an NVIDIA GPU, a recent NVIDIA driver, and the CUDA Toolkit 12.x (nvcc). Set CUDA_ARCH_BIN to the GPU’s compute capability:

GPU CUDA_ARCH_BIN
RTX 20xx (Turing) 7.5
RTX 30xx (Ampere) 8.6
RTX 40xx (Ada) 8.9
Jetson Orin 8.7

Linux — a helper script is provided (scripts/build_opencv_cuda.sh). Stage the sources, then run it with ARCH set to your CUDA_ARCH_BIN:

git clone --branch 4.10.0 --depth 1 https://github.com/opencv/opencv.git         ~/opencv_src/opencv
git clone --branch 4.10.0 --depth 1 https://github.com/opencv/opencv_contrib.git ~/opencv_src/opencv_contrib
ARCH=8.9 bash scripts/build_opencv_cuda.sh

It installs a self-contained OpenCV to ~/opencv-cuda and leaves the system OpenCV untouched. Point Fmd at it:

cmake -S . -B build -DOpenCV_DIR=$HOME/opencv-cuda/lib/cmake/opencv4

Windows — build OpenCV + opencv_contrib with CMake and Visual Studio:

cmake -S opencv -B opencv_build -G "Visual Studio 17 2022" -A x64 ^
  -D CMAKE_INSTALL_PREFIX=C:/opencv-cuda ^
  -D OPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules ^
  -D WITH_CUDA=ON -D CUDA_ARCH_BIN=8.9 ^
  -D BUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,flann,photo,cudev,cudaarithm,cudawarping,cudaoptflow ^
  -D WITH_GSTREAMER=OFF
cmake --build opencv_build --config Release --target INSTALL -j 8

WITH_GSTREAMER=OFF avoids a glib alloca.h build break under MSVC; FFmpeg (bundled automatically) covers video I/O. Then point Fmd at the install:

cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -D OpenCV_DIR=C:/opencv-cuda/x64/vc17/lib

When configured correctly, CMake prints Fmd: opencv_cudaoptflow available — CUDA Farneback enabled, and getParam(TYPE) returns 2 (Farneback CUDA) after the first optical flow computation.

Build stand alone

The repository does not contain sub-repositories. The libraries Fmd depends on (Frame, ConfigReader, ObjectDetector and Logger) must be placed in the same folder as Fmd: copy their folders, for example from the folder modules of the RapidPixel SDK. The folder structure must be:

<work folder>
    Fmd
    Frame
    ConfigReader
    ObjectDetector
    Logger

Typical commands to build the library and the applications:

cd Fmd
mkdir build
cd build
cmake ..
cmake --build . --config Release

If Fmd is the top-level CMake project, the main CMakeLists.txt file adds the folders ../Frame, ../ConfigReader, ../ObjectDetector and ../Logger (in this order) before the library itself. If a folder does not exist CMake stops with an error.

CMake options

Table 5 - CMake options of the repository. In the options below ${PARENT} is the prefix defined by the top-level project (empty if Fmd is the top-level project).

Option Default Description
${PARENT}_FMD ON Build the library.
${PARENT}_FMD_EXAMPLE ON (stand alone), OFF (included in other project) Build the example application.
${PARENT}_FMD_DEMO ON (stand alone), OFF (included in other project) Build the demo application.
${PARENT}_FMD_BENCHMARK ON (stand alone), OFF (included in other project) Build the benchmark application.
${PARENT}_SUBMODULE_CACHE_OVERWRITE ON Forced rewriting of the options above in the CMake cache. The top-level project sets it to OFF when it includes Fmd as a sub-repository.
CMAKE_BUILD_TYPE Release (if not specified) Build type.
OpenCV_DIR found by find_package(OpenCV) Folder with the CMake configuration of the OpenCV to use (for example an OpenCV built with the CUDA modules).
vpi_DIR found by find_package(vpi 3.0) Folder with the CMake configuration of VPI 3.x. Without VPI the VPI backend is not compiled in.
FMD_OPENCV_NOCONFIG_ROOT not set Windows only, benchmark application: folder of an OpenCV installation whose bin/opencv_videoio_ffmpeg*.dll is copied next to FmdBenchmark.

The preprocessor flags FMD_HAVE_OPENCV_CUDA, FMD_HAVE_OPENCV_OPTFLOW and FMD_HAVE_VPI are not options: the CMake checks set them (see Hardware and platform support).

Connect to your project

If you want to connect Fmd library to your CMake project as source code, you can do the following. For example, if your repository has structure:

CMakeLists.txt
src
    CMakeLists.txt
    yourLib.h
    yourLib.cpp

Create folder 3rdparty in your repository and copy the folders of the libraries Frame, ConfigReader, ObjectDetector, Logger and Fmd there (for example from the folder modules of the RapidPixel SDK). The structure of your repository:

CMakeLists.txt
src
    CMakeLists.txt
    yourLib.h
    yourLib.cpp
3rdparty
    Frame
    ConfigReader
    ObjectDetector
    Logger
    Fmd

Fmd does not add the libraries it depends on when it is included in another project: the project must add them. Create CMakeLists.txt file in 3rdparty folder. CMakeLists.txt adds the dependencies in dependency order and then Fmd. CMakeLists.txt should contain:

cmake_minimum_required(VERSION 3.13)

################################################################################
## 3RD-PARTY
## dependencies for the project
################################################################################
project(3rdparty LANGUAGES CXX)

################################################################################
## SETTINGS
## basic 3rd-party settings before use
################################################################################
# To inherit the top-level architecture when the project is used as a submodule.
SET(PARENT ${PARENT}_YOUR_PROJECT_3RDPARTY)
# Disable self-overwriting of parameters inside included subdirectories.
SET(${PARENT}_SUBMODULE_CACHE_OVERWRITE OFF CACHE BOOL "" FORCE)

################################################################################
## CONFIGURATION
## 3rd-party submodules configuration
################################################################################
SET(${PARENT}_SUBMODULE_FRAME                           ON  CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_FRAME)
    SET(${PARENT}_FRAME                                 ON  CACHE BOOL "" FORCE)
    SET(${PARENT}_FRAME_TEST                            OFF CACHE BOOL "" FORCE)
endif()

SET(${PARENT}_SUBMODULE_CONFIG_READER                   ON  CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_CONFIG_READER)
    SET(${PARENT}_CONFIG_READER                         ON  CACHE BOOL "" FORCE)
    SET(${PARENT}_CONFIG_READER_EXAMPLES                OFF CACHE BOOL "" FORCE)
    SET(${PARENT}_CONFIG_READER_TESTS                   OFF CACHE BOOL "" FORCE)
endif()

SET(${PARENT}_SUBMODULE_OBJECT_DETECTOR                 ON  CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_OBJECT_DETECTOR)
    SET(${PARENT}_OBJECT_DETECTOR                       ON  CACHE BOOL "" FORCE)
    SET(${PARENT}_OBJECT_DETECTOR_TEST                  OFF CACHE BOOL "" FORCE)
    SET(${PARENT}_OBJECT_DETECTOR_EXAMPLE               OFF CACHE BOOL "" FORCE)
endif()

SET(${PARENT}_SUBMODULE_LOGGER                          ON  CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_LOGGER)
    SET(${PARENT}_LOGGER                                ON  CACHE BOOL "" FORCE)
    SET(${PARENT}_LOGGER_EXAMPLE                        OFF CACHE BOOL "" FORCE)
endif()

SET(${PARENT}_SUBMODULE_FMD                             ON  CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_FMD)
    SET(${PARENT}_FMD                                   ON  CACHE BOOL "" FORCE)
    SET(${PARENT}_FMD_EXAMPLE                           OFF CACHE BOOL "" FORCE)
    SET(${PARENT}_FMD_DEMO                              OFF CACHE BOOL "" FORCE)
    SET(${PARENT}_FMD_BENCHMARK                         OFF CACHE BOOL "" FORCE)
endif()

################################################################################
## INCLUDING SUBDIRECTORIES
## Adding subdirectories according to the 3rd-party configuration
################################################################################
if (${PARENT}_SUBMODULE_FRAME)
    add_subdirectory(Frame)
endif()

if (${PARENT}_SUBMODULE_CONFIG_READER)
    add_subdirectory(ConfigReader)
endif()

if (${PARENT}_SUBMODULE_OBJECT_DETECTOR)
    add_subdirectory(ObjectDetector)
endif()

if (${PARENT}_SUBMODULE_LOGGER)
    add_subdirectory(Logger)
endif()

if (${PARENT}_SUBMODULE_FMD)
    add_subdirectory(Fmd)
endif()

File 3rdparty/CMakeLists.txt adds the folders Frame, ConfigReader, ObjectDetector, Logger and Fmd to your project and excludes the applications of Fmd and the tests and examples of the other libraries from compiling. Next you need include folder 3rdparty in main CMakeLists.txt file of your repository. Add string at the end of your main CMakeLists.txt:

add_subdirectory(3rdparty)

Next you have to link the Fmd library in your src/CMakeLists.txt file:

target_link_libraries(${PROJECT_NAME} Fmd)

Fmd links ObjectDetector (and through it Frame and ConfigReader) publicly and Logger and OpenCV privately, so your target gets the include folders of ObjectDetector, Frame and ConfigReader. The order of the add_subdirectory(...) calls of the libraries does not matter for CMake (the target names are resolved when the build files are generated), but all of them must be added: if one is missing the compilation of Fmd stops with an error like ObjectDetector.h: No such file or directory.

Portability

The library is compiled for the baseline instruction set of the target architecture (no host-specific options such as -march=native or /arch:AVX2, no fast-math), so the binary runs on every CPU of the architecture; OpenCV selects its own SIMD code at run time. The library has no own SIMD code and does not serialize data, so it does not depend on the byte order. For the same input, builds with the same OpenCV give the same results with GCC and Clang on x86-64. Other OpenCV builds and other architectures compute slightly different floating-point flow values and can give slightly different detections (on the test sequences the aarch64 build gave the same boxes and IDs, with velocity differences below 1e-5 pixel/frame). The library and the applications were built with GCC 12 and 13 and Clang 15 and 18 (x86-64, Linux), with GCC 13 and Clang 18 for aarch64 (Linux, run under QEMU) and with MSVC 19.51 (Visual Studio 2026, x64, Windows, prebuilt OpenCV 4.12.0).

Applications

Three applications are built next to the library when Fmd is the top-level CMake project. All of them use only the public ObjectDetector API, so they exercise the library exactly as an integrator would:

Application Binary Purpose
example FmdExample Minimal integration reference — the code to copy into your own project.
demo FmdDemo Interactive viewer to evaluate detection quality on your own footage.
benchmark FmdBenchmark Measures per-frame processing time and frame rate on your platform.

The binaries are placed in <build folder>/bin (<build folder>/bin/Release with the Visual Studio generator). On Windows make sure the OpenCV DLLs are on PATH before running them.

Example application

The example application opens a video file with OpenCV, wraps each frame in a rapidpixel::Frame, runs the Fmd detector through the ObjectDetector interface and draws the bounding boxes of the detected moving objects:

FmdExample <video_file> [--headless] [--type N] [--output result.mp4]

Source code (Fmd/example/main.cpp is the complete version; the essential loop is below):

#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <Fmd.h>

int main(void)
{
    // Open video file "test.mp4".
    cv::VideoCapture videoSource;
    if (!videoSource.open("test.mp4"))
    {
        std::cout << "Can't open test.mp4" << std::endl;
        return -1;
    }

    // Create detector and configure. SENSITIVITY is normalized 0..1, higher
    // = more sensitive (0.6 == internal N-sigma 3.0).
    rapidpixel::Fmd detector;
    if (!detector.setParam(rapidpixel::ObjectDetectorParam::MIN_OBJECT_WIDTH, 4.0f) ||
        !detector.setParam(rapidpixel::ObjectDetectorParam::MAX_OBJECT_WIDTH, 128.0f) ||
        !detector.setParam(rapidpixel::ObjectDetectorParam::MIN_OBJECT_HEIGHT, 4.0f) ||
        !detector.setParam(rapidpixel::ObjectDetectorParam::MAX_OBJECT_HEIGHT, 128.0f) ||
        !detector.setParam(rapidpixel::ObjectDetectorParam::SENSITIVITY, 0.6f))
    {
        std::cout << "Parameter rejected" << std::endl;
        return -1;
    }

    // The pipeline is one frame deep: detect(frame N) returns the objects of
    // frame N-1 (see the detect method). So the overlay is drawn on the
    // previous frame, kept here — a copy, because VideoCapture reuses its
    // buffer.
    cv::Mat frameBgrOpenCv;
    cv::Mat previousFrame;
    int frameIdx = 0;

    // Main loop: until the end of the video or ESC.
    while (videoSource.read(frameBgrOpenCv))
    {
        // Wrap the OpenCV frame in rapidpixel::Frame (no copy).
        rapidpixel::Frame bgrFrame;
        bgrFrame.width = frameBgrOpenCv.cols;
        bgrFrame.height = frameBgrOpenCv.rows;
        bgrFrame.size = static_cast<int>(frameBgrOpenCv.total() *
                                         frameBgrOpenCv.elemSize());
        bgrFrame.data = frameBgrOpenCv.data;
        bgrFrame.fourcc = rapidpixel::Fourcc::BGR24;
        bgrFrame.frameId = frameIdx++;

        // Run detection.
        if (!detector.detect(bgrFrame))
        {
            std::cout << "Frame rejected" << std::endl;
        }

        // Retrieve confirmed objects and draw them on the frame they belong to.
        const std::vector<rapidpixel::Object> objects = detector.getObjects();
        if (!previousFrame.empty())
        {
            for (const rapidpixel::Object& obj : objects)
            {
                cv::rectangle(previousFrame,
                              cv::Rect(obj.x, obj.y, obj.width, obj.height),
                              cv::Scalar(0, 255, 0), 2);
            }
            cv::imshow("Fmd", previousFrame);
            if (cv::waitKey(1) == 27)
            {
                break;
            }
        }
        frameBgrOpenCv.copyTo(previousFrame);
    }
    return 0;
}

Demo application

The demo application is an interactive viewer for evaluating Fmd on your own footage. It plays a video in a loop at its native frame rate, runs the detector on every frame and marks the confirmed objects, with live sliders for the parameters that matter most:

FmdDemo <video_file>

Launched without an argument on Windows it opens a file-selection dialog; on other platforms pass the file on the command line.

Control Action
Sensitivity slider SENSITIVITY as 0..100, i.e. the 0..1 parameter x100 (default 70).
Min size / Max size sliders MIN_/MAX_OBJECT_WIDTH and MIN_/MAX_OBJECT_HEIGHT, pixels.
Flow gap slider FRAME_BUFFER_SIZE — optical-flow temporal gap, 1..16.
SPACE Play / pause.
N Step one frame (while paused).
R Restart the clip and reset the detector.
ESC / Q Quit.

The window opens filled to the screen and can be resized freely; the sliders and the overlay are drawn into the frame and scale with the window, so they stay legible at any size. The status panel reports the selected optical-flow backend, the per-frame detector time, the number of current objects, the playback rate and the source resolution — detection always runs at the native resolution, the window only changes how the frame is displayed.

Two things to keep in mind when judging quality here: the per-frame time in step mode is higher than during continuous playback (CPU/GPU clocks drop between manual steps), and the detector needs a few frames after a start, a loop wrap or a parameter change before its output stabilizes.

Benchmark application

The benchmark application measures what the library costs on your platform. It runs a clip head-less, times detect() per frame (video decoding excluded) and prints the distribution together with the resulting frame rate:

FmdBenchmark <video_file> [options]
Option Description
--type N Optical-flow backend: -1 auto (default), 0 VPI, 1 Farneback CPU, 2 Farneback CUDA, 3 Dual TV-L1.
--sensitivity S SENSITIVITY, 0..1 (default: the library default).
--min-size N / --max-size N Object side limits, pixels.
--gap N FRAME_BUFFER_SIZE — optical-flow temporal gap, 1..16.
--frames N Stop after N frames read, warmup included (default: the whole clip).
--warmup N Frames excluded from the statistics (default 30).
--csv <file> Per-frame log: frame,detect_us,library_us,objects,warmup.
--quiet Print only the final summary.
--help, -h Print the option list.

Output looks like this:

FMD 1.0.6 benchmark
  clip        : test.mkv
  frame       : 640x512 (0.33 Mpx)
  measured    : 270 frames (300 read, first 30 excluded as warmup)
  backend     : Farneback CUDA (TYPE 2)
  sensitivity : 0.700  (library default)
  settings    : flow gap 1, object side 4..128 px
  detect()    : median 12.70 ms | mean 12.85 | p90 15.00 | p99 16.40 | min 12.10 | max 17.90
  throughput  : 78.7 FPS from median (66.7 FPS from p90)
  cost        : 38.5 ms/Mpx (median)
  library     : median 12.69 ms (PROCESSING_TIME_MCS, cross-check)
  detections  : 180 confirmed objects, 173 of 270 frames with >= 1

Timing covers detect() only - video decoding and any work the
application does with the results are excluded. Frames are fed
back to back, so this is peak throughput, not the load the
detector puts on the system at a fixed capture rate.

Two measurement notes. The warmup matters: the first detect() call only primes the flow history and is much faster than the steady state, and with a GPU backend the first optical flow computation also allocates the GPU buffers, which takes an order of magnitude longer than the steady state, so neither must enter the statistics. And the numbers depend on the power state of the machine — clocks boost at the start of a run and may throttle later, so compare runs of the same length and prefer a few hundred frames for a figure you intend to quote. On Jetson, scripts/perf-run.sh raises the clocks to the maximum permitted by the current nvpmodel, which makes the results both faster and more repeatable.


Table of contents