Created
December 17, 2017 18:44
-
-
Save diogocapela/9fd84200d8a7188eac23e63fef1d71f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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