Skip to main content

Overview

OpenCV.js brings OpenCV functionality to web browsers through WebAssembly (WASM). It’s compiled from C++ using Emscripten and provides a JavaScript API that closely mirrors the C++ interface.

Installation

Using Pre-built opencv.js

The easiest way to get started:
The async attribute allows the page to load while opencv.js downloads. Use the onRuntimeInitialized callback to run code after OpenCV is ready.

Waiting for OpenCV to Load

Or use the runtime callback:

Building from Source

For custom builds with specific modules:
Emscripten 4.0.20+ requires C++17 or newer. The build process typically takes 5-10 minutes.

Quick Start

Basic HTML Template

Core Concepts

Mat Objects

All images in OpenCV.js are represented by cv.Mat:
Memory Management: JavaScript garbage collection doesn’t handle WebAssembly memory. Always call .delete() on Mat objects to prevent memory leaks.

Reading Images

Displaying Images

Code Examples

Image Processing Pipeline

Face Detection in Browser

Real-time Webcam Processing

Working with Data

Image Transformations

Advanced Features

Using DNN Module

Performance Optimization

Performance Tip: Reuse Mat objects in loops instead of creating new ones. Only delete when completely done.

Common Issues

Memory Usage

Loading Files

OpenCV.js uses Emscripten’s virtual filesystem:

Build Configuration

Customize which modules to include:
Common build flags:
  • --build_wasm: Build WebAssembly version
  • --disable_single_file: Separate .wasm file
  • --enable_exception: Enable C++ exceptions
  • --build_test: Include test utilities

Resources

Next Steps