opencv4nodejs
By its nature, JavaScript lacks the performance to implement Computer Vision tasks efficiently. Therefore this package brings the performance of the native OpenCV library to your Node.js application. Supports all OpenCV 3 versions. The bindings are available as an asynchronous (via promises or callbacks) and synchronous API.
The ultimate goal of this project is to provide a collection of Node.js bindings, which supports the entire OpenCV and OpenCV-contrib API. An overview of available bindings can be found in the API Documentation. The implementation of missing bindings can be specifically prioritized by requesting new features. Furthermore, contribution is highly appreciated. If you want to get involved you can have a look at the contribution guide.
- Examples
- How to install
- Usage with Docker
- Usage with Electron
- Quick Start
- Async API
- Available Modules
- Request new Features
Examples
See examples for implementation.
Face Detection
Face Recognition
Check out my article about face recognition.
Hand Gesture Recognition
Check out my article about fingertip detection.
Object Recognition with Deep Neural Networks
Check out my article about using OpenCV's Deep Neural Networks with Node.js.
Tensorflow Inception
Single Shot Multibox Detector with COCO
Machine Learning
Check out my article about recognition of handwritten letters using Histogram of oriented Gradients (HOG) and Support Vector Machines (SVM).
Object Tracking
Feature Matching
Image Histogram
How to install
$ npm install --save opencv4nodejs
Make sure to have OpenCV 3+ ( extra modules are optional ) installed on your System https://github.com/opencv/opencv/releases/. In case you are running on Windows or have OpenCV set up in a custom directory make sure to set the following environment variables:
- OPENCV_DIR pointing to the root path containing include directory or set OPENCV_INCLUDE_DIR explicitly.
- OPENCV_LIB_DIR pointing to the library dir containing the OpenCV .lib, .so or .dylib files.
On Windows
On windows you will need the windows build tools to compile opencv4nodejs:
npm install --global windows-build-tools
Also you will need to add the OpenCV binaries to your system path:
- Add an environment variable OPENCV_BIN_DIR pointing to the library dir containing the OpenCV .dll files.
- Append
;%OPENCV_BIN_DIR%;
to your path variable.
Note: Restart your current console session after making changes to your environment.
If you are running into issues also check the requirements for node-gyp specific to your OS: https://github.com/nodejs/node-gyp.
Usage with Docker
opencv-express - example for opencv4nodejs with express.js and docker
Or simply pull from justadudewhohacks/opencv-nodejs for opencv-3.2 + contrib-3.2 with opencv4nodejs globally installed:
FROM justadudewhohacks/opencv-nodejs
Different OpenCV 3.x base images can be found here: https://hub.docker.com/r/justadudewhohacks/.
Usage with Electron
opencv-electron - example for opencv4nodejs with electron
Add the following script to your package.json:
"electron-rebuild": "electron-rebuild -w opencv4nodejs"
Run the script:
$ npm run electron-rebuild
Require it in the application:
const electron = ;const cv = electronremote;
Quick Start
const cv = ;
Initializing Mat (image matrix), Vec, Point
const rows = 100; // heightconst cols = 100; // width// empty Matconst emptyMat = rows cols cvCV_8UC3;// fill the Mat with default valueconst whiteMat = rows cols cvCV_8UC1 255;const blueMat = rows cols cvCV_8UC3 255 0 0;// from array (3x3 Matrix, 3 channels)const matData =255 0 0 255 0 0 255 0 00 0 0 0 0 0 0 0 0255 0 0 255 0 0 255 0 0;const matFromArray = matData cvCV_8UC3;// from node bufferconst charData = 255 0 ...;const matFromArray = charData rows cols cvCV_8UC3;// Pointconst pt2 = 100 100;const pt3 = 100 100 05;// Vectorconst vec2 = 100 100;const vec3 = 100 100 05;const vec4 = 100 100 05 05;
Mat and Vec operations
const mat0 = ...;const mat1 = ...;// arithmetic operations for Mats and Vecsconst matMultipliedByScalar = mat0; // scalar multiplicationconst matDividedByScalar = mat0; // scalar divisionconst mat0PlusMat1 = mat0; // additionconst mat0MinusMat1 = mat0; // subtractionconst mat0MulMat1 = mat0; // elementwise multiplicationconst mat0DivMat1 = mat0; // elementwise division// logical operations Mat onlyconst mat0AndMat1 = mat0;const mat0OrMat1 = mat0;const mat0bwAndMat1 = mat0;const mat0bwOrMat1 = mat0;const mat0bwXorMat1 = mat0;const mat0bwNot = mat0;
Accessing Mat data
const matBGR = ... cvCV_8UC3;const matGray = ... cvCV_8UC1;// get pixel value as vector or number valueconst vec3 = matBGR;const grayVal = matGray;// get raw pixel value as arrayconst b g r = matBGR;// set single pixel valuesmatBGR;matBGR;matGray;// get a 25x25 sub region of the Mat at offset (50, 50)const width = 25;const height = 25;const region = matBGR;// get a node buffer with raw Mat dataconst matAsBuffer = matBGR;// get entire Mat data as JS arrayconst matAsArray = matBGR;
IO
// load image from fileconst mat = cv;cv// save imagecv;cv// show imagecv;cv;// open capture from webcamconst devicePort = 0;const wCap = devicePort;// open video captureconst vCap = './path/video.mp4';// read frames from captureconst frame = vCap;vCap;// loop through the captureconst delay = 10;let done = false;while !donelet frame = vCap;// loop back to start on end of stream reachedif frameemptyvCap;frame = vCap;// ...const key = cv;done = key !== 255;
Useful Mat methods
const matBGR = ... cvCV_8UC3;// convert typesconst matSignedInt = matBGR;const matDoublePrecision = matBGR;// convert color spaceconst matGray = matBGR;const matHSV = matBGR;const matLab = matBGR;// resizeconst matHalfSize = matBGR;const mat100x100 = matBGR;const matMaxDimIs100 = matBGR;// extract channels and create Mat from channelsconst matB matG matR = matBGR;const matRGB = matR matB matG;
Drawing a Mat into HTML Canvas
const img = ...// convert your image to rgba color spaceconst matRGBA = imgchannels === 1? img: img;// create new ImageData from raw mat dataconst imgData =matRGBAimgcolsimgrows;// set canvas dimensionsconst canvas = document;canvasheight = imgrows;canvaswidth = imgcols;// set image dataconst ctx = canvas;ctx;
Method Interface
OpenCV method interface from official docs or src:
void
translates to:
const src = ...;// invoke with required argumentsconst dst0 = src;// with optional paramatersconst dst2 = src;// or pass specific optional parametersconst optionalArgs =borderType: cvBORDER_CONSTANT;const dst2 = src;
Async API
The async API can be consumed by passing a callback as the last argument of the function call. By default, if an async method is called without passing a callback, the function call will yield a Promise.
Async Face Detection
const classifier = cvHAAR_FRONTALFACE_ALT2;// by nesting callbackscv;// via Promisecv;// using async awaittryconst img = await cv;const grayImg = await img;const objects numDetections = await classifier;...catch errconsole;
Available Modules
API doc overview
- core
- io
- imgproc
- ximgproc
- objdetect
- machinelearning
- deepneuralnetworks
- video
- calib3d
- photo
- features2d
- xfeatures2d
- tracking
- text
- face
Request new Features
Are you missing some functions from OpenCV (overview of available bindings) or have an idea for utility in regards to the OpenCV API, which you would like to be added to this package? No problem! Open a new issue with a listing of the desired function bindings or features and you will find them in this package soon.