kcfcpp Documentation

repository·master·Indexed 21 days ago

https://github.com/joaofaro/kcfcpp

A C++ implementation of the Kernelized Correlation Filter (KCF) tracker ported to OpenCV 3.0.0. The library provides high-speed object tracking compatible with the VOT benchmark, featuring the KCFTracker class for HOG and Lab color space features, as well as a CSK Tracker implementation using raw grayscale features.

Tokens
1.9K
Snippets
6
Records
9
Agent score
76%

What's inside kcfcpp

  1. Available KCF algorithm implementations

    master

    The repository provides different modes of the KCF algorithm via the KCF executable:

    • KCFC++ (./KCF): KCF using HOG features.
    • KCFLabC++ (./KCF lab): KCF using both HOG and Lab features (Lab features are quantized into 15 centroids via k-means).
    • CSK Tracker: Implemented by using raw grayscale as features (single-channel filter).
  2. Use the KCFTracker C++ class in your project

    master

    To integrate the tracker into your own C++ application without using the VOT toolkit, follow this pattern:

    1. Instantiate a KCFTracker object with your desired feature and scale options.
    2. Initialize the tracker using tracker.init() with the initial bounding box (Rect) and the first frame.
    3. Update the tracker in subsequent frames using tracker.update() to retrieve the new object position.
    // Create the KCFTracker object with one of the available options
    KCFTracker tracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);
    
    // Give the first frame and the position of the object to the tracker
    tracker.init( Rect(xMin, yMin, width, height), frame );
    
    // Get the position of the object for the new frame
    result = tracker.update(frame);
  3. Initialize and use the KCFTracker class

    master

    The KCFTracker class implements tracking based on Kernelized Correlation Filters (KCF) and Circulant Structure with Kernels (CSK).

    To use the tracker:

    1. Instantiate the tracker with desired feature and scale settings.
    2. Initialize the tracker using init() with the target's initial bounding box (cv::Rect) and the first frame (cv::Mat).
    3. Update the tracker in subsequent frames using update() with the new frame (cv::Mat), which returns the new target position (cv::Rect).

    Note: If multiscale is enabled, fixed_window must be set to false in the constructor.

    // Example usage pattern
    KCFTracker tracker(true, true, true, true);
    
    // Initialize with target ROI and first frame
    cv::Rect roi(100, 100, 50, 50);
    cv::Mat first_frame = ...;
    tracker.init(roi, first_frame);
    
    // Update in a loop with new frames
    cv::Mat next_frame = ...;
    cv::Rect new_roi = tracker.update(next_frame);
  4. Reference KCF command-line options

    master

    The following options are available when running the ./KCF executable:

    • gray: Use raw gray level features.
    • hog: Use HOG features.
    • lab: Use Lab colorspace features (this also enables HOG features by default).
    • singlescale: Performs single-scale detection using a variable-size window.
    • fixed_window: Keeps the window size fixed when in singlescale mode.
    • show: Displays the tracking results in a window.
  5. Run the KCF executable with command-line options

    master

    The KCF executable is designed to interface with the VOT benchmark. You can pass several options to configure the tracking algorithm behavior.

    ./KCF [OPTION_1] [OPTION_2] [...]
  6. Configure KCFTracker via constructor

    master

    The KCFTracker constructor accepts four boolean parameters to define the tracking mode:

    • hog (default: true): If true, uses HOG features (KCF mode). If false, uses raw pixels (CSK mode).
    • fixed_window (default: true): If true, uses a fixed window size. If false, uses the ROI size (more accurate but slower).
    • multiscale (default: true): Enables multi-scale tracking. Note: This cannot be used if fixed_window is set to true.
    • lab (default: true): Enables Lab color space features.
    KCFTracker(bool hog = true, bool fixed_window = true, bool multiscale = true, bool lab = true);
  7. Implement or use a Tracker via the Tracker base class

    master

    The Tracker class is an abstract base class that defines the interface for all tracking algorithms in this library. To use a specific tracking algorithm, you must call init to set the initial target location and then call update in a loop to follow the target in subsequent frames.

    Key methods:

    • init(const cv::Rect &roi, cv::Mat image): Initializes the tracker with the initial bounding box (roi) and the first frame (image).
    • update(cv::Mat image): Processes a new frame and returns the updated bounding box (cv::Rect) of the target. If the target is not found, the behavior depends on the specific implementation (typically returning an empty rect or the last known position).
    // Example usage pattern for a Tracker implementation
    Tracker* tracker = /* get instance of a specific tracker, e.g., KCFTracker */; 
    
    // 1. Initialize with the first frame and the target ROI
    cv::Rect initial_roi(100, 100, 50, 50);
    cv::Mat first_frame = cv::imread("frame0.jpg");
    tracker->init(initial_roi, first_frame);
    
    // 2. Update in a loop with subsequent frames
    while (true) {
        cv::Mat frame = /* get next frame from video stream or file */;
        if (frame.empty()) break;
    
        cv::Rect updated_roi = tracker->update(frame);
        
        if (updated_roi.width > 0 && updated_roi.height > 0) {
            // Draw the bounding box on the frame
            cv::rectangle(frame, updated_roi, cv::Scalar(0, 255, 0), 2);
        }
        cv::imshow("Tracking", frame);
        if (cv::waitKey(30) == 27) break;
    }
    
    delete tracker;
  8. Customize KCFTracker parameters

    master

    After instantiation but before calling init(), you can fine-tune the tracker's behavior by modifying its public member variables:

    ParameterTypeDescription
    interp_factorfloatLinear interpolation factor for adaptation
    sigmafloatGaussian kernel bandwidth
    lambdafloatRegularization
    cell_sizeintHOG cell size
    paddingfloatHorizontal area surrounding the target, relative to its size
    output_sigma_factorfloatBandwidth of gaussian target
    template_sizeintTemplate size in pixels (use 0 to use ROI size)
    scale_stepfloatScale step for multi-scale estimation (set to 1 to disable)
    scale_weightfloatWeight to downweight detection scores of other scales for stability

    Performance Tip: For optimal speed, ensure the ratio (template_size / cell_size) is a power of 2 or a product of small prime numbers.