【问题标题】:Not understanding 2D Matrix algorithm question in JavaScript不理解 JavaScript 中的 2D 矩阵算法问题
【发布时间】:2020-09-08 19:04:49
【问题描述】:

我不确定如何处理这个算法问题。

给定一个表示图像灰度的二维整数矩阵M,需要设计一个平滑器,使每个单元格的灰度变为周围所有8个单元格及其自身的平均灰度(向下取整)。如果一个单元格周围的单元格少于 8 个,则尽可能多地使用。

示例 1:

Input:
[[1, 1, 1],
[1, 0, 1],
[1, 1, 1]]

Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

解释:

对于点 (0, 0), (0, 2), (2, 0), (2, 2): floor(3 / 4) = floor(0.75) = 0

对于点 (0, 1), (1, 0), (1, 2), (2, 1): floor(5 / 6) = floor(0.83333333) = 0

对于点 (1, 1): floor(8 / 9) = floor(0.88888889) = 0

注意: 给定矩阵中的值在 [0, 255] 的范围内。 给定矩阵的长度和宽度在[1, 150]的范围内。

问题:

我的实现逻辑有问题: 我正在尝试检查所有邻居,我觉得好像我的逻辑受限,但我收到错误 Uncaught TypeError: Cannot read property '0' of undefined

到目前为止我写的代码:

const imageSmoother = (M) => {
    for (let rows = 0; rows < M.length; rows++) {
        for (let cols = 0; cols < M[0].length; cols++) {
            let val = M[rows][cols]
            let neighborsCount = 0

            if (rows >= 0 && rows < M.length &&
                cols >= 0 && cols < M[0].length) {
                //check right
                if (M[rows][cols + 1] !== undefined) {
                    val += M[rows][cols + 1]
                    neighborsCount++
                }
                //check left
                if (M[rows][cols - 1] !== undefined) {
                    val += M[rows][cols - 1]
                    neighborsCount++
                }
                //check bottom
                if (M[rows + 1][cols] !== undefined) {
                    val += M[rows + 1][cols]
                    neighborsCount++
                }
                //check above
                if (M[rows - 1][cols] !== undefined) {
                    val += M[rows - 1][cols]
                    neighborsCount++
                }
                //check diagonal top left
                if (M[rows - 1][cols - 1] !== undefined) {
                    val += M[rows - 1][cols - 1]
                    neighborsCount++
                }
                //check diagonal bottom right
                if (M[rows + 1][cols + 1] !== undefined) {
                    val += M[rows + 1][cols + 1]
                    neighborsCount++
                }
                //check diagonal bottom left
                if (M[rows + 1][cols - 1] !== undefined) {
                    val += M[rows + 1][cols - 1]
                    neighborsCount++
                }
                //check diagonal top right
                if (M[rows - 1][cols + 1] !== undefined) {
                    val += M[rows - 1][cols + 1]
                    neighborsCount++
                }
                console.log(val, neighborsCount);
            }

        }
    }
}

测试用例如下:

console.log(imageSmoother([[1, 1, 1],
[1, 0, 1],
[1, 1, 1]])) // [[0, 0, 0],
            //   [0, 0, 0],
            //   [0, 0, 0]]

【问题讨论】:

    标签: javascript java arrays algorithm matrix


    【解决方案1】:

    这是我能想到的最好的解决方案。不确定是否有更好的方法,但它似乎有效:

    const imageSmoother = (M) => {
        let result = Array(M.length)
        for (let i = 0; i < result.length; i++) {
            result[i] = Array(M[0].length)
        }
    
        for (let rows = 0; rows < M.length; rows++) {
            for (let cols = 0; cols < M[0].length; cols++) {
                let val = M[rows][cols]
                let neighborsCount = 0
    
                //check right
                if (M[rows][cols + 1] !== undefined) {
                    val += M[rows][cols + 1]
                    neighborsCount++
                }
                //check left
                if (M[rows][cols - 1] !== undefined) {
                    val += M[rows][cols - 1]
                    neighborsCount++
                }
                //check bottom
                if (Array.isArray(M[rows + 1]) && M[rows + 1][cols] !== undefined) {
                    val += M[rows + 1][cols]
                    neighborsCount++
                }
                //check above
                if (M[rows - 1] && M[rows - 1][cols] !== undefined) {
                    val += M[rows - 1][cols]
                    neighborsCount++
                }
                //check diagonal top left
                if (M[rows - 1] && M[rows - 1][cols - 1] !== undefined) {
                    val += M[rows - 1][cols - 1]
                    neighborsCount++
                }
                //check diagonal bottom right
                if (M[rows + 1] && M[rows + 1][cols + 1] !== undefined) {
                    val += M[rows + 1][cols + 1]
                    neighborsCount++
                }
                //check diagonal bottom left
                if (M[rows + 1] && M[rows + 1][cols - 1] !== undefined) {
                    val += M[rows + 1][cols - 1]
                    neighborsCount++
                }
                //check diagonal top right
                if (M[rows - 1] && M[rows - 1][cols + 1] !== undefined) {
                    val += M[rows - 1][cols + 1]
                    neighborsCount++
                }
                result[rows][cols] = Math.floor(val / (neighborsCount + 1))
            }
        }
        return result
    }
    

    【讨论】:

      【解决方案2】:

      也许让我们一起走过几个步骤,为您指明正确的方向。我们从以下矩阵开始:

      Input:
      [[1, 1, 1],
      [1, 0, 1],
      [1, 1, 1]]
      

      我们要做的是查看给定条目及其邻居。

      例如,让我们从左上角的数字开始:1,为了清楚起见,我将用引号括起来。

      [['1', 1, 1],
      [1, 0, 1],
      [1, 1, 1]]
      

      它“接触”了 3 个邻居(相邻)——一个在右侧,一个在正下方,一个在对角线的右侧和下方:

      [['1', "1", 1],
      ["1", "0", 1],
      [1, 1, 1]]
      

      他们要求我们考虑突出显示的数字并将它们平均在一起,然后在最后四舍五入。

      所以要取平均值,我们将 1) 将邻居 + 自身相加,2) 除以我们相加的数字的数量(平均值见 formula),然后 3) 我们将四舍五入为问题问(地板)。

      (1+1+1+0) / 4 = 0.75

      现在让我们向下取整:

      floor(0.75) = 0.

      所以左上角的单元格应该是0

      然后,您将为每个数字执行此操作。为了澄清我所说的“触摸”是什么意思,下一步是计算这个单元格:

      [[1, '1', 1],
      [1, 0, 1],
      [1, 1, 1]]
      

      其中有以下 5 个邻居(包括自身在内 6 个):

      [["1", '1', "1"],
       ["1", "0", "1"],
       [1, 1, 1]]
      

      【讨论】:

      • 我现在对这个问题有了更好的理解,但是我在实现逻辑时遇到了麻烦,我已经将代码发布在我修改后的问题中,您有什么建议吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2021-07-20
      • 1970-01-01
      • 2017-07-16
      • 2023-04-08
      • 1970-01-01
      • 2016-05-11
      相关资源
      最近更新 更多