Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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