Skip to content
cascade256 edited this page Dec 15, 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);

##Image Operations

###absDiff(image1, image2)

Sets this matrix to a image with the absolute difference between image1 and image2.

image1: Matrix

image2: Matrix

output: none

###adaptiveThreshold(maxVal, adaptiveMethod, thresholdType, blockSize, C)

Returns the thresholded image. This matrix needs to be a single channel image.

maxVal: number: This is the value that pixels are set to if the conditions are satisfied. 255 for white should be fine for most cases.

adaptiveMethod: number: This is how to set which method to use, 0 for ADAPTIVE_THRESH_MEAN_C, and 1 for ADAPTIVE_THRESH_GAUSSIAN_C.

thresholdType: number: This is the type of threshold to do, 0 for THRESH_BINARY and 1 for THRESH_BINARY_INV.

blockSize: number: This is the area around the pixel for comparing against.

C: number: This is subtracted from the mean of every pixel.

output: Matrix

##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