Skip to content
cascade256 edited this page Oct 23, 2015 · 8 revisions

Matrix

The matrix is the most useful base data structure in OpenCV. Things like images are just matrices of pixels.

Creating:

Matrix(rows, cols)

Creates a matrix with the specified number of rows and columns.

rows: number, cols: number

output: Matrix


Matrix.Eye(rows, cols, type)

Creates an identity matrix.

rows: number, cols: number, type: number (optional)

output: Matrix


Matrix.Zeros(rows, cols, type)

Creates a matrix of zeros.

rows: number, cols: number, type: number (optional)

output: Matrix


Matrix.Ones(rows, cols, type)

Creates a matrix of ones.

rows: number, cols: number, type: number (optional)

output: Matrix


Example:

var image = new Matrix(256, 256);
var identity = new Matrix.Eye(512, 512);
var zeroes = new Matrix.Zeroes(512, 512);
var ones = new Matrix.Ones(512, 512);

##Accessing Data For color images, be careful with get, row, and col. They return a number that represents the pixel by bit-shifting all the channels together.

get(row, col)

Gets the number at that point in the matrix.

row: number, col: number

output: number


row(i)

Returns the array of numbers for row i.

i: number

output: array


col(i)

Returns the array of numbers for column i.

i: number

output: array


toBuffer(cb, opt)

Saves the matrix as JPEG and returns it in a NodeJS buffer.

cb: function (optional),

opt: BufferOptions (optional)

output: Buffer


Example:

var pixel = image.get(0,0);
var row = image.row(0);
var col = image.col(0);
var buff = image.toBuffer();

##Saving

save(path, cb)

Saves the matrix as an image at the path specified, with an optional callback. The path must end with a valid image extension. The output number is 0 for failed, and 1 for success.

path: string, cb: function

output: number

Clone this wiki locally