| Crates.io | opencv-wasm |
| lib.rs | opencv-wasm |
| version | 4.8.1 |
| created_at | 2025-07-12 17:25:58.093078+00 |
| updated_at | 2025-07-12 20:25:32.890088+00 |
| description | OpenCV WebAssembly bindings for browser deployment |
| homepage | |
| repository | https://github.com/ruvnet/ruv-FANN |
| max_upload_size | |
| id | 1749523 |
| size | 32,139 |
WebAssembly bindings for OpenCV in Rust, enabling computer vision in the browser. This crate provides a JavaScript-friendly API for OpenCV's core functionality.
[dependencies]
opencv-wasm = "4.8.0"
wasm-bindgen = "0.2"
npm install opencv-wasm
Build the WASM module:
wasm-pack build --target web --out-dir pkg
use opencv_wasm::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn process_image(input: &WasmMat) -> Result<WasmMat, JsValue> {
// Apply Gaussian blur
gaussian_blur(input, 5, 1.0)
}
import init, {
init_opencv_wasm,
WasmMat,
mat_from_canvas,
mat_to_canvas,
gaussian_blur
} from './pkg/opencv_wasm.js';
async function main() {
// Initialize WASM module
await init();
init_opencv_wasm();
// Get canvas element
const canvas = document.getElementById('canvas');
// Convert canvas to Mat
const mat = await mat_from_canvas(canvas);
// Apply blur
const blurred = await gaussian_blur(mat, 5, 1.0);
// Draw result back to canvas
await mat_to_canvas(blurred, canvas);
}
main();
WebAssembly-compatible matrix wrapper:
// Create new matrix
const mat = new WasmMat(640, 480);
// Properties
console.log(mat.width, mat.height, mat.channels);
// Operations
const roi = mat.roi(10, 10, 100, 100);
const clone = mat.clone();
2D point for browser use:
const pt1 = new WasmPoint(10, 20);
const pt2 = new WasmPoint(30, 40);
const distance = pt1.distance_to(pt2);
Dimensions container:
const size = new WasmSize(1920, 1080);
console.log(size.area()); // 2073600
// Blur operations
const blurred = blur(src, 5);
const gaussian = gaussian_blur(src, 5, 1.0);
// Edge detection
const edges = canny(src, 100, 200);
// Resize
const resized = resize(src, 320, 240);
// Load from canvas
const mat = await mat_from_canvas(canvas);
// Save to canvas
await mat_to_canvas(mat, canvas);
// Load from image element
const img = document.getElementById('image');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const mat = await mat_from_canvas(canvas);
<!DOCTYPE html>
<html>
<head>
<title>OpenCV WASM Demo</title>
</head>
<body>
<input type="file" id="fileInput" accept="image/*">
<canvas id="canvas"></canvas>
<script type="module">
import init, {
init_opencv_wasm,
mat_from_canvas,
mat_to_canvas,
gaussian_blur,
canny,
get_version
} from './pkg/opencv_wasm.js';
async function processImage(file) {
const img = new Image();
img.src = URL.createObjectURL(file);
await img.decode();
const canvas = document.getElementById('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
// Convert to Mat
const mat = await mat_from_canvas(canvas);
// Apply edge detection
const edges = await canny(mat, 50, 150);
// Display result
await mat_to_canvas(edges, canvas);
}
async function init_app() {
await init();
init_opencv_wasm();
console.log(get_version());
document.getElementById('fileInput').addEventListener('change', (e) => {
if (e.target.files.length > 0) {
processImage(e.target.files[0]);
}
});
}
init_app();
</script>
</body>
</html>
WebAssembly SIMD support (when available) provides additional performance.
# Install dependencies
cargo install wasm-pack
# Build for web
wasm-pack build --target web
# Build optimized for size
wasm-pack build --target web --release -- --features wee_alloc
We welcome contributions! See CONTRIBUTING.md for guidelines.
Apache License 2.0 - see LICENSE for details.