-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPSE.java
2583 lines (2279 loc) · 109 KB
/
PSE.java
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import java.awt.BasicStroke;
import java.awt.geom.Line2D;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.Vector;
import java.util.Collections;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.ToolTipManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.BorderFactory;
import java.nio.charset.Charset;
public class PSE extends JFrame {
private final int borderX = 35;
private final int borderY = 55;
private int windowX = 1200;
private int windowY = 650;
private int timelineButton1X = 285;
private int timelineButton2X = 285;
private int timelineX = windowX - timelineButton1X - timelineButton2X;
private int timelineY = 40;
private int timelineButton1Y = timelineY;
private int timelineButton2Y = timelineY;
private int buttonX = 125;
private int buttonY = windowY - timelineY;
private int gridX = windowX - buttonX;
private int gridY = windowY - timelineY;
private JPanel timelinePanel;
private JPanel timelineButtonPanel1;
private JPanel timelineButtonPanel2;
private JPanel master;
private JPanel passaAltaPanel;
private JPanel passaBaixaPanel;
private JPanel naoLinearPanel;
private JPanel intensidadePanel;
private JPanel outrosPanel;
private JPanel diversosPanel;
private JPanel drawPanel;
private JPanel drawPanel2;
private JFrame histogramFrame;
private JFrame histogramFrame2;
private JPanel histogramPanel;
private JPanel histogramPanel2;
private BufferedImage originalImage;
private BufferedImage originalSecondImage;
private BufferedImage mainImage;
private BufferedImage secondImage;
private ArrayList<BufferedImage> imageHistory;
private JLabel mainImageLabel;
private JLabel secondImageLabel;
private JLabel histogramLabel;
private JLabel histogramLabel2;
private Boolean mustProcess = true;
private int lastProcessed = 0;
private int yPassaAlta = 500;
// Nomes das funções
private final String f1 = "Cinza";
private final String f2 = "Negativo";
private final String f3 = "Media";
private final String f4 = "Gaussiano";
private final String f5 = "Laplaciano";
private final String f6 = "Sobel";
private final String f7 = "Convolucao";
private final String f8 = "Brilho";
private final String f9 = "Contraste";
private final String f10 = "Limiar";
private final String f11 = "Cor";
private final String f12 = "Interpolar";
private final String f13 = "Linhas";
private final String f14 = "EMQ";
private final String f15 = "Histograma";
private final String f16 = "Linha";
private final String f17 = "Mediana";
private final String f18 = "Moda";
private final String f19 = "Minimo";
private final String f20 = "Maximo";
private final String f98 = "Tam. Original";
private final String f99 = "Resetar";
// Descrições para os botões
private final String opentip = "Clique para abrir uma imagem.";
private final String processtip = "Clique para processar a imagem seguindo a ordem definida no timeline (à direita).";
private final String savetip = "Clique para salvar a imagem atualmente sendo visualizada.";
private final String quittip = "Clique para fechar o programa. (Não salva a imagem!).";
private final String resettip = "Clique para resetar a imagem de volta à original e resetar o timeline.";
private final String sizetip = "Mostrar tamanho original:<br>(*Clique para ligar/desligar visualização da imagem em seu tamanho original*)";
private final String timelinetip = "Clique esquerdo para visualizar esta etapa.<br>Clique direito para remover esta etapa.";
private final String f1tip = "Escala de Cinza:<br><br>Transforma a imagem para tons de cinza.<br><br>Geralmente usada para preparar a imagem para outros filtros / transformações.";
private final String f2tip = "Filtro Negativo:<br><br>Inverte todos os tons da imágem.<br><br>Geralmente usado para transformar uma imagem obtida em sua forma negativa para a sua positiva (imagem normal).";
private final String f3tip = "Filtro de Média:<br><br>Percorre a imagem substituindo cada pixel pela média de seus vizinhos.<br><br>Geralmente usado para pre-processar a imagem, removendo ruído, para melhorar o resultado de processamentos subsequentes.";
private final String f4tip = "Filtro Gaussiano:<br><br>Percorre a imagem aplicando um efeito \"borrado\".<br><br>Geralmente usado para pre-processar a imagem, removendo ruído, para melhorar o resultado de processamentos subsequentes.";
private final String f5tip = "Operador de Laplace:<br><br>Percorre a imagem calculando a divergěncia de gradientes, identificando áreas de mudança rápida (bordas).<br><br>Geralmente usado para detecção de bordas, usualmente após operações que reduzem ruído / suavizam a imagem.";
private final String f6tip = "Operador de Sobel:<br><br>Percorre a imagem calculando as normais dos gradientes, identificando potenciais bordas.<br><br>Geralmente usado para detecção de bordas, usualmente após operações que reduzem ruído / suavizam a imagem. ";
private final String f7tip = "Filtro de Convolução:<br><br>Percorre a imagem substituindo cada pixel pela média ponderada de seus vizinhos a partir de uma matriz de convolução.<br><br>Filtro de propósito geral usado quando se quer um maior controle no processamento da imagem.";
private final String f8tip = "Filtro de Brilho:<br><br>Percorre a imagem aumentando ou reduzindo o brilho de cada pixel.<br><br>Geralmente usado para corrigir uma imagem que está muito clara ou escura, dificultando o seu processamento.";
private final String f9tip = "Filtro de Contraste:<br><br>Percorre a imagem aumentando ou reduzindo o contraste.<br><br>Geralmente usado para corrigir uma imagem que esta muito suave ou ruidosa.";
private final String f10tip = "Limiar Global Padrão:<br><br>Percorre a imagem para verificar a média do valor de intensidade dos pixels, e usa essa média para gerar uma nova imagem binária repartindo o pixels por esse valor. O limiar pode ser configurado para usar um valor fornecido, ao inves do valor da média.";
private final String f11tip = "Filtro de Cor:<br><br>Percorre a imagem verificando cada pixel, gerando uma imagem binária a partir daqueles que estiverem dentro do escopo de cor permitido.<br><br>Geralmente usado quando é fácil retirar da imagem a parte desejada pela sua cor distinta.";
private final String f12tip = "Interpolação:<br><br>Percorre a imagem e interpola os pixels para gerar uma nova imagem gerada a partir do fator de escala definido na configuração. <br><br> Geralmente usado para escalar uma imagem quando necessário.";
private final String f13tip = "Hough Linha:<br><br>";
private final String f14tip = "Erro Médio Quadrático:<br>(*Clique para calcular o EMQ da imagem atualmente sendo visualizada*)";
private final String f15tip = "Histograma:<br>(*Clique para ligar/desligar visualização do Histograma*)";
private final String f16tip = "Hough Linha:<br>(*Clique para calcular gerar a detecção de linhas pela transformada de Hough*)<br>";
private final String f17tip = "Filtro de mediana<br>Com uma máscara 3x3, percorre a imagem e atribui ao pixel central da máscara a mediana dos valores presentes na máscara";
private final String f18tip = "Filtro de moda<br>Com uma máscara 3x3, percorre a imagem e atribui ao pixel central da máscara a moda dos valores presentes na máscara";
private final String f19tip = "Filtro de mínimo<br>Com uma máscara 3x3, percorre a imagem e atribui ao pixel central da máscara o menor valor presente na máscara";
private final String f20tip = "Filtro de máximo<br>Com uma máscara 3x3, percorre a imagem e atribui ao pixel central da máscara o maior valor presente na máscara";
// Argumentos das funções que precisam deles
private int convolucaoLinhas = 3;
private int convolucaoColunas = 3;
private ArrayList<Integer> convolucaoPesos = new ArrayList<Integer>(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1));
private int brilhoFloat = 0;
private int contrasteFloat = 0;
private int[] filtroRGB = {0, 0, 0, 255, 255, 255};
private double limiarDouble = -1;
private double interpolacaoFator = 1.0;
private Boolean histogramOn = false;
private Boolean scaleOff = false;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
PSE prog = new PSE();
prog.setVisible(true);
});
}
public PSE() {
initUI();
}
private void initUI() {
// Main UI Window
// -------------------------------------------------------------------------
setTitle("PSE Image");
setLayout(new FlowLayout());
setSize(windowX + borderX, windowY + borderY);
getContentPane().setBackground(Color.DARK_GRAY);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Resizing
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent evt) {
Component component = (Component) evt.getSource();
Dimension size = component.getBounds().getSize();
windowX = (int) Math.round(size.getWidth()) - borderX;
windowY = (int) Math.round(size.getHeight()) - borderY;
timelineX = windowX - timelineButton1X - timelineButton2X;
timelineButton1Y = timelineY;
timelineButton2Y = timelineY;
buttonY = windowY - timelineY;
gridX = windowX - buttonX;
gridY = windowY - timelineY;
if (timelinePanel != null) {
timelinePanel.setPreferredSize(new Dimension(timelineX, timelineY));
timelinePanel.setSize(new Dimension(timelineX, timelineY));
timelinePanel.setMinimumSize(new Dimension(timelineX, timelineY));
timelinePanel.setMaximumSize(new Dimension(timelineX, timelineY));
timelinePanel.repaint();
timelinePanel.validate();
}
if (timelineButtonPanel1 != null) {
timelineButtonPanel1.setPreferredSize(new Dimension(timelineButton1X, timelineButton1Y));
timelineButtonPanel1.setSize(new Dimension(timelineButton1X, timelineButton1Y));
timelineButtonPanel1.setMinimumSize(new Dimension(timelineButton1X, timelineButton1Y));
timelineButtonPanel1.setMaximumSize(new Dimension(timelineButton1X, timelineButton1Y));
timelineButtonPanel1.repaint();
timelineButtonPanel1.validate();
}
if (timelineButtonPanel2 != null) {
timelineButtonPanel2.setPreferredSize(new Dimension(timelineButton2X, timelineButton2Y));
timelineButtonPanel2.setSize(new Dimension(timelineButton2X, timelineButton2Y));
timelineButtonPanel2.setMinimumSize(new Dimension(timelineButton2X, timelineButton2Y));
timelineButtonPanel2.setMaximumSize(new Dimension(timelineButton2X, timelineButton2Y));
timelineButtonPanel2.repaint();
timelineButtonPanel2.validate();
}
if (master != null) {
master.setPreferredSize(new Dimension(buttonX, buttonY));
master.setSize(new Dimension(buttonX, buttonY));
master.setMinimumSize(new Dimension(buttonX, buttonY));
master.setMaximumSize(new Dimension(buttonX, buttonY));
master.repaint();
master.validate();
}
if (drawPanel != null) {
drawPanel.setPreferredSize(new Dimension(gridX/2, gridY));
drawPanel.setSize(new Dimension(gridX/2, gridY));
drawPanel.setMinimumSize(new Dimension(gridX/2, gridY));
drawPanel.setMaximumSize(new Dimension(gridX/2, gridY));
drawPanel.repaint();
drawPanel.validate();
}
if (drawPanel2 != null) {
drawPanel2.setPreferredSize(new Dimension(gridX/2, gridY));
drawPanel2.setSize(new Dimension(gridX/2, gridY));
drawPanel2.setMinimumSize(new Dimension(gridX/2, gridY));
drawPanel2.setMaximumSize(new Dimension(gridX/2, gridY));
drawPanel2.repaint();
drawPanel2.validate();
}
component.repaint();
component.validate();
showImage();
}
}
);
// Histogram Frame & Panel
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
histogramFrame = new JFrame(f15);
histogramFrame.setLayout(new FlowLayout());
histogramFrame.setSize(new Dimension(800, 600));
histogramFrame.getContentPane().setBackground(Color.DARK_GRAY);
histogramFrame.setLocationRelativeTo(null);
histogramFrame.setAlwaysOnTop(false);
histogramFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
int x = (int) rect.getMaxX() - 2*(histogramFrame.getWidth());
int y = (int) rect.getMaxY() - histogramFrame.getHeight();
histogramFrame.setLocation(x, y);
histogramPanel = new JPanel();
histogramPanel.setPreferredSize(new Dimension(750, 550));
histogramFrame.add(histogramPanel);
// Histogram 2 Frame & Panel
histogramFrame2 = new JFrame("Histograma 2");
histogramFrame2.setLayout(new FlowLayout());
histogramFrame2.setSize(new Dimension(800, 600));
histogramFrame2.getContentPane().setBackground(Color.DARK_GRAY);
histogramFrame2.setLocationRelativeTo(null);
histogramFrame2.setAlwaysOnTop(false);
histogramFrame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
int x2 = (int) rect.getMaxX() - histogramFrame2.getWidth();
int y2 = (int) rect.getMaxY() - histogramFrame2.getHeight();
histogramFrame2.setLocation(x2, y2);
histogramPanel2 = new JPanel();
histogramPanel2.setPreferredSize(new Dimension(750, 550));
histogramFrame2.add(histogramPanel2);
// TimeLine Button Panel 1
// -------------------------------------------------------------------------
timelineButtonPanel1 = new JPanel();
timelineButtonPanel1.setPreferredSize(new Dimension(timelineButton1X, timelineButton1Y));
timelineButtonPanel1.setLayout(new GridLayout(1, 2));
timelineButtonPanel1.setBackground(Color.GRAY);
add(timelineButtonPanel1);
// Open Image
JButton openButton = new JButton("Abrir");
openButton.setToolTipText("<html><p width=\"300\">" + opentip + "</p></html>");
openButton.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - Abrir");
openImage();
});
openButton.setBackground(Color.WHITE);
timelineButtonPanel1.add(openButton);
// Process
JButton processButton = new JButton("Processar");
processButton.setToolTipText("<html><p width=\"300\">" + processtip + "</p></html>");
processButton.addActionListener((ActionEvent event) -> {
if (mainImage != null || secondImage != null) {
new ProcessFunctionsWorker().execute();
}
});
processButton.setBackground(Color.WHITE);
timelineButtonPanel1.add(processButton);
// TimeLine Panel
// -------------------------------------------------------------------------
timelinePanel = new JPanel();
timelinePanel.setPreferredSize(new Dimension(timelineX, timelineY));
timelinePanel.setLayout(new GridLayout(1, 10));
timelinePanel.setBackground(Color.GRAY);
add(timelinePanel);
// TimeLine Button Panel 2
// -------------------------------------------------------------------------
timelineButtonPanel2 = new JPanel();
timelineButtonPanel2.setPreferredSize(new Dimension(timelineButton2X, timelineButton2Y));
timelineButtonPanel2.setLayout(new GridLayout(1, 2));
timelineButtonPanel2.setBackground(Color.GRAY);
add(timelineButtonPanel2);
// Save Image
JButton saveButton = new JButton("Salvar");
saveButton.setToolTipText("<html><p width=\"300\">" + savetip + "</p></html>");
saveButton.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - Salvar");
saveImage();
});
saveButton.setBackground(Color.WHITE);
timelineButtonPanel2.add(saveButton);
// Exit Image
JButton exitButton = new JButton("Fechar");
exitButton.setToolTipText("<html><p width=\"300\">" + quittip + "</p></html>");
exitButton.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - Fechar");
System.exit(0);
});
exitButton.setBackground(Color.WHITE);
//timelineButtonPanel2.add(exitButton);
// Help Image
JButton helpButton = new JButton("Ajuda");
helpButton.setToolTipText("<html><p width=\"300\">" + quittip + "</p></html>");
helpButton.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - Ajuda");
openWebpage("https://github.com/rafaelkalan/Pse-Image/blob/master/Documentation.pdf");
});
helpButton.setBackground(Color.WHITE);
timelineButtonPanel2.add(helpButton);
// Button Panel Passa Alta
// -------------------------------------------------------------------------
passaAltaPanel = new JPanel();
passaAltaPanel.setPreferredSize(new Dimension(buttonX, 120));
passaAltaPanel.setLayout(new GridLayout(2, 1));
passaAltaPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Filtro Passa Alta"));
passaAltaPanel.setVisible(true);
// Button Panel PassaBaixa
passaBaixaPanel = new JPanel();
passaBaixaPanel.setPreferredSize(new Dimension(buttonX, 120));
passaBaixaPanel.setLayout(new GridLayout(3, 1));
passaBaixaPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Filtro Passa Baixa"));
passaBaixaPanel.setVisible(true);
naoLinearPanel = new JPanel();
naoLinearPanel.setPreferredSize(new Dimension(buttonX, 120));
naoLinearPanel.setLayout(new GridLayout(3,1));
naoLinearPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Filtro Nao-Linear"));
//Transformação de Intensidade Panel
intensidadePanel = new JPanel();
intensidadePanel.setPreferredSize(new Dimension(buttonX, buttonY));
intensidadePanel.setLayout(new GridLayout(5,1));
intensidadePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Trans. Intensidade"));
//Outros Panel
outrosPanel = new JPanel();
outrosPanel.setPreferredSize(new Dimension(buttonX, 120));
outrosPanel.setLayout(new GridLayout(3,1));
outrosPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Outros"));
//Diversos Panel
diversosPanel = new JPanel();
diversosPanel.setPreferredSize(new Dimension(buttonX, 120));
diversosPanel.setLayout(new GridLayout(4,1));
diversosPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Diversos"));
//Painel Master
master = new JPanel();
master.setLayout(new GridLayout(6,1));
master.setPreferredSize(new Dimension(buttonX, buttonY));
master.setBackground(Color.DARK_GRAY);
master.add(outrosPanel, new Integer(0), 0);
master.add(diversosPanel, new Integer(0), 0);
master.add(naoLinearPanel, new Integer(2), 0);
master.add(passaAltaPanel, new Integer(0), 0);
master.add(passaBaixaPanel, new Integer(1), 0);
master.add(intensidadePanel, new Integer(3), 0);
add(master);
// Func1
JButton f1Button = new JButton(f1);
f1Button.setToolTipText("<html><p width=\"300\">" + f1tip + "</p></html>");
f1Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f1);
addTimeline(f1);
});
// Func2
JButton f2Button = new JButton(f2);
f2Button.setToolTipText("<html><p width=\"300\">" + f2tip + "</p></html>");
f2Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f2);
addTimeline(f2);
});
// Func3
JButton f3Button = new JButton(f3);
f3Button.setToolTipText("<html><p width=\"300\">" + f3tip + "</p></html>");
f3Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f3);
addTimeline(f3);
});
// Func4
JButton f4Button = new JButton(f4);
f4Button.setToolTipText("<html><p width=\"300\">" + f4tip + "</p></html>");
f4Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f4);
addTimeline(f4);
});
// Func5
JButton f5Button = new JButton(f5);
f5Button.setToolTipText("<html><p width=\"300\">" + f5tip + "</p></html>");
f5Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f5);
addTimeline(f5);
});
// Func6
JButton f6Button = new JButton(f6);
f6Button.setToolTipText("<html><p width=\"300\">" + f6tip + "</p></html>");
f6Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f6);
addTimeline(f6);
});
// Func7 (Convolução)
JButton f7Button = new JButton(f7);
f7Button.setToolTipText("<html><p width=\"300\">" + f7tip + "</p></html>");
f7Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f7);
addTimeline(f7);
});
f7Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField xField = new JTextField(3);
JTextField yField = new JTextField(3);
JTextField weightField = new JTextField(9);
xField.setText("" + convolucaoLinhas);
yField.setText("" + convolucaoColunas);
weightField.setText(convolucaoPesos.toString().substring(1, convolucaoPesos.toString().length() - 1));
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
p1.add(new JLabel("Número de linhas da máscara:"));
p1.add(xField);
p1.add(Box.createHorizontalStrut(15)); // a spacer
p1.add(new JLabel("Número de colunas da máscara:"));
p1.add(yField);
parameters.add(p1);
parameters.add(new JLabel("Pesos da máscara separados por vírgula:"));
parameters.add(weightField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros do filtro de convolução", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
int tempLinhas = Integer.parseInt(xField.getText());
int tempColunas = Integer.parseInt(yField.getText());
if (tempLinhas < 2 || tempColunas < 2) {
JOptionPane.showMessageDialog(new JFrame(), "Número de linhas e colunas deve ser maior que 2!");
return;
}
if (tempLinhas % 2 != 1 || tempColunas % 2 != 1) {
JOptionPane.showMessageDialog(new JFrame(), "Número de linhas e colunas deve ser ímpar!");
return;
}
ArrayList tempPesos = new ArrayList();
String stringPesos[] = weightField.getText().split(",");
if (stringPesos.length != tempLinhas * tempColunas) {
JOptionPane.showMessageDialog(new JFrame(), "Número de pesos não esta de acordo com número de linhas e colunas!");
return;
}
convolucaoLinhas = tempLinhas;
convolucaoColunas = tempColunas;
for (int i = 0; i < stringPesos.length; i++) {
convolucaoPesos.add(Integer.parseInt(stringPesos[i].trim()));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func8 (Brilho)
JButton f8Button = new JButton(f8);
f8Button.setToolTipText("<html><p width=\"300\">" + f8tip + "</p></html>");
f8Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f8);
addTimeline(f8);
});
f8Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField xField = new JTextField(3);
xField.setText("" + brilhoFloat);
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(new JLabel("Brilho(%):"));
parameters.add(xField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros do filtro de brilho", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
int tempBrilho = Integer.parseInt(xField.getText());
brilhoFloat = tempBrilho;
System.out.println(brilhoFloat);
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func9 (Contraste)
JButton f9Button = new JButton(f9);
f9Button.setToolTipText("<html><p width=\"300\">" + f9tip + "</p></html>");
f9Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f9);
addTimeline(f9);
});
f9Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField xField = new JTextField(3);
xField.setText("" + contrasteFloat);
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(new JLabel("Contraste(%):"));
parameters.add(xField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros do filtro de contraste", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
int tempContraste = Integer.parseInt(xField.getText());
contrasteFloat = tempContraste;
System.out.println(contrasteFloat);
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func10
JButton f10Button = new JButton(f10);
f10Button.setToolTipText("<html><p width=\"300\">" + f10tip + "</p></html>");
f10Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f10);
addTimeline(f10);
});
f10Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField xField = new JTextField(3);
xField.setText("" + limiarDouble);
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(new JLabel("Valor de intensidade para o limiar:"));
parameters.add(xField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros do limiar global", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
Double tempDouble = Double.parseDouble(xField.getText());
if (tempDouble < 0 || tempDouble > 255) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros devem estar entre 0 e 255");
return;
}
limiarDouble = tempDouble;
System.out.println(limiarDouble);
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func11
JButton f11Button = new JButton(f11);
f11Button.setToolTipText("<html><p width=\"300\">" + f11tip + "</p></html>");
f11Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f11);
addTimeline(f11);
});
f11Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField rMinField = new JTextField(3);
JTextField gMinField = new JTextField(3);
JTextField bMinField = new JTextField(3);
JTextField rMaxField = new JTextField(3);
JTextField gMaxField = new JTextField(3);
JTextField bMaxField = new JTextField(3);
rMinField.setText("" + filtroRGB[0]);
gMinField.setText("" + filtroRGB[1]);
bMinField.setText("" + filtroRGB[2]);
rMaxField.setText("" + filtroRGB[3]);
gMaxField.setText("" + filtroRGB[4]);
bMaxField.setText("" + filtroRGB[5]);
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(new JLabel("Colocar valores de RGB (0-255 para cada):"));
JPanel minRGB = new JPanel();
parameters.add(minRGB);
minRGB.setLayout(new FlowLayout());
minRGB.add(new JLabel("Min:"));
minRGB.add(rMinField);
minRGB.add(gMinField);
minRGB.add(bMinField);
JPanel maxRGB = new JPanel();
parameters.add(maxRGB);
maxRGB.setLayout(new FlowLayout());
maxRGB.add(new JLabel("Max:"));
maxRGB.add(rMaxField);
maxRGB.add(gMaxField);
maxRGB.add(bMaxField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros do filtro de cor", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
filtroRGB = new int[6];
filtroRGB[0] = Integer.parseInt(rMinField.getText());
filtroRGB[1] = Integer.parseInt(gMinField.getText());
filtroRGB[2] = Integer.parseInt(bMinField.getText());
filtroRGB[3] = Integer.parseInt(rMaxField.getText());
filtroRGB[4] = Integer.parseInt(gMaxField.getText());
filtroRGB[5] = Integer.parseInt(bMaxField.getText());
for (int i = 0; i < filtroRGB.length; i++) {
if (filtroRGB[i] < 0 || filtroRGB[i] > 255) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros devem estar entre 0 e 255");
filtroRGB[0] = 0;
filtroRGB[1] = 0;
filtroRGB[2] = 0;
filtroRGB[3] = 255;
filtroRGB[4] = 255;
filtroRGB[5] = 255;
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func12
JButton f12Button = new JButton(f12);
f12Button.setToolTipText("<html><p width=\"300\">" + f12tip + "</p></html>");
f12Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f12);
addTimeline(f12);
});
f12Button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if (SwingUtilities.isLeftMouseButton(click)) {
JTextField xField = new JTextField(3);
xField.setText("" + interpolacaoFator);
JPanel parameters = new JPanel();
parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
parameters.add(new JLabel("Fator de escala para a interpolação:"));
parameters.add(xField);
int result = JOptionPane.showConfirmDialog(null, parameters,
"Parâmetros da interpolação", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
Double tempDouble = Double.parseDouble(xField.getText());
if (tempDouble <= 0) {
JOptionPane.showMessageDialog(new JFrame(), "Fator deve ser maior que 0");
return;
}
interpolacaoFator = tempDouble;
System.out.println(interpolacaoFator);
} catch (Exception e) {
JOptionPane.showMessageDialog(new JFrame(), "Parâmetros inválidos!");
}
}
}
}
});
// Func14 (EMQ)
JButton f14Button = new JButton(f14);
f14Button.setToolTipText("<html><p width=\"300\">" + f14tip + "</p></html>");
f14Button.addActionListener((ActionEvent event) -> {
if (mainImage != null) {
setTitle("PSE Image - " + f14);
JOptionPane.showMessageDialog(new JFrame(), calculoEMQ(originalImage, mainImage));
}
});
// Func15 (Histograma)
JToggleButton f15Button = new JToggleButton(f15);
f15Button.setToolTipText("<html><p width=\"300\">" + f15tip + "</p></html>");
f15Button.addItemListener((ItemEvent event) -> {
int state = event.getStateChange();
if (state == ItemEvent.SELECTED) {
histogramOn = true;
setTitle("PSE Image - " + f15);
histogramFrame.setVisible(true);
histogramFrame2.setVisible(true);
showImage();
showSecondImage();
} else {
histogramOn = false;
setTitle("PSE Image");
histogramFrame.setVisible(false);
histogramFrame2.setVisible(false);
}
});
// Func13
JButton f13Button = new JButton(f13);
f13Button.setToolTipText("<html><p width=\"300\">" + f13tip + "</p></html>");
f13Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f13);
addTimeline(f13);
});
// Func98 (Tamanho Original)
JToggleButton f98Button = new JToggleButton(f98);
f98Button.setToolTipText("<html><p width=\"300\">" + sizetip + "</p></html>");
f98Button.addItemListener((ItemEvent event) -> {
int state = event.getStateChange();
if (state == ItemEvent.SELECTED) {
scaleOff = true;
setTitle("PSE Image - " + f98);
showImage();
} else {
scaleOff = false;
setTitle("PSE Image");
showImage();
}
});
// Func16
JButton f16Button = new JButton(f16);
f16Button.setToolTipText("<html><p width=\"300\">" + f16tip + "</p></html>");
f16Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f16);
addTimeline(f16);
});
// Func17 (Mediana)
JButton f17Button = new JButton(f17);
f17Button.setToolTipText("<html><p width=\"300\">" + f17tip + "</p></html>");
f17Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f17);
addTimeline(f17);
});
// Func18 (Moda)
JButton f18Button = new JButton(f18);
f18Button.setToolTipText("<html><p width=\"300\">" + f18tip + "</p></html>");
f18Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f18);
addTimeline(f18);
});
// Func19 (Mínimo)
JButton f19Button = new JButton(f19);
f19Button.setToolTipText("<html><p width=\"300\">" + f19tip + "</p></html>");
f19Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f19);
addTimeline(f19);
});
// Func20 (Máximo)
JButton f20Button = new JButton(f20);
f20Button.setToolTipText("<html><p width=\"300\">" + f20tip + "</p></html>");
f20Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image - " + f20);
addTimeline(f20);
});
// Func99
JButton f99Button = new JButton(f99);
f99Button.setToolTipText("<html><p width=\"300\">" + resettip + "</p></html>");
f99Button.addActionListener((ActionEvent event) -> {
setTitle("PSE Image");
resetTimeline();
});
f99Button.setBackground(Color.WHITE);
// Add function buttons in desired order
//Filtros Passa Baixa
passaBaixaPanel.add(f3Button); // Media
passaBaixaPanel.add(f17Button); // Mediana
passaBaixaPanel.add(f4Button); // Gaussiano
//Filtro Passa Alta
passaAltaPanel.add(f5Button); // Laplaciano
passaAltaPanel.add(f6Button); // Sobel
//Filtros Nao lineares
naoLinearPanel.add(f20Button); // Máximo
naoLinearPanel.add(f19Button); // Mínimo
naoLinearPanel.add(f18Button); // Moda
//Transformacoes de Intensidade
intensidadePanel.add(f1Button); // Cinza
intensidadePanel.add(f9Button); // ;Contraste
intensidadePanel.add(f8Button); // Brilho
intensidadePanel.add(f10Button); // Limiar
intensidadePanel.add(f2Button); // Negativo
//Diversos Panel
diversosPanel.add(f12Button); // Interpolação
diversosPanel.add(f7Button); // Convolução]
diversosPanel.add(f11Button); // Cor
diversosPanel.add(f14Button); // EMQ
//Outros
outrosPanel.add(f15Button); // Histograma
outrosPanel.add(f98Button); // Tamanho original
outrosPanel.add(f99Button); // Resetar
// outrosPanel.add(f13Button); // Hough Linha
// outrosPanel.add(f16Button); // Hough Círculo
// outrosPanel.add(f16Button); // HoughLine
// Draw Panel
// -------------------------------------------------------------------------
drawPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(gridX, gridY));
drawPanel.setLayout(new BorderLayout());
drawPanel.setBackground(Color.GRAY);
drawPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Imagem alterada"));
add(drawPanel);
// Draw Panel 2
// -------------------------------------------------------------------------
drawPanel2 = new JPanel();
drawPanel2.setPreferredSize(new Dimension(gridX, gridY));
drawPanel2.setLayout(new BorderLayout());
drawPanel2.setBackground(Color.GRAY);
drawPanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Imagem original"));
add(drawPanel2);
// Tooltip configuration
ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
}
// Opens the help on the web
public static void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
// Opens a dialog to choose an image to open
private void openImage() {
JFileChooser imageChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Arquivos de imagem", "png", "jpg", "jpeg");
imageChooser.setFileFilter(filter);
int returnVal = imageChooser.showOpenDialog(drawPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
mainImage = ImageIO.read(imageChooser.getSelectedFile());
secondImage = ImageIO.read(imageChooser.getSelectedFile());
originalImage = mainImage;
showImage();
showSecondImage();
setTitle("PSE Image - " + imageChooser.getName(imageChooser.getSelectedFile()));
resetTimeline();
} catch (IOException e) {
}
}
}
private void showImage() {
if (mainImage != null) {
if (mainImageLabel != null) {
drawPanel.remove(mainImageLabel);
}
float drawWidth = (float) drawPanel.getSize().getWidth();
float drawHeight = (float) drawPanel.getSize().getHeight();
float imgWidth = mainImage.getWidth();
float imgHeight = mainImage.getHeight();
float imgRatio = imgWidth / imgHeight;
float drawRatio = drawWidth / drawHeight;
float imgWidthNew = imgWidth;
float imgHeightNew = imgHeight;
if (imgRatio > drawRatio) {
imgWidthNew = drawWidth;
imgHeightNew = imgHeight * (drawWidth / imgWidth);
} else if (imgRatio < drawRatio) {
imgWidthNew = imgWidth * (drawHeight / imgHeight);
imgHeightNew = drawHeight;
} else {
imgWidth = drawWidth;
imgHeight = drawHeight;
}
Image tempImage = mainImage.getScaledInstance(Math.round(imgWidthNew), Math.round(imgHeightNew), Image.SCALE_SMOOTH);
if (!scaleOff) {
mainImageLabel = new JLabel(new ImageIcon(tempImage));
} else {
mainImageLabel = new JLabel(new ImageIcon(mainImage));
}
drawPanel.add(mainImageLabel, BorderLayout.CENTER);
drawPanel.repaint();
drawPanel.validate();
if (histogramOn) {
drawWidth = (float) histogramPanel.getSize().getWidth();
drawHeight = (float) histogramPanel.getSize().getHeight();
BufferedImage mainHistogram = Histograma(mainImage);
imgWidth = mainHistogram.getWidth();
imgHeight = mainHistogram.getHeight();
imgRatio = imgWidth / imgHeight;
drawRatio = drawWidth / drawHeight;
imgWidthNew = imgWidth;
imgHeightNew = imgHeight;
if (imgRatio > drawRatio) {
imgWidthNew = drawWidth;
imgHeightNew = imgHeight * (drawWidth / imgWidth);
} else if (imgRatio < drawRatio) {
imgWidthNew = imgWidth * (drawHeight / imgHeight);
imgHeightNew = drawHeight;
} else {
imgWidth = drawWidth;
imgHeight = drawHeight;
}
tempImage = mainHistogram.getScaledInstance(750, 550, Image.SCALE_SMOOTH);
if (histogramLabel != null) {
histogramPanel.remove(histogramLabel);
}
histogramLabel = new JLabel(new ImageIcon(tempImage));