opencv-rust

repository·master·Indexed 25 days ago

https://github.com/twistedfall/opencv-rust

Rust bindings for the OpenCV computer vision library (version 0.100.0). It provides access to OpenCV's functionality within Rust applications, supporting OpenCV versions 4.x, 5.x, and 3.4 (deprecated). The crate uses libclang to generate a C interface and wraps it in Rust, organizing the API into modules corresponding to major OpenCV modules. Requires rustc 1.88.0 or later and supports Linux, macOS, and Windows.

Tokens
8.4K
Snippets
14
Records
65
Agent score
81%

What's inside opencv-rust

  1. Safety and Ownership (Unsafety)

    master

    The crate provides an ergonomic interface but does not provide full Rust safety guarantees regarding borrow-checking and shared mutable ownership.

    Key Concept: Mat Ownership Mat is essentially a reference-counted object. You may own a Mat in Rust that is actually a mutable reference to another Mat under the hood. To ensure safety, treat the API as you would C++ and use .clone() when necessary to manage ownership correctly.

  2. Understand the binding strategy and module structure

    master

    The crate uses libclang to parse OpenCV C++ headers, generates a C interface, and wraps it in Rust.

    Module Mapping

    While C++ uses a single large cv:: namespace, this crate organizes code into one Rust module per major OpenCV module.

    • Example: C++ cv::Mat becomes opencv::core::Mat in Rust.

    Naming Conventions

    • Methods and fields are snake_cased.
    • Method arguments that have default values in C++ do not include them in the Rust signatures (though they are documented).
    • Overloaded methods are renamed using a *_1, *_2 suffix pattern.
  3. Understand error handling and infallibility

    master

    Error Handling

    Most functions return a Result to expose potential C++ exceptions. However, some methods are infallible and return a naked value (e.g., property reads or functions marked CV_NOEXCEPT in OpenCV headers).

    Class Fields

    Fields of OpenCV classes are accessed via getters and setters. These functions are infallible and return values directly instead of a Result.

    String Truncation in Infallible Setters

    For infallible setters accepting &str, if the string contains a null byte (\0), the value will be truncated at that byte. For example, passing "123\0456" will set the property to "123".

  4. Handle callbacks and memory limitations

    master

    Some API functions (like set_mouse_callback) accept closures as callbacks.

    Warning: The current implementation leaks the passed callback argument. Closures used as callbacks will never be freed during the program's lifetime, and their Drop implementation will not be called. Use this with caution as it can lead to memory accumulation.

  5. Quickstart with opencv crate

    master

    To use the opencv bindings in your Rust project, ensure you have a supported OpenCV version (4.x or 5.x) and Clang (part of LLVM) installed on your system.

    1. Add the dependency to your Cargo.toml:
    opencv = "0.100.0"
    1. Import the prelude in your Rust code to access the API:
    use opencv::prelude::*;
    opencv = "0.100.0"
    use opencv::prelude::*;
  6. Cross-compile for Raspberry Pi using Docker

    master

    Cross-compilation is supported, specifically for targeting Raspberry Pi from an x86-64 Linux host. The project provides a Dockerfile to simplify this setup.

    Build the Cross-compilation Image

    docker build -t rpi-xcompile -f tools/docker/rpi-xcompile.Dockerfile tools

    Prerequisites

    • qemu-arm must be present on the host system.
    • binfmt-misc must be set up.
      • For Debian-based distros: Install the qemu-arm package.
      • For OpenSUSE: Install qemu-linux-user via zypper.

    Once the image is built, it contains a sample build script at /usr/local/bin/cargo-xbuild which you can use to check the environment and command line arguments for cross-compiling inside the container.

    docker build -t rpi-xcompile -f tools/docker/rpi-xcompile.Dockerfile tools
  7. Install OpenCV on Linux

    master

    The opencv crate requires the OpenCV system library to be present. Depending on your distribution, use the following commands to install the necessary packages. Ensure you install the -dev versions of packages to include the headers required for the crate build.

    Arch Linux

    pacman -S clang qt6-base opencv

    To support additional OpenCV modules:

    pacman -S vtk glew fmt openmpi

    Ubuntu

    apt install libopencv-dev clang libclang-dev

    Opensuse

    zypper install opencv-devel clang-devel gcc-c++

    Manual Build (Other Linux)

    If installing from a repository is not an option, you can build OpenCV manually. You must set the following environment variables before building your Rust project:

    • PKG_CONFIG_PATH: Location of *.pc files OR
    • OpenCV_DIR: Location of *.cmake files
    • LD_LIBRARY_PATH: Location of the installed *.so files for runtime.

    Note: You must also install clang (or a derivative) that contains libclang.so and the clang binary.

    pacman -S clang qt6-base opencv
  8. Check OpenCV version and Rust compatibility

    master

    Before starting, ensure your environment meets the following requirements:

    Supported OpenCV Versions

    • 5.x
    • 4.x
    • 3.4 (Deprecated; will be removed in a future release)

    Minimum Rust Version (MSRV)

    • rustc 1.88.0 or later is required.

    Platform Support

    Primary development occurs on Linux, but macOS and Windows are also supported.

  9. Install OpenCV on macOS

    master

    The easiest way to install OpenCV on macOS is via Homebrew. If installed through Homebrew, the crate can automatically detect OpenCV, so you should not set OPENCV_LINK_LIBS, OPENCV_LINK_PATHS, or OPENCV_INCLUDE_PATHS.

    Using Homebrew

    brew install opencv

    You will need a working C++ compiler and libclang. You can install Command Line Tools via xcode-select --install, install Xcode from the App Store, or install llvm via Brew:

    brew install llvm

    Required Environment Variables

    Even with automatic detection, ensure the following variables are configured:

    export DYLD_FALLBACK_LIBRARY_PATH="$(xcode-select --print-path)/Toolchains/XcodeDefault.xctoolchain/usr/lib/"
    export LDFLAGS=-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
    brew install opencv