-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessador.java
More file actions
341 lines (273 loc) · 13.2 KB
/
Copy pathProcessador.java
File metadata and controls
341 lines (273 loc) · 13.2 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.concurrent.ThreadLocalRandom;
public final class Processador {
public static BufferedImage ensureRgb(BufferedImage src) {
if (src.getType() == BufferedImage.TYPE_INT_RGB) {
return src;
}
BufferedImage out = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = out.createGraphics();
g.drawImage(src, 0, 0, null);
g.dispose();
return out;
}
public static BufferedImage paraPB(BufferedImage image) {
int altura = image.getHeight();
int largura = image.getWidth();
for (int lin = 0; lin < largura; lin++) {
for (int col = 0; col < altura; col++) {
int pixel = image.getRGB(lin, col);
Color cor = new Color(pixel);
int pixelCorCinza = (int) ((cor.getRed() * .3) + (cor.getGreen() * .6) + (cor.getBlue() * .1));
Color novaCor = new Color(pixelCorCinza);
Color cinza = new Color(novaCor.getBlue(), novaCor.getBlue(), novaCor.getBlue());
image.setRGB(lin, col, cinza.getRGB());
}
}
return image;
}
public static BufferedImage binarizacao(BufferedImage image, int limiar) {
BufferedImage imagemBin = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
int altura = imagemBin.getHeight();
int largura = imagemBin.getWidth();
for (int lin = 0; lin < largura; lin++) {
for (int col = 0; col < altura; col++) {
int pixel = image.getRGB(lin, col);
Color cor = new Color(pixel);
if (cor.getRed() >= limiar) {
imagemBin.setRGB(lin, col, Color.white.getRGB());
} else {
imagemBin.setRGB(lin, col, Color.black.getRGB());
}
}
}
return imagemBin;
}
public static double[][] gerarKernel(int tamanhoLado) {
double[][] kernel = new double[tamanhoLado][tamanhoLado];
double peso = 1.0 / (tamanhoLado * tamanhoLado);
for (int i = 0; i < tamanhoLado; i++) {
for (int j = 0; j < tamanhoLado; j++) {
kernel[i][j] = peso;
}
}
return kernel;
}
public static BufferedImage aplicarConvolucao(BufferedImage imagem, double[][] kernel) {
int width = imagem.getWidth();
int height = imagem.getHeight();
int tamanhoLado = kernel.length;
int deslocamento = tamanhoLado / 2;
BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = deslocamento; y < height - deslocamento; y++) {
for (int x = deslocamento; x < width - deslocamento; x++) {
double sumR = 0;
double sumG = 0;
double sumB = 0;
for (int ky = 0; ky < tamanhoLado; ky++) {
for (int kx = 0; kx < tamanhoLado; kx++) {
int sampleX = x + (kx - deslocamento);
int sampleY = y + (ky - deslocamento);
Color c = new Color(imagem.getRGB(sampleX, sampleY));
double peso = kernel[ky][kx];
sumR += c.getRed() * peso;
sumG += c.getGreen() * peso;
sumB += c.getBlue() * peso;
}
}
int r = Math.min(255, Math.max(0, (int) Math.round(sumR)));
int g = Math.min(255, Math.max(0, (int) Math.round(sumG)));
int b = Math.min(255, Math.max(0, (int) Math.round(sumB)));
out.setRGB(x, y, new Color(r, g, b).getRGB());
}
}
return out;
}
public static BufferedImage filtroMedia(BufferedImage imagem, int tamanhoLado) {
double[][] kernelMedia = gerarKernel(tamanhoLado);
return aplicarConvolucao(imagem, kernelMedia);
}
public static BufferedImage aplicarFiltro(BufferedImage imagem, double[] filtro, boolean usarValorAbsoluto) {
int width = imagem.getWidth();
int height = imagem.getHeight();
BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int col = 1; col < height - 1; col++) {
for (int lin = 1; lin < width - 1; lin++) {
double sumR = 0;
double sumG = 0;
double sumB = 0;
int index = 0;
for (int fCol = -1; fCol <= 1; fCol++) {
for (int fLin = -1; fLin <= 1; fLin++) {
Color c = new Color(imagem.getRGB(lin + fLin, col + fCol));
sumR += c.getRed() * filtro[index];
sumG += c.getGreen() * filtro[index];
sumB += c.getBlue() * filtro[index];
index++;
}
}
int r;
int g;
int b;
if (usarValorAbsoluto) {
r = Math.min(255, Math.max(0, (int) Math.abs(sumR)));
g = Math.min(255, Math.max(0, (int) Math.abs(sumG)));
b = Math.min(255, Math.max(0, (int) Math.abs(sumB)));
} else {
r = Math.min(255, Math.max(0, (int) Math.round(sumR)));
g = Math.min(255, Math.max(0, (int) Math.round(sumG)));
b = Math.min(255, Math.max(0, (int) Math.round(sumB)));
}
out.setRGB(lin, col, new Color(r, g, b).getRGB());
}
}
return out;
}
public static BufferedImage aplicarFiltroDuplo(BufferedImage imagem, double[] filtroH, double[] filtroV) {
int width = imagem.getWidth();
int height = imagem.getHeight();
BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int col = 1; col < height - 1; col++) {
for (int lin = 1; lin < width - 1; lin++) {
double sumHr = 0;
double sumHg = 0;
double sumHb = 0;
double sumVr = 0;
double sumVg = 0;
double sumVb = 0;
int idx = 0;
for (int fCol = -1; fCol <= 1; fCol++) {
for (int fLin = -1; fLin <= 1; fLin++) {
Color c = new Color(imagem.getRGB(lin + fLin, col + fCol));
sumHr += c.getRed() * filtroH[idx];
sumHg += c.getGreen() * filtroH[idx];
sumHb += c.getBlue() * filtroH[idx];
sumVr += c.getRed() * filtroV[idx];
sumVg += c.getGreen() * filtroV[idx];
sumVb += c.getBlue() * filtroV[idx];
idx++;
}
}
double magR = Math.sqrt(sumHr * sumHr + sumVr * sumVr);
double magG = Math.sqrt(sumHg * sumHg + sumVg * sumVg);
double magB = Math.sqrt(sumHb * sumHb + sumVb * sumVb);
int r = Math.min(255, Math.max(0, (int) Math.round(magR)));
int g = Math.min(255, Math.max(0, (int) Math.round(magG)));
int b = Math.min(255, Math.max(0, (int) Math.round(magB)));
out.setRGB(lin, col, new Color(r, g, b).getRGB());
}
}
return out;
}
public static BufferedImage filtroNitidez(BufferedImage imagem) {
double[] nitidez = {0, -1, 0, -1, 5, -1, 0, -1, 0};
return aplicarFiltro(imagem, nitidez, false);
}
public static BufferedImage filtroPrewittHorizontal(BufferedImage imagem) {
double[] prewittHorizontal = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
return aplicarFiltro(imagem, prewittHorizontal, true);
}
public static BufferedImage filtroPrewittVertical(BufferedImage imagem) {
double[] prewittVertical = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
return aplicarFiltro(imagem, prewittVertical, true);
}
public static BufferedImage filtroSobelHorizontal(BufferedImage imagem) {
double[] sobelHorizontal = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
return aplicarFiltro(imagem, sobelHorizontal, true);
}
public static BufferedImage filtroSobelVertical(BufferedImage imagem) {
double[] sobelVertical = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
return aplicarFiltro(imagem, sobelVertical, true);
}
public static BufferedImage filtroLaplace(BufferedImage imagem) {
double[] laplace = {0, -1, 0, -1, 4, -1, 0, -1, 0};
return aplicarFiltro(imagem, laplace, false);
}
public static BufferedImage filtroGaussiano(BufferedImage imagem) {
double[] gauss = {
1.0 / 16, 2.0 / 16, 1.0 / 16,
2.0 / 16, 4.0 / 16, 2.0 / 16,
1.0 / 16, 2.0 / 16, 1.0 / 16
};
return aplicarFiltro(imagem, gauss, false);
}
public static BufferedImage filtroSobelDuplo(BufferedImage imagem) {
double[] sobelGx = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
double[] sobelGy = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
return aplicarFiltroDuplo(imagem, sobelGx, sobelGy);
}
public static BufferedImage filtroPrewittDuplo(BufferedImage imagem) {
double[] prewittH = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
double[] prewittV = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
return aplicarFiltroDuplo(imagem, prewittH, prewittV);
}
public static BufferedImage rotacionarImagem(BufferedImage imagem, double angulo) {
int largura = imagem.getWidth();
int altura = imagem.getHeight();
double radianos = Math.toRadians(angulo);
double seno = Math.sin(radianos);
double cosseno = Math.cos(radianos);
int novaLargura = (int) Math.ceil(Math.abs(largura * cosseno) + Math.abs(altura * seno));
int novaAltura = (int) Math.ceil(Math.abs(largura * seno) + Math.abs(altura * cosseno));
BufferedImage imagemRotacionada = new BufferedImage(novaLargura, novaAltura, BufferedImage.TYPE_INT_RGB);
double centroColuna = largura / 2.0;
double centroLinha = altura / 2.0;
double novoCentroColuna = novaLargura / 2.0;
double novoCentroLinha = novaAltura / 2.0;
for (int linha = 0; linha < altura; linha++) {
for (int coluna = 0; coluna < largura; coluna++) {
double deslocamentoColuna = coluna - centroColuna;
double deslocamentoLinha = linha - centroLinha;
int novaColuna = (int) Math.round(deslocamentoColuna * cosseno - deslocamentoLinha * seno + novoCentroColuna);
int novaLinha = (int) Math.round(deslocamentoColuna * seno + deslocamentoLinha * cosseno + novoCentroLinha);
if (novaColuna >= 0 && novaColuna < novaLargura && novaLinha >= 0 && novaLinha < novaAltura) {
int cor = imagem.getRGB(coluna, linha);
imagemRotacionada.setRGB(novaColuna, novaLinha, cor);
}
}
}
return imagemRotacionada;
}
public static BufferedImage aplicarRuidoSalPimenta(BufferedImage imagem, double intensidade) {
int width = imagem.getWidth();
int height = imagem.getHeight();
BufferedImage imagemComRuido = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color corOriginal = new Color(imagem.getRGB(x, y));
double valorAleatorio = rnd.nextDouble();
if (valorAleatorio < intensidade / 2.0) {
imagemComRuido.setRGB(x, y, Color.BLACK.getRGB());
} else if (valorAleatorio < intensidade) {
imagemComRuido.setRGB(x, y, Color.WHITE.getRGB());
} else {
imagemComRuido.setRGB(x, y, corOriginal.getRGB());
}
}
}
return imagemComRuido;
}
public static BufferedImage aplicarRuidoGaussiano(BufferedImage imagem, double desvioPadrao) {
int width = imagem.getWidth();
int height = imagem.getHeight();
BufferedImage imagemComRuido = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color cor = new Color(imagem.getRGB(x, y));
int vermelho = limitar(cor.getRed() + (int) Math.round(rnd.nextGaussian() * desvioPadrao));
int verde = limitar(cor.getGreen() + (int) Math.round(rnd.nextGaussian() * desvioPadrao));
int azul = limitar(cor.getBlue() + (int) Math.round(rnd.nextGaussian() * desvioPadrao));
Color novaCor = new Color(vermelho, verde, azul);
imagemComRuido.setRGB(x, y, novaCor.getRGB());
}
}
return imagemComRuido;
}
private static int limitar(int value) {
return Math.max(0, Math.min(255, value));
}
}