1 回答
TA贡献1810条经验 获得超5个赞
这看起来合适吗?我更改了uniform_array以使其创建新数组,而不是为每一行指向相同的数组。
const array = [
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
];
const kernel = [
[1,1,1],
[1,1,1],
[1,1,1],
];
function uniform_array(len, value) {
let arr = new Array(len); for (let i=0; i<len; ++i) arr[i] = Array.isArray(value) ? [...value] : value;
return arr;
}
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, ii, jj;
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
ii = i + (m - kCenterY);
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;
};
conv_2d(kernel, array).forEach(row => console.log(row.join(' ')));
添加回答
举报