opencv-rust
Library Featured
Rust bindings for OpenCV
- Downloads
- 262,425
- Dependents
- 96
- Stars
- 2,500
- Forks
- 185
- Contributors
- 46
- Commits
- 1,739
- Latest release
- 0.101.1 ·
About
Rust OpenCV bindings
Rust bindings for the popular OpenCV computer vision library.
The API is usable, but unstable and not very battle-tested; use at your own risk.
Changelog | Troubleshooting | Support the project
Quickstart
Make sure the supported OpenCV version (4.x or 5.x) and Clang (part of LLVM, needed for automatic binding generation) are installed in your system. OpenCV 3.4 is still supported but deprecated and will be removed in a future release.
Update your Cargo.toml
opencv = "0.102.0"
Import prelude
use opencv::prelude::*;
Getting OpenCV
See INSTALL.md for instructions on how to install required system dependencies.
Troubleshooting
See TROUBLESHOOTING.md for some common issues and their solutions.
Environment variables
The following variables must be set when building without pkg_config, cmake or vcpkg. You can set them
on any platform, the specified values will override those automatically discovered.
Read the full README
OPENCV_LINK_LIBSComma separated list of library names to link to..lib,.soor.dylibextension is optional. For every library you can specify optional "dylib=", "static=" or "framework=" prefix to indicate the specific type. E.g. "opencv_world411", "framework=OpenCL". Additionally, extensions can be used to indicate the type as well, ".a" and ".lib" for static libraries and ".framework" for macOS frameworks. E.g. "OpenCL.framework".If this list starts with '+' (plus sign) then the specified items will be appended to whatever the system probe returned. E.g. a value of "+dc1394" will do a system discovery of the OpenCV library and its linked libraries and then will additionally link
dc1394library at the end. Can be useful if the system probe produces a mostly working setup, but has incomplete link list, or the order is wrong (especially important during static linking).OPENCV_LINK_PATHSComma separated list of paths to search for libraries to link. E.g. "C:\tools\opencv\build\x64\vc15\lib". The path list can start with '+', seeOPENCV_LINK_LIBSfor a detailed explanation (e.g. "+/usr/local/lib").OPENCV_INCLUDE_PATHSComma separated list of paths to search for system include files during compilation. E.g. "C:\tools\opencv\build\include". One of the directories specified therein must contain "opencv2/core/version.hpp" or "core/version.hpp" file, it's used to detect the version of the headers. The path list can start with '+', seeOPENCV_LINK_LIBSfor a detailed explanation (e.g. "+/opt/cuda/targets/x86_64-linux/include/").
The following variables are rarely used, but you might need them under some circumstances:
OPENCV_PACKAGE_NAMEIn some cases you might want to override the pkg-config, cmake or vcpkg package name, you can use this environment variable for that. If you set it pkg-config will expect to find the file with that name and.pcextension in the package directory. Cmake will look for that file with.cmakeextension. And vcpkg will use that name to try to find package inpackagesdirectory underVCPKG_ROOT. You can also use separate environment variables to set different package names for different package systems:OPENCV_PKGCONFIG_NAMEOPENCV_CMAKE_NAMEOPENCV_VCPKG_NAME
OPENCV_CMAKE_BINPath to cmake binary (used in OpenCV discovery process using cmake). If not set then just "cmake" will be used. For example, you can set something like "/usr/local/bin/cmake" here.OPENCV_DISABLE_PROBESComma separated list of OpenCV package auto-discovery systems to exclude from running. Might be useful if one of the higher priority systems is producing incorrect results. Can contain the following values:- environment - reads data only from the
OPENCV_LINK_LIBS,OPENCV_LINK_PATHSandOPENCV_INCLUDE_PATHSenvironment variables - pkg_config
- cmake
- vcpkg_cmake - like vcpkg, but only uses vcpkg for path discovery, the actual OpenCV probe is done using cmake (cmake related environment variables are applicable with this probe)
- vcpkg
- environment - reads data only from the
OPENCV_MSVC_CRTAllows selecting the CRT library when building with MSVC for Windows. Allowed values are"static"for/MTand"dynamic"for/MD.OPENCV_CMAKE_TOOLCHAIN_FILEPath to a cmake toolchain file to be used during OpenCV discovery. Useful when cross-compiling. This will pass-DCMAKE_TOOLCHAIN_FILE=<value>to cmake command line.OPENCV_CMAKE_ARGSAdditional arguments to be passed to cmake during OpenCV discovery. This will be parsed according to the POSIX command line rules (e.g. quotes are supported) and passed as is tocmake.
The following variables affect the building the of the opencv crate, but belong to external components:
PKG_CONFIG_PATHWhere to look for*.pcfiles see the man pkg-config Path specified here must containopencv.pc(pre OpenCV 4) oropencv4.pc(OpenCV 4 and later).VCPKG_ROOT,VCPKGRS_DYNAMICandVCPKGRS_TRIPLETThe root ofvcpkginstallation, flag allowing use of*.dlllibraries and selectedvcpkgtriplet, see the documentation forvcpkgcrateOpenCV_DIRThe directory that contains OpenCV package cmake files. Usually there areOpenCVConfig.cmake,OpenCVConfig-version.cmakeandOpenCVModules.cmakein it.LD_LIBRARY_PATHOn Linux it sets the list of directories to look for the installed*.sofiles during runtime. Linux documentation has more info. Path specified here must containlibopencv_*.sofiles.DYLD_LIBRARY_PATHandDYLD_FALLBACK_LIBRARY_PATHSimilar toLD_LIBRARY_PATH, but for loading*.dylibfiles on macOS, see man dyld and this SO answer for more info. Path specified here must contain*.dylibfiles.PATHWindows searches for*.dlls inPATHamong other places, be sure to set it up, or copy required OpenCV*.dlls next to your binary. Be sure to specify paths in UNIX style (/C/Program Files/Dir) because colon inPATHmight be interpreted as the entry separator. Summary here.OPENCV_CLANG_ARGSAllow custom arguments for generating and parsing code with clang, see the documentation for clang arguments.clangcrate environment variables See crate's README
Cargo features
- There is a feature named after each OpenCV module (e.g.
imgproc,highgui, etc.). They are all enabled by default, but if a corresponding module is not found then it will silently be ignored. If you need to select a specific set of modules be sure to disable the default features and provide the required feature set:opencv = { version = ..., default-features = false, features = ["calib3d", "features2d", "flann"]} clang-runtime- enables the runtime detection of libclang (runtimefeature ofclang-sys). Useful as a workaround for when your dependencies (likebindgen) pull inclang-syswith hardruntimefeature.rgb- allow usingrgbcrate types asMatelementsf16- add intergration withf16type from thehalfcrate
API details
API Documentation is automatically translated from OpenCV's doxygen docs. Most likely you'll still want to refer to the official OpenCV C++ documentation as well.
OpenCV version support
The following OpenCV versions are supported at the moment:
- 3.4 (deprecated, will be removed in a future release)
- 4.x
- 5.x
Minimum rustc version (MSRV)
Currently, Rust version 1.88.0 or later is required. General policy is that rust version from 1 year ago is supported. Bumping versions older than that is not considered a breaking change.
Platform support
Currently, the main development and testing of the crate is performed on Linux, but other major platforms are also supported: macOS and Windows.
For some more details please refer to the CI build scripts: Linux OpenCV install, macOS OpenCV install as framework, macOS OpenCV install via brew, Windows OpenCV install via Chocolatey, Windows OpenCV install via vcpkg, Test runner script.
Functionality
Generally the crate tries to only wrap OpenCV API and provide some convenience functions to be able to use it in Rust easier. We try to avoid adding any functionality besides that.
Errors
Most functions return a Result to expose a potential C++ exception. Although some methods like property reads
or functions that are marked CV_NOEXCEPT in the OpenCV headers are infallible and return a naked value.
CV_MAKETYPE
CV_MAKETYPE and related CV_MAT_DEPTH constant functions are available to replace the corresponding OpenCV macros.
Yet it's usually easier to call ::opencv_type() function on the corresponding Rust type. E.g.:
let t = u16::opencv_type(); // equivalent to CV_MAKETYPE(CV_16U, 1)
let t = Vec2f::opencv_type(); // equivalent to CV_MAKETYPE(CV_32F, 2)
C++ operators
Some C++ operators are supported, they are converted to the corresponding functions on Rust side. Here is the list with the corresponding function name:
[]→get()orget_mut()+,-→add(),sub()*,/→mul(),div()()(function call) →apply()=→set()*(deref) →try_deref()ortry_deref_mut()==,!=→equals(),not_equals()>,>=→greater_than(),greater_than_or_equal()<,<=→less_than(),less_than_or_equal()++,--→incr(),decr()&,|,^→and(),or(),xor()!→negate()
Class fields
Fields of OpenCV classes are accessible through setters and getters. Those functions are infallible, they
return the value directly instead of Result.
Infallible functions
For infallible functions (like setters) that accept &str values the following logic applies: if a Rust
string passed as argument contains null byte then this string will be truncated up to that null byte. So if
for example you pass "123\0456" to the setter, the property will be set to "123".
Callbacks
Some API functions accept callbacks, e.g. set_mouse_callback. While currently it's possible to successfully
use those functions there are some limitations to keep in mind. Current implementation of callback handling
leaks the passed callback argument. That means that the closure used as a callback will never be freed during
the lifetime of a program and moreover Drop will not be called for it. There is a plan to implement possibility
to be able to free at least some closures.
Unsafety
Although the crate tries to provide an ergonomic Rust interface for OpenCV, don't expect
Rust safety guarantees at this stage. It's especially true for the borrow-checking and the
shared mutable ownership. Notable example would be Mat which is a reference counted
object in its essence. You can own a seemingly separate Mat in Rust terms, but
it's going to be a mutable reference to the other Mat under the hood. Treat safety
of the crate's API as you would treat one of C++, use clone() when needed.
Contrib modules
To be able to use some modules you need to have opencv_contrib
installed. You can find the full list of contrib modules here.
Missing modules and functions
While most of the API is covered, for various reasons (that might no longer hold) there are modules and functions that are not yet implemented. If a missing module/function is near and dear to you, please file an issue (or better, open a pull request!).
The binding strategy
This crate works similar to the model of python and java's OpenCV wrappers - it uses libclang to parse the OpenCV C++ headers, generates a C interface to the C++ API, and wraps the C interface in Rust.
All the major modules in the C++ API are merged together in a huge cv:: namespace. We instead made one rust
module for each major OpenCV module. So, for example, C++ cv::Mat is opencv::core::Mat in this crate.
The methods and field names have been snake_cased. Methods arguments with default value lose these default values, but they are reported in the API documentation.
Overloaded methods have been mostly manually given different names or automatically renamed to *_1, *_2, etc.
Older OpenCV branches support
OpenCV 3.4
The last version with the confirmed OpenCV 3.4 support is 0.98, after that this branch of OpenCV is no longer
tested and supported. It will be removed in a future release.
OpenCV 3.2
The last version with confirmed OpenCV 3.2 support is 0.75, after that this branch of OpenCV is no longer
tested and supported. Since version 0.94.0 the support of OpenCV 3.2 is removed from the codebase.
OpenCV 2
If you can't use OpenCV 3.x or higher, the (no longer maintained) 0.2.4 version of this crate is known to
work with OpenCV 2.4.7.13 (and probably other 2.4 versions). Please refer to the README.md file for that
version because the crate has gone through the considerable rewrite since.
Contributor's Guide
The binding generator code lives in a separate crate under binding-generator. During the build phase it creates bindings from the header files and puts them into bindings directory. Those are then transferred to src for the consumption by the crate users.
The crate itself, as imported by users, consists of generated rust code in src committed to the repo.
This way, users don't have to handle the code generation overhead in their builds. When developing this crate,
you can test changes to the binding generation using cargo build -vv. When changing the binding-generator,
be sure to push changes to the generated code!
If you're looking for things to improve be sure to search for todo and fixme labels in the project
source, those usually carry the comment of what exactly needs to be fixed.
The license for the original work is MIT.
Special thanks to ttacon for yielding the crate name.
Compatibility
- Runs on
- GPU support unknown
What shows it
- no dependency or feature matches a backend row
- Native code
- Build tool
- Build uses a native toolchain: cc, pkg-config, vcpkg
- Python bindings
- No Python binding crate found
- License
- MIT
Compare with similar projects
Order: strongest tier first, then most downloads, then name, with unmeasured values last. A library counts downloads over 30 days. An app counts binary downloads per release. An app with none counts its crate downloads over 30 days.
| Project | Downloads | Dependents | Stars | Latest release | Runs on (GPU) | Native code | License |
|---|---|---|---|---|---|---|---|
|
Rust bindings for OpenCV |
262,425 downloads over 30 days | 96 | 2,500 | 0.101.1 | GPU support unknown | Build uses a native toolchain: cc, pkg-config, vcpkg | MIT |
|
ML-powered manga translator, written in Rust. |
6,338 binary downloads per release | Not applicable | 6,620 | Not measured | Not measured | Apache-2.0 | |
|
inference Listed
Ultralytics YOLO inference library and CLI for Rust |
2,609 downloads over 30 days | 1 | 217 | 0.0.56 | CUDA, WebGPU, ROCm | Binds native code: ort-sys, ffmpeg-sys-next (feature video), openvino-sys (feature intel) | AGPL-3.0 |
|
kornia-rs Listed
Low-level computer vision library in Rust |
2,294 downloads over 30 days | 6 | 751 | 0.1.5 | CUDA | Binds native code: glib-sys (feature gstreamer), gobject-sys (feature gstreamer), gstreamer-app-sys (feature gstreamer), gstreamer-base-sys (feature gstreamer), gstreamer-sys (feature gstreamer) and 1 more | Apache-2.0 |
|
usls Listed
A Rust library integrated with ONNXRuntime, providing a collection of ML models. |
355 downloads over 30 days | 1 | 447 | 0.1.11 | CUDA, ROCm | Binds native code: ffmpeg-sys-next (feature video), ort-sys (feature acl) | MIT |
|
A Rust library for object detection via SSD MobileNet. |
Not measured | Not applicable | 5 | Not measured | Not measured | Not measured | MIT |
Why FeaturedIt passes every Featured gate.
What it meets
- It has 30 active dependents and 15 new dependents in 365 days. Featured needs 11 active dependents or 9 new dependents in 365 days.
- Active dependents are at 93% of their peak. Featured needs at least 50%.
- Docs are present. The release commit or a manifest ties the crate to the repository.
- No vulnerability advisory stays unpatched past the grace window. At least one version is neither a prerelease nor 0.0.x.
- The last activity is on 8 Nov 2026, 1 day ago. The rules expect an update within 1200 days. Dormancy rule
Exact rules: Featured library. The check on applied thresholds version 9.
Every rule outcome, as the check records it
expected-interval: version 0.101.1 base 300, doubled for 262425 downloads per month, doubled for 30 active users: E = 1200 daysdormancy: last activity 2026-11-08, 1 days ago, E = 1200: activefeatured: featured: active_users 30, new_adopters 15, former_glory 0.93
Crates2 crates
- opencv Primary
- opencv-binding-generator
Measurements and sources
Each signal row shows the raw value, its source and the fetch date. The Sources list gathers every distinct source on this page. Movement needs more history.
Sources6 sources
- Description: the crates.io description
- crates.io database dump, fetched
- RustSec advisory database, fetched
- GitHub repository, fetched
- crates.io package, fetched
- GitHub contributors, fetched
Adoption30 active dependents, 15 new dependents in 365 days1 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Active dependents | 30 | crates.io database dump | Yes | |
| New dependents (365 days) | 15 | crates.io database dump | Yes | |
| Peak active dependents | 34 | crates.io database dump | Yes | |
| Active dependents as a share of the peak | 0.9318 | crates.io database dump | Yes | |
| Binary downloads per release | Not measured | Yes |
MaintenanceLatest human commit: 1 day ago3 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Latest human commit | 2026-11-08 | GitHub repository | Yes | |
| Latest release | 2026-10-09 | crates.io database dump | Yes | |
| First release | 2015-09-27 | crates.io database dump | Yes | |
| Latest version | 0.101.1 | crates.io database dump | Yes | |
| Days the oldest outside issue waits for a reply | 342 | GitHub repository | Yes | |
| Maintainer replies over 365 days | 51 | GitHub repository | Yes | |
| Repository archived | false | GitHub repository | Yes | |
| Repository gone | false | GitHub repository | Yes | |
| Release commit or manifest in the repo | true | crates.io package | Yes | |
| Published docs | true | crates.io database dump | Yes | |
| Latest release fails cargo check | Not measured | Yes | ||
| Build check date | Not measured | Yes | ||
| Build check toolchain | Not measured | Yes |
SecurityUnpatched vulnerabilities: none
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Unpatched vulnerabilities past the grace window | none | RustSec advisory database | Yes | |
| RustSec unmaintained advisory | none | RustSec advisory database | Yes | |
| Deprecation notice | none | crates.io database dump | Yes | |
| Deprecated requirement | none | crates.io database dump | Yes |
Distribution216 releases, 262,425 downloads over 30 days5 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Releases | 216 | crates.io database dump | Yes | |
| Downloads over 30 days | 262425 | crates.io database dump | Yes | |
| Binary downloads over the last 3 releases | Not measured | Yes | ||
| Homebrew installs on request over 365 days | Not measured | Yes | ||
| Repology repository families | Not measured | Yes | ||
| Docker pull growth over 90 days | Not measured | Yes | ||
| Docker pulls at the growth baseline | Not measured | Yes | ||
| Every version a prerelease | false | crates.io database dump | Yes | |
| Every version 0.0.x | false | crates.io database dump | Yes | |
| Every version yanked | false | crates.io database dump | Yes | |
| Crate deleted from crates.io | false | crates.io database dump | Yes |
Repository2,500 stars, 185 forks, 46 contributors, 1,739 commits. Stars alone never qualify a project.
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| GitHub stars | 2500 | GitHub repository | Yes — one part of the repository route | |
| GitHub forks | 185 | GitHub repository | Yes — one part of the repository route | |
| Contributors with a GitHub account | 46 | GitHub contributors | Yes — one part of the repository route | |
| Commits on the default branch | 1739 | GitHub repository | No | |
| Repository's main crate left out of the entry | none | crates.io database dump | Yes |
Facts that never count
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| License | MIT | crates.io database dump | No | |
| Runs on, from listed crates and their direct dependencies | GPU support unknown | crates.io database dump | No | |
| What shows each backend | no dependency or feature matches a backend row | crates.io database dump | No | |
| Native code, from listed crates and their dependencies | Build uses a native toolchain: cc, pkg-config, vcpkg | crates.io database dump | No | |
| Python bindings, from direct dependencies of listed crates | No Python binding crate found | crates.io database dump | No | |
| Crates that depend on a listed crate directly | 96 | crates.io database dump | No |
Corrections and review history
No correction applies to this entry.
Judgments
A judgment enters only through an approved issue and a merged pull request.
No judgment applies to this entry.