Skip to content
btsimonh edited this page Nov 4, 2017 · 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:number, cols:number): Matrix

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


Matrix.Eye(rows: number, cols: number, type?: number): Matrix

Creates an identity matrix.


Matrix.Zeros(rows: number, cols: number, type?: number): Matrix

Creates a matrix of zeros.


Matrix.Ones(rows: number, cols: number, type?: number): Matrix

Creates a matrix of ones.


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: Matrix, image2: Matrix)

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


adaptiveThreshold(maxVal: number, adaptiveMethod: number, thresholdType: number, blockSize: number, C: number): Matrix

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

MaxVal is the value that pixels are set to if the conditions are satisfied. 255 for white should be fine for most cases. AdaptiveMethod is how to set which method to use, 0 for ADAPTIVE_THRESH_MEAN_C, and 1 for ADAPTIVE_THRESH_GAUSSIAN_C. ThresholdType is the type of threshold to do, 0 for THRESH_BINARY and 1 for THRESH_BINARY_INV. BlockSize is the area around the pixel for comparing against. This must be an odd number. C is a constant that is subtracted from the mean of every pixel.


addWeighted(img: Matrix, alpha: number, output: Matrix, beta: number)


adjustROI(dtop: number, dbottom: number, dleft: number, dright: number)


bilateralFilter()

bilateralFilter(d: number, sigmaColor: number, sigmaSpace: number, borderType?: number)


bitwiseAnd(img: Matrix, output: Matrix, mask?: Matrix)


bitwiseNot(output: Matrix, mask?: Matrix)


bitwiseXor(img: Matrix, output: Matrix, mask?: Matrix)


brightness(diff: number)

brightness(alpha: number, beta: number)


canny(lowThresh: number, highThresh: number)


convertGrayscale()


convertHSVscale()


convertTo(output: Matrix, rtype: number, alpha?: number, beta?: number)


crop(x: number, y: number, width: number, height: number): Matrix


cvtColor(conversionCode: string)


dilate(iterations: number, kernel?: Matrix)


drawAllContours(contours: Contour, color?: Array<number>, thickness?: number)


drawContour(contour: Contour, index: number, color?: Array<number>, thickness?: number)


ellipse(x: number, y: number, width: number, height: number, color?: Array<number>, thickness?: number)

ellipse(options: EllipseOptions)


empty(): boolean


equalizeHist()


erode(iterations: number, kernel?: Matrix)


fillPoly(polygons: Array<Array<number>>, color?: Array<number>)


findContours(mode?: number, chain?: number): Contour


flip(flipCode: number): Matrix


floodFill(options: FloodFillOptions): number


##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: number, col: number): number

Gets the number at that point in the matrix.


row(i: number): [number]

Returns the array of numbers for row i.


col(i: number): [number]

Returns the array of numbers for column i.


toBuffer(cb: ()=>any, opt?: BufferOptions): Buffer

Saves the matrix as an image formal (default JPEG - see BufferOptions above) and returns it in a NodeJS 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: string, cb, ()=>any): number

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.

Clone this wiki locally