Asynchronous OpenCV bindings for Node.js with a simple promise based API and good documentation
JavaScript
simple-cv
Asynchronous OpenCV bindings for Node.js with a simple promise based API
and good documentation.
simple-cv replicates the good parts of the OpenCV API but replaces the really crappy ones with something
better. For example instead of a flip method that takes a number literal -1, 0 or 1 to indicate flip direction
simple-cv has flipLeftRight and flipUpDown methods.
All heavy operations are performed in a worker thread pool and the results a returned back asynchronously using
promises.
All methods have a synchronous equivalent. Just append Sync to the function name.
The documentation is full of examples but here's one to get you started:
letmatrix=newcv.Matrix({width: 3,height: 2,type: cv.ImageType.Float,data: [1,2,3,4,5,6]});// The same using the cv.matrix shorthand:matrix=cv.matrix({width: 3,height: 2,type: cv.ImageType.Float,data: [1,2,3,4,5,6]});
cv.Matrix(rows)
Creates a cv.ImageType.Float matrix.
property
type
description
rows
Array<Array
The values as a list of rows
letmatrix=newcv.Matrix([[1,2,3],[4,5,6],[7,8,9]]);// The same using the cv.matrix shorthand:matrix=cv.matrix([[1,2,3],[4,5,6],[7,8,9]]);
Methods
array = matrix.toArray()
Returns an array with the matrice's values in row-major order.
return value
type
description
array
Array
The matrix values in row-major order.
constarray=matrix.toArray();
promise = matrix.crop(rect)
Cuts a rectangular piece of the matrix and returns it as a new matrix. The original matrix is not modified.
The resize parameters or a number. If a number is given it will become the width of the image while keeping the aspect ratio. See ResizeParams for other options.
return value
type
description
promise
Promise
The resized image
// Resize the image to have width 400 while preserving aspect ratio.letresized=awaitcv.resize(image,400);// This does exactly the same as the previous example.resized=awaitcv.resize(image,{width: 400});// Resize the image to have height 400 while preserving aspect ratio.resized=awaitcv.resize(image,{height: 400});// Resize the image half the width and height.resized=awaitcv.resize(image,{scale: 0.5});// Resize the image to 320x200resized=awaitcv.resize(image,{width: 320,height: 200});// Create a truly weird image.resized=awaitcv.resize(image,{xScale: 0.5,yScale: 20.0});
// Rotate 20 degrees around the center of the image.letrotated=awaitcv.rotate(image,20);// Rotate 30 degrees around point (10, 20)rotated=awaitcv.rotate(image,{x: 10,y: 20,angle: 30});
Gray scale image. The underlying OpenCV data type is CV_8UC1.
BGR
BGR color image. The underlying OpenCV data type is CV_8UC3.
BGRA
BGRA color image with an alpha channel. The underlying OpenCV data type is CV_8UC4.
Float
Floating point matrix. The underlying OpenCV data type is CV_64FC1
constGray=cv.ImageType.Gray;
EncodeType
value
description
PNG
PNG format.
JPEG
JPEG format.
constJPEG=cv.EncodeType.JPEG;
BorderType
Describes how to fill the empty space created by some operations like rotate.
value
description
Replicate
`aaaaaa
Reflect
`fedcba
Reflect101
`gfedcb
Wrap
`cdefgh
Constant
`iiiiii
constReplicate=cv.BorderType.Replicate;
Channel
value
description
Gray
Gray channel
Red
Red channel
Green
Green channel
Blue
Blue channel
Alpha
Alpha channel
Float
Floating point channel
constGray=cv.Channel.Gray;
Types
Rectangle
Any javascript object with properties x, y, width and height. Note that the Rect class
instances have these properties and can be passed as Rectangles.
property
type
description
x
number
x-coordinate
y
number
y-coordinate
width
number
width
height
number
height
constrect={x: 10,y: 20,width: 100,height: 100};
Point
A javascript object with properties x and y.
property
type
description
x
number
x-coordinate
y
number
y-coordinate
constpoint={x: 10,y: 20};
Color
A javascript object with properties red, green, blue and optionally alpha.