JavaScript 数组的 2D 卷积

我对 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]


MYYA
浏览 131回答 1
1回答

森栏

这看起来合适吗?我更改了uniform_array以使其创建新数组,而不是为每一行指向相同的数组。const array = [&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],&nbsp; [1, 2, 3, 4, 5, 6, 7, 8, 9],];const kernel = [&nbsp; [1,1,1],&nbsp; [1,1,1],&nbsp; [1,1,1],];function uniform_array(len, value) {&nbsp; &nbsp; let arr = new Array(len); for (let i=0; i<len; ++i) arr[i] = Array.isArray(value) ? [...value] : value;&nbsp; &nbsp; return arr;}function conv_2d(kernel, array){&nbsp; &nbsp; var result = uniform_array(array.length, uniform_array(array[0].length, 0));&nbsp; &nbsp; var kRows = kernel.length;&nbsp; &nbsp; var kCols = kernel[0].length;&nbsp; &nbsp; var rows = array.length;&nbsp; &nbsp; var cols = array[0].length;&nbsp; &nbsp; // find center position of kernel (half of kernel size)&nbsp; &nbsp; var kCenterX = Math.floor(kCols/2);&nbsp; &nbsp; var kCenterY = Math.floor(kRows/2);&nbsp; &nbsp; var i, j, m, n, ii, jj;&nbsp; &nbsp; for(i=0; i < rows; ++i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for all rows&nbsp; &nbsp; &nbsp; &nbsp; for(j=0; j < cols; ++j){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for all columns&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(m=0; m < kRows; ++m){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// for all kernel rows&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(n=0; n < kCols; ++n){&nbsp; &nbsp; &nbsp; &nbsp; // for all kernel columns&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // index of input signal, used for checking boundary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ii = i + (m - kCenterY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jj = j + (n - kCenterX);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ignore input samples which are out of bound&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ii >= 0 && ii < rows && jj >= 0 && jj < cols){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i][j] += array[ii][jj] * kernel[m][n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; };&nbsp; &nbsp; return result;};conv_2d(kernel, array).forEach(row => console.log(row.join(' ')));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript