twistedfall/opencv-rust
OpenCV API bindings for Rust
What it solves
OpenCV is a powerful C++ computer vision library, but Rust developers who want to use it would normally need to write and maintain their own FFI glue code. This crate provides ready-made Rust bindings so you can call OpenCV's functions and use its data structures directly from Rust. It also makes the API more ergonomic by converting C++ conventions to Rust-friendly ones, such as snake_case method names and returning Result for functions that can throw exceptions.
How it works
The bindings are generated automatically during development using libclang to parse OpenCV's C++ headers. The generator produces a C interface, which is then wrapped in Rust. This approach is similar to how Python and Java wrappers for OpenCV work. The crate maps each major OpenCV module to its own Rust module, so for example the C++ cv::Mat becomes opencv::core::Mat. Method names are snake_cased, overloaded functions are renamed (with suffixes like *_1, *_2), and default argument values are dropped but reported in the documentation. At build time, the crate discovers your OpenCV installation via pkg-config, cmake, vcpkg, or environment variables, and it lets you select specific OpenCV modules instead of linking everything.
Who it's for
This crate is for Rust developers who want to use OpenCV's computer vision functionality in their projects without writing manual FFI. It's especially suited to those working on image processing, video analysis, or other vision tasks who are comfortable with Rust's unsafe concepts, because the crate does not yet provide Rust-level memory safety guarantees. The README warns that the API is "usable, but unstable and not very battle-tested," so it's aimed at users willing to accept some rough edges.
Highlights
- Auto-generated bindings from OpenCV's C++ headers using libclang, following the same pattern as Python and Java wrappers.
- Supports OpenCV 3.4 (deprecated), 4.x, and 5.x, with main development on Linux but also macOS and Windows.
- Module structure mirrors OpenCV's C++ namespaces, e.g., cv::Mat becomes opencv::core::Mat.
- Most functions return Result to expose C++ exceptions; infallible functions (like setters) return naked values.
- Provides a set of environment variables to control how OpenCV is discovered and linked, including overrides for pkg-config, cmake, and vcpkg.
- Cargo features allow enabling only specific OpenCV modules, and there are optional integrations for rgb and f16 types.
Related
- Project
- Project
- Project
- Project
- Project