Skip to content

Instantly share code, notes, and snippets.

@diogocapela
Created December 17, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diogocapela/9fd84200d8a7188eac23e63fef1d71f7 to your computer and use it in GitHub Desktop.
Save diogocapela/9fd84200d8a7188eac23e63fef1d71f7 to your computer and use it in GitHub Desktop.
/**
* Aplicar filtro da média a uma matriz.
*/
public int[][] filterMedia(int[][] matrix) {
int[][] novaMatrix = new int[matrix.length][matrix.length];
int[][] directions = new int[][] {
{-1, -1},
{-1, 0},
{-1, 1},
{ 0, -1},
{ 0, 1},
{ 1, -1},
{ 1, 0},
{ 1, 1}
};
for(int i = 0; i < matrix.length; i++) {
for(int k = 0; k < matrix.length; k++) {
int soma = 0;
int contador = 0;
for(int d = 0; d < directions.length; d++) {
int positionI = i + directions[d][0];
int positionK = k + directions[d][1];
if(positionI >= 0 && positionI < matrix.length && positionK >= 0 && positionK < matrix.length) {
soma = soma + matrix[positionI][positionK];
contador++;
}
}
novaMatrix[i][k] = soma / contador;
}
}
return novaMatrix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment