OpenCV WASM for Rust
WebAssembly bindings for OpenCV in Rust, enabling computer vision in the browser. This crate provides a JavaScript-friendly API for OpenCV's core functionality.
Features
- Browser-Ready: Compiled to WebAssembly for web applications
- Canvas Integration: Direct interaction with HTML5 Canvas
- Type Safety: Rust's safety with JavaScript interop
- Small Size: Optimized WASM output with wee_alloc
- Image Processing: Basic filters and transformations
Installation
Rust Project
[]
= "4.8.0"
= "0.2"
JavaScript/TypeScript
Building
Build the WASM module:
Usage
Rust Side
use *;
use *;
JavaScript Side
import init from './pkg/opencv_wasm.js';
;
API Overview
Core Types
WasmMat
WebAssembly-compatible matrix wrapper:
// Create new matrix
const mat = ;
// Properties
console.log;
// Operations
const roi = mat.;
const clone = mat.;
WasmPoint
2D point for browser use:
const pt1 = ;
const pt2 = ;
const distance = pt1.;
WasmSize
Dimensions container:
const size = ;
console.log; // 2073600
Image Processing Functions
// Blur operations
const blurred = ;
const gaussian = ;
// Edge detection
const edges = ;
// Resize
const resized = ;
Canvas Integration
// Load from canvas
const mat = await ;
// Save to canvas
await ;
// Load from image element
const img = document.;
const canvas = document.;
const ctx = canvas.;
ctx.;
const mat = await ;
Complete Example
OpenCV WASM Demo
Performance Tips
- Reuse Matrices: Create once, reuse multiple times
- Batch Operations: Process multiple operations before canvas update
- Use Web Workers: Offload processing to background threads
- Optimize Size: Use appropriate matrix types (8-bit vs 32-bit)
Browser Compatibility
- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 79+
WebAssembly SIMD support (when available) provides additional performance.
Building from Source
# Install dependencies
# Build for web
# Build optimized for size
Limitations
- Currently supports single-channel and 3-channel 8-bit images
- Some advanced OpenCV features not yet implemented
- Performance varies by browser and hardware
Contributing
We welcome contributions! See CONTRIBUTING.md for guidelines.
License
Apache License 2.0 - see LICENSE for details.