我正在使用JSFeat计算机视觉库,我正在尝试将图像转换为灰度.函数jsfeat.imgproc.grayscale输出到矩阵(下面的img_u8),其中每个元素都是0到255之间的整数.我不确定如何将这个矩阵应用于原始图像,所以我在https://inspirit.github中查看了他们的示例. io/jsfeat/sample_grayscale.htm.
下面是我将图像转换为灰度的代码.我采用他们的方法更新原始图像中的像素,但我不明白它是如何工作的.
/**
* I understand this stuff
*/
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let img = document.getElementById('img-in');
ctx.drawImage(img, 0, 0, img.width, img.height);
let imageData = ctx.getImageData(0, 0, img.width, img.height);
let img_u8 = new jsfeat.matrix_t(img.width, img.height, jsfeat.U8C1_t);
jsfeat.imgproc.grayscale(imageData.data, img.width, img.height, img_u8);
let data_u32 = new Uint32Array(imageData.data.buffer);
let i = img_u8.cols*img_u8.rows, pix = 0;
/**
* Their logic to update the pixel values of the original image
* I …Run Code Online (Sandbox Code Playgroud)