【问题标题】:Need to smooth an image using matrix - Calculate sum of cells and compute average需要使用矩阵平滑图像 - 计算细胞总和并计算平均值
【发布时间】:2019-04-08 15:05:36
【问题描述】:

我有一个矩阵,每个单元格都包含我需要计算单元格及其相邻单元格的数字,并得出这些单元格的平均值。

看看我写的代码。

public Matrix imageFilterAverage() {
        for (int i=0; i < _array.length;i++)
            for (int j=0; i < _array[i].length;j++){
                _array[i][j] = (_array[i][j] + _array[i][j+1] + _array[i+1][j] + _array[i+1][j+1]) / 4;
            }

        return this;
    }

当 j+1 达到 3 时,我的代码在索引问题上返回错误,因为单元格是 0 1 2,所以它超出了界限。

例如,如果我有这样的矩阵

10 5 7 3
50 3 2 1
60 2 5 2

结果矩阵应该是这样的。

17 12 3 3
21 16 2 3
28 20 2 2

我已经发布了图像一个矩阵源和矩阵结果

非常感谢您的时间和帮助。

【问题讨论】:

  • 你写了你需要做的事,但这不是问题??
  • 我该如何解决这个问题?
  • 问题是你的 for 循环走得太远了,或者你没有检查你的界限。您自己已经说明了这一点-当 j+1 为 3 时,它会失败,因为单元格是 0、1、2。您必须阻止 j 上升到 2,否则您必须在单元格 j+1 结束时跳过添加单元格界限。
  • 我不明白你是如何计算这些结果的。您能否解释一下,例如,如何将 [1][3] 处的数组元素从 230 变为 98?
  • 您计算该单元格周围的所有单元格,包括其自身,然后除以单元格数以获得平均值,然后将该平均值放回该单元格中。

标签: java matrix


【解决方案1】:
    /**
         * Takes the given array and transforms each slot in the array as an average of the slots around it.
         * @return an array with each where each slot in the array is "blurred" by the slots around it.
         */
        public Matrix imageFilterAverage() {
            int avgArray[][] = new int[_twoDiPicture.length][];
            int numOfCellsAround = 0;
            int cellsSum = 0;
            for (int y = 0; y < _twoDiPicture.length; y++) {
                avgArray[y] = new int[_twoDiPicture[y].length];
                for (int x = 0; x < _twoDiPicture[y].length; x++) {
                    numOfCellsAround = 0;
                    cellsSum = 0;
                    numOfCellsAround += cellsAround(y, x);
                    cellsSum += cellsSum(y, x);
                    avgArray[y][x] = cellsSum / numOfCellsAround;
                }
            }

            return new Matrix(avgArray);
        }

/* a private method that  deals with index out of bound exceptions. */
    private boolean isInBounds(int y, int x) {
        return y < _twoDiPicture.length && y >= 0 && x < _twoDiPicture[y].length  && x >= 0;
    }

    /* A private methods that uses "isInBounds" to find how many cells are surrounding the target array. */
    private int cellsAround(int y, int x) {
        int cells = 1;

        if (isInBounds(y + 1, x)) {
            cells++;
        }
        if (isInBounds(y - 1, x)) {
            cells++;
        }
        if (isInBounds(y, x + 1)) {
            cells++;
        }
        if (isInBounds(y, x - 1)) {
            cells++;
        }
        if (isInBounds(y - 1, x + 1)) {
            cells++;
        }
        if (isInBounds(y - 1, x - 1)) {
            cells++;
        }
        if (isInBounds(y + 1, x - 1)) {
            cells++;
        }
        if (isInBounds(y + 1, x + 1)) {
            cells++;
        }

        return cells;
    }


    /*A private method that returns the sum of all the adjacent cells around target cell. */
    private int cellsSum(int y, int x) {
        int sum = _twoDiPicture[y][x];

        if (isInBounds(y + 1, x)) {
            sum += _twoDiPicture[y + 1][x];
        }
        if (isInBounds(y - 1, x)) {
            sum += _twoDiPicture[y - 1][x];
        }
        if (isInBounds(y, x + 1)) {
            sum += _twoDiPicture[y][x + 1];
        }
        if (isInBounds(y, x - 1)) {
            sum += _twoDiPicture[y][x - 1];
        }
        if (isInBounds(y - 1, x + 1)) {
            sum += _twoDiPicture[y - 1][x + 1];
        }
        if (isInBounds(y - 1, x - 1)) {
            sum += _twoDiPicture[y - 1][x - 1];
        }
        if (isInBounds(y + 1, x - 1)) {
            sum += _twoDiPicture[y + 1][x - 1];
        }
        if (isInBounds(y + 1, x + 1)) {
            sum += _twoDiPicture[y + 1][x + 1];
        }

        return sum;
    }

【讨论】:

    【解决方案2】:

    我有一个丑陋的解决方案可以改进:

    public static void main(String[] args) {
        int[][] matrix = {{10, 5, 7, 3},
                          {50, 3, 2, 1},
                          {60, 2, 5, 2}};
    
        int[][] average = new int[matrix.length][matrix[0].length];
        for(int i = 0; i< matrix.length; i++){
            for(int j = 0; j< matrix[0].length; j++){
                int sum = 0;
                int div =  ((i==0 && j ==0) || 
                            (i==0 && j == matrix[0].length-1) || 
                            (i== matrix.length-1 && j ==0)|| 
                            (i==  matrix.length-1 && j == matrix[0].length-1)) ? 4 : 
                           ((i==0 && j > 0) || 
                            (i>0 && j == 0) || 
                            (i== matrix.length-1 && j >0)|| 
                            (i>  0 && j == matrix[0].length-1))? 6 : 9;
    
                for(int k = Math.max(i-1, 0); k <= Math.min(i+1, matrix.length-1); k++){
                    for(int t = Math.max(j-1, 0); t <= Math.min(j+1, matrix[0].length-1); t++){
                        sum += matrix[k][t];
                    }
                }
                average[i][j] = sum / div;
            }
        }
        for(int[] r:average){
            System.out.println(Arrays.toString(r));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多