JavaScript image processing: Matrix Basic method

Source: Internet
Author: User
Tags tostring

Objective

In the previous article, we defined the matrix, this article we will add some common methods to the matrix.

ToString Method

The ToString method is often used as a description of the object as a string, so we define this method as an output matrix element.

Mat.prototype.toString = function () {
    var tempdata = this.data,
        text = "Mat (" + This.type + ") = {\ n",
        num = this . Col * This.channel;
    for (var i = 0; i < This.row i++) {
        text = ' [For
        (var j = 0; J < num; J + +) {
            Text = = (Tempdata[i * num + j] + ",");
        }
        Text + "]\n";
    }
    Text + = "}";
    return text;

In this way, we can go through:

Console.log (MAT);

To output the matrix.

Clone method

In fact, we can clone operations through constructors, but still provide a way to facilitate memory and use.

Mat.prototype.clone = function () {return
    new Mat (This.row, This.col, this.data);

Gets the specified element

We have two ways to get the matrix element.

Array method

Because the mat actually holds the data as an array, the data looks like this:

R00 G00 B00 A00 R01 G01 B01 A01 ... r0n g0n b0n a0n

R10 G10 B10 A10 R11 G11 B11 A11 ... r1n g1n b1n a1n

......

Rm0 Gm0 Bm0 Am0 Rm1 Gm1 Bm1 Am1 ... RMN Gmn Bmn Amn

Where capital R, G, B, and a represent the values of each channel, and the subscript first represents the line number, and the second represents the column number. That is, line K, the G-Channel value of column J is gkj.

We can easily get to a mat type of mat, the K line, each element of the J-column Pixel is:

Rkj = mat.data[(k * Mat.col + j) * 4 + 0]

Gkj = mat.data[(k * Mat.col + j) * 4 + 1]

Bkj = mat.data[(k * Mat.col + j) * 4 + 2]

AKJ = mat.data[(k * Mat.col + j) * 4 + 3]

Buffer Partial Reference method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.