Нужно сгладить изображение с помощью матрицы - рассчитать сумму ячеек и вычислить среднее - PullRequest
0 голосов
/ 08 апреля 2019

У меня есть матрица, каждая ячейка содержит число, которое мне нужно, чтобы вычислить ячейку и соседние ячейки и получить среднее значение для этих ячеек.

Посмотрите на код, который я написал.

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

Я разместил изображения с одним источником матрицы и результатами матрицы source matrix

Results matrix

Большое спасибо за ваше время и помощь.

Ответы [ 2 ]

1 голос
/ 08 апреля 2019
    /**
         * 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;
    }
0 голосов
/ 08 апреля 2019

У меня есть ужасное решение, которое можно улучшить:

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));
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...