我对 JavaScript 还很陌生。我正在尝试在 Web 应用程序的 JavaScript 中实现
function conv_2d(kernel, array){
var result = uniform_array(array.length, uniform_array(array[0].length, 0));
var kRows = kernel.length;
var kCols = kernel[0].length;
var rows = array.length;
var cols = array[0].length;
// find center position of kernel (half of kernel size)
var kCenterX = Math.floor(kCols/2);
var kCenterY = Math.floor(kRows/2);
var i, j, m, n, mm, nn;
for(i=0; i < rows; ++i){ // for all rows
for(j=0; j < cols; ++j){ // for all columns
for(m=0; m < kRows; ++m){ // for all kernel rows
for(n=0; n < kCols; ++n){ // for all kernel columns
// index of input signal, used for checking boundary
var ii = i + (m - kCenterY);
var jj = j + (n - kCenterX);
// ignore input samples which are out of bound
if(ii >= 0 && ii < rows && jj >= 0 && jj < cols){
result[i][j] += array[ii][jj] * kernel[m][n];
};
};
};
};
};
return result;
};
function uniform_array(len, value) {
let arr = new Array(len); for (let i=0; i<len; ++i) arr[i] = value;
return arr;
}
现在,我试图看看我做错了什么,但我找不到错误。我所知道的是,对同一对矩阵应用 2D 卷积,javascript 中的结果给出了输出矩阵中每行的所有行的总和。我发现与 C++ 中的输出相比:
JavaScript 输出:
0: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
1: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
2: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
3: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
4: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
5: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
6: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
7: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
8: (9) [75, 150, 225, 300, 375, 450, 525, 600, 425]
森栏
相关分类