Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/java/tutorial_code/ImgProc/threshold/Threshold.java
16354 views
1
import java.awt.BorderLayout;
2
import java.awt.Container;
3
import java.awt.Image;
4
5
import javax.swing.BoxLayout;
6
import javax.swing.ImageIcon;
7
import javax.swing.JFrame;
8
import javax.swing.JLabel;
9
import javax.swing.JPanel;
10
import javax.swing.JSlider;
11
import javax.swing.event.ChangeEvent;
12
import javax.swing.event.ChangeListener;
13
14
import org.opencv.core.Core;
15
import org.opencv.core.Mat;
16
import org.opencv.highgui.HighGui;
17
import org.opencv.imgcodecs.Imgcodecs;
18
import org.opencv.imgproc.Imgproc;
19
20
public class Threshold {
21
private static int MAX_VALUE = 255;
22
private static int MAX_TYPE = 4;
23
private static int MAX_BINARY_VALUE = 255;
24
private static final String WINDOW_NAME = "Threshold Demo";
25
private static final String TRACKBAR_TYPE = "<html><body>Type: <br> 0: Binary <br> "
26
+ "1: Binary Inverted <br> 2: Truncate <br> "
27
+ "3: To Zero <br> 4: To Zero Inverted</body></html>";
28
private static final String TRACKBAR_VALUE = "Value";
29
private int thresholdValue = 0;
30
private int thresholdType = 3;
31
private Mat src;
32
private Mat srcGray = new Mat();
33
private Mat dst = new Mat();
34
private JFrame frame;
35
private JLabel imgLabel;
36
37
public Threshold(String[] args) {
38
//! [load]
39
String imagePath = "../data/stuff.jpg";
40
if (args.length > 0) {
41
imagePath = args[0];
42
}
43
// Load an image
44
src = Imgcodecs.imread(imagePath);
45
if (src.empty()) {
46
System.out.println("Empty image: " + imagePath);
47
System.exit(0);
48
}
49
// Convert the image to Gray
50
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY);
51
//! [load]
52
53
//! [window]
54
// Create and set up the window.
55
frame = new JFrame(WINDOW_NAME);
56
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57
// Set up the content pane.
58
Image img = HighGui.toBufferedImage(srcGray);
59
addComponentsToPane(frame.getContentPane(), img);
60
// Use the content pane's default BorderLayout. No need for
61
// setLayout(new BorderLayout());
62
// Display the window.
63
frame.pack();
64
frame.setVisible(true);
65
//! [window]
66
}
67
68
private void addComponentsToPane(Container pane, Image img) {
69
if (!(pane.getLayout() instanceof BorderLayout)) {
70
pane.add(new JLabel("Container doesn't use BorderLayout!"));
71
return;
72
}
73
74
JPanel sliderPanel = new JPanel();
75
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
76
77
//! [trackbar]
78
sliderPanel.add(new JLabel(TRACKBAR_TYPE));
79
// Create Trackbar to choose type of Threshold
80
JSlider sliderThreshType = new JSlider(0, MAX_TYPE, thresholdType);
81
sliderThreshType.setMajorTickSpacing(1);
82
sliderThreshType.setMinorTickSpacing(1);
83
sliderThreshType.setPaintTicks(true);
84
sliderThreshType.setPaintLabels(true);
85
sliderPanel.add(sliderThreshType);
86
87
sliderPanel.add(new JLabel(TRACKBAR_VALUE));
88
// Create Trackbar to choose Threshold value
89
JSlider sliderThreshValue = new JSlider(0, MAX_VALUE, 0);
90
sliderThreshValue.setMajorTickSpacing(50);
91
sliderThreshValue.setMinorTickSpacing(10);
92
sliderThreshValue.setPaintTicks(true);
93
sliderThreshValue.setPaintLabels(true);
94
sliderPanel.add(sliderThreshValue);
95
//! [trackbar]
96
97
//! [on_trackbar]
98
sliderThreshType.addChangeListener(new ChangeListener() {
99
@Override
100
public void stateChanged(ChangeEvent e) {
101
JSlider source = (JSlider) e.getSource();
102
thresholdType = source.getValue();
103
update();
104
}
105
});
106
107
sliderThreshValue.addChangeListener(new ChangeListener() {
108
@Override
109
public void stateChanged(ChangeEvent e) {
110
JSlider source = (JSlider) e.getSource();
111
thresholdValue = source.getValue();
112
update();
113
}
114
});
115
//! [on_trackbar]
116
117
pane.add(sliderPanel, BorderLayout.PAGE_START);
118
imgLabel = new JLabel(new ImageIcon(img));
119
pane.add(imgLabel, BorderLayout.CENTER);
120
}
121
122
//! [Threshold_Demo]
123
private void update() {
124
Imgproc.threshold(srcGray, dst, thresholdValue, MAX_BINARY_VALUE, thresholdType);
125
Image img = HighGui.toBufferedImage(dst);
126
imgLabel.setIcon(new ImageIcon(img));
127
frame.repaint();
128
}
129
//! [Threshold_Demo]
130
131
public static void main(String[] args) {
132
// Load the native OpenCV library
133
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
134
135
// Schedule a job for the event dispatch thread:
136
// creating and showing this application's GUI.
137
javax.swing.SwingUtilities.invokeLater(new Runnable() {
138
@Override
139
public void run() {
140
new Threshold(args);
141
}
142
});
143
}
144
}
145
146