Que está mal con mi código?
Publicado por Marcos (25 intervenciones) el 02/05/2020 23:08:59
Estoy tratando de hacer la matriz de convolución pero al momento de ejecutar los filtros no se muestran como debería ser, les adjunto parte del código
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class ComandoConvolucionar implements IComando {
private Imagen imagen;
private int kernel[][] = {
{-2, -1, 0},
{-1, 1, 1},
{0, 1, 2}
};
@Override
public void setImagen(Imagen img) {
imagen = img;
}
@Override
public void ejecutar() {
calculoConvolucion(imagen, kernel);
}
public Imagen calculoConvolucion(Imagen imagen, int[][] kernel) {
this.imagen = imagen;
int[][] pixeles = imagen.getPixeles();
int tope = kernel.length / 2;
for (int i = tope; i < pixeles.length - tope; i++) {
for (int j = tope; j < pixeles[0].length - tope; j++) {
pixeles[i][j] = convolucionar(imagen.getPixeles(), kernel, i, j);
}
}
this.imagen.setPixeles(pixeles);
return this.imagen;
}
public int convolucionar(int[][] imagen, int[][] kernel, int fila, int columna) {
int tope = kernel.length / 2; //variable que sirve de control para evitar que se desborde la mascara de la matriz
//short pixel = 0;
int b = 0;
int g = 0;
int r = 0;
int factor = 1;
for (int i = 0; i < kernel.length; i++) {
for (int j = 0; j < kernel[0].length; j++) {
factor += kernel[i][j];
b += blue(imagen[fila - tope + i][columna - tope + j]) * kernel[i][j];
g += green(imagen[fila - tope + i][columna - tope + j]) * kernel[i][j];
r += red(imagen[fila - tope + i][columna - tope + j]) * kernel[i][j];
}
}
if (factor > 0) {
b /= factor;
g /= factor;
r /= factor;
}
return (r << 16) | ((g << 8) | b);
}
public int blue(int b) {
int blue = b & 0x000000ff;
return blue;
}
public int green(int g) {
int green = (g >>> 8) & 0x000000ff;
return green;
}
public int red(int r) {
int red = (r >>> 16) & 0x000000ff;
return red;
}
Valora esta pregunta


0