OpenCV Core for Rust
Pure Rust implementation of OpenCV's core data structures and operations. This crate provides the fundamental building blocks for computer vision applications in Rust.
Features
- Pure Rust: No FFI dependencies, 100% safe Rust
- Core Data Structures: Mat, Point, Size, Rect, Scalar, Range
- Type System: Compatible with OpenCV's type conventions
- Memory Safe: Zero unsafe blocks in public APIs
- Serialization: Optional serde support
- Performance: Optimized with SIMD where available
Installation
[]
= "4.8.0"
# With serialization support
= { = "4.8.0", = ["serde"] }
Quick Start
use ;
Core Types
Mat - Matrix/Image Container
// Create matrices of different types
let mat_u8 = new_size?;
let mat_f32 = new_size?;
let mat_rgb = new_size?;
// Clone and copy
let cloned = mat_u8.clone?;
let mut dst = new;
mat_u8.copy_to?;
Point - 2D/3D Points
// 2D points
let pt2i = new; // Point2i
let pt2f = new; // Point2f
let pt2d = new; // Point2d
// 3D points
let pt3f = new; // Point3f
let pt3d = new; // Point3d
// Operations
let distance = pt2i.distance_to;
let dot_product = pt2i.dot;
Size - Dimensions
let size = new;
let area = size.area; // 307200
let aspect = size.aspect_ratio; // 1.333...
// Float sizes
let size_f = new;
Rect - Rectangles
let rect = new;
// Properties
let center = rect.center;
let area = rect.area;
let is_inside = rect.contains;
// Operations
let intersection = rect1.intersection;
let union = rect1.union;
Scalar - Multi-channel Values
// Single value for all channels
let gray = all;
// Individual channel values
let bgr = new; // Blue in BGR
// Arithmetic
let sum = scalar1.add;
let product = scalar1.mul;
Memory Management
The crate includes built-in memory management with allocation tracking:
use memory;
// Initialize memory system
init_allocators?;
// Check memory usage
let = get_memory_usage;
println!;
Error Handling
All operations that can fail return Result<T, opencv_core::Error>
:
use ;
Performance
- SIMD optimizations via
safe_arch
crate - Efficient memory layout compatible with OpenCV
- Zero-copy operations where possible
- Lazy allocation strategies
Compatibility
This crate aims to be compatible with OpenCV 4.x conventions while providing Rust-idiomatic APIs. Types can be converted to/from OpenCV's C++ types when using the FFI layer.
Contributing
Contributions are welcome! Please see our contributing guidelines.
License
Licensed under Apache License 2.0 - see LICENSE for details.