Created
April 21, 2013 22:54
-
-
Save tonussi/5431413 to your computer and use it in GitHub Desktop.
Detector de Bordas Recebe o valor de uma máscara (matriz 3x3 de inteiros) e acessa uma memória onde está armazenado um bloco de nxn pixels (imagem de referência). Com base nos pixels da imagem de referência e na máscara, remonta uma imagem com cada pixel multiplicado pela máscara. Há duas memórias: M1, que armazena a máscara de referência, e M2,…
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
package bd; | |
public class BlocoControle { | |
private int dimensaoMatrixMascara; | |
private int dimensaoVetorCodigo; | |
private int dimensaoImagemReferencia; | |
private byte[][] imagemProcessada; | |
BlocoControle() { | |
this.dimensaoMatrixMascara = 3; | |
this.dimensaoVetorCodigo = 2; | |
} | |
public void operacoes(byte[][] mascara, byte[][] imagemReferencia) { | |
byte[][] matrizParcial = new byte[dimensaoMatrixMascara][dimensaoMatrixMascara]; | |
if (testamatriz(imagemReferencia)) { | |
dimensaoImagemReferencia = imagemReferencia.length; | |
for (int i = 0; i < dimensaoImagemReferencia; i = +dimensaoMatrixMascara) { | |
for (int j = 0; j < dimensaoImagemReferencia; j = +dimensaoMatrixMascara) { | |
matrizParcial = Mascara.multiplique(mascara, | |
retornaMatrizParcial(i, j, imagemReferencia)); | |
montaMatrizResultado(matrizParcial, i, j); | |
} | |
} | |
} | |
} | |
public boolean testamatriz(byte[][] a) { | |
int j = 1; | |
if (a.length % dimensaoMatrixMascara == 0) { | |
j--; | |
for (int i = 0; i < dimensaoVetorCodigo; i++) { | |
if ((a[i].length % dimensaoVetorCodigo == 0)) { | |
j++; | |
} | |
} | |
} | |
return j == 0; | |
} | |
public void montaMatrizResultado(byte[][] parcial, int l, int c) { | |
int i = 0, j = 0; | |
while (i < dimensaoMatrixMascara) { | |
while (j < dimensaoMatrixMascara) { | |
imagemProcessada[i + l][j + c] = parcial[i][j]; | |
j++; | |
} | |
i++; | |
} | |
} | |
public byte[][] retornaMatrizParcial(int linha, int coluna, byte[][] matriz) { | |
byte[][] matrizParcial = new byte[dimensaoMatrixMascara][dimensaoMatrixMascara]; | |
int i = 0, j = 0; | |
while (linha < dimensaoVetorCodigo) { | |
while (coluna < dimensaoVetorCodigo) { | |
matrizParcial[i][j] = matriz[linha + i][coluna + j]; | |
j++; | |
} | |
i++; | |
} | |
return matrizParcial; | |
} | |
} |
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
package bd; | |
public class Main { | |
public static void main(String[] args) { | |
TestBench tb = new TestBench(); | |
tb.setUp(); | |
tb.testGeneral(); | |
} | |
} |
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
package bd; | |
public class Mascara { | |
private short dimensao = 3; | |
private byte[][] resultado; | |
public Mascara(byte[][] mascara, byte[][] operando) { | |
assert testaMatrizes(mascara, operando) : "Falha nas Matrizes"; | |
byte soma = 0; | |
for (byte i = 0; i < dimensao; i++) { | |
for (byte j = 0; j < dimensao; j++) { | |
for (byte k = 0; k < dimensao; k++) { | |
soma = (byte) (soma + mascara[i][k] * operando[k][j]); | |
} | |
resultado[i][j] = soma; | |
soma = 0; | |
} | |
} | |
} | |
public Mascara() {} | |
public boolean testaMatrizes(byte[][] a, byte[][] b) { | |
assert ((a.length == dimensao) || (b.length == dimensao)) : "Falha nas Linhas" | |
+ a.length + " , " + b.length; | |
for (int i = 0; i < dimensao; i++) | |
assert (!(a[i].length == 0) && (b[i].length == 0)) : "Falha nas Colunas"; | |
return true; | |
} | |
public static byte[][] multiplique(byte[][] mascara, byte[][] operando) { | |
return new Mascara(mascara, operando).resultado; | |
} | |
} |
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
package bd; | |
import static java.lang.Math.*; | |
public class TestBench extends junit.framework.TestCase { | |
private BlocoControle controle; | |
private Mascara mascara; | |
private byte[][] a, b; | |
public void setUp() { | |
a = new byte[(int) random() * 100][(int) random() * 100]; | |
b = new byte[(int) random() * 100][(int) random() * 100]; | |
fill(a); | |
fill(b); | |
controle = new BlocoControle(); | |
mascara = new Mascara(a, b); | |
a = new byte[((int) random() * 1000) % 10][((int) random() * 1000) % 10]; | |
b = new byte[((int) random() * 10) % 3][((int) random() * 10) % 3]; | |
fill(a); | |
fill(b); | |
controle = new BlocoControle(); | |
mascara = new Mascara(a, b); | |
a = new byte[((int) random() * 100) % 3][((int) random() * 100) % 3]; | |
b = new byte[((int) random() * 100) % 3][((int) random() * 100) % 3]; | |
fill(a); | |
fill(b); | |
controle = new BlocoControle(); | |
mascara = new Mascara(a, b); | |
} | |
public void testGeneral() { | |
mascara.testaMatrizes(a, b); | |
int l = 0; | |
byte[][] parcial = null; | |
fill(parcial); | |
int c = 0; | |
controle.montaMatrizResultado(parcial, l, c); | |
byte[][] matriz = null; | |
fill(matriz); | |
int coluna = 0; | |
int linha = 0; | |
controle.retornaMatrizParcial(linha, coluna, matriz); | |
mascara.testaMatrizes(a, b); | |
l = 3; | |
parcial = new byte[((int) random() * 100) % 3][((int) random() * 100) % 3]; | |
fill(parcial); | |
c = 3; | |
controle.montaMatrizResultado(parcial, l, c); | |
matriz = new byte[((int) random() * 100) % 3][((int) random() * 100) % 3]; | |
fill(matriz); | |
coluna = 3; | |
linha = 3; | |
controle.retornaMatrizParcial(linha, coluna, matriz); | |
} | |
public void fill(byte[][] f) { | |
for (int i = 0; i < f.length; i++) { | |
for (int j = 0; j < f[i].length; j++) { | |
f[i][j] = (byte) ((random() * 100) % 2); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment