-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreshold.java
More file actions
280 lines (242 loc) · 12.1 KB
/
threshold.java
File metadata and controls
280 lines (242 loc) · 12.1 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task7;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
/**
*
* @author Maestro
*/
public class threshold extends javax.swing.JFrame {
/**
* Creates new form threshold
*/
BufferedImage temp_img;
public threshold() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
btn_imageChooser = new javax.swing.JButton();
lbl_image1 = new javax.swing.JLabel();
lbl_threshold = new javax.swing.JLabel();
jslice_thresholder = new javax.swing.JSlider();
txt_slider = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(750, 560));
jLabel1.setText("Choose Image");
btn_imageChooser.setText("Choose");
btn_imageChooser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_imageChooserActionPerformed(evt);
}
});
lbl_image1.setPreferredSize(new java.awt.Dimension(350, 350));
lbl_image1.setSize(new java.awt.Dimension(350, 350));
lbl_threshold.setPreferredSize(new java.awt.Dimension(350, 350));
lbl_threshold.setSize(new java.awt.Dimension(350, 350));
jslice_thresholder.setMaximum(255);
jslice_thresholder.setPaintLabels(true);
jslice_thresholder.setPaintTicks(true);
jslice_thresholder.setToolTipText("");
jslice_thresholder.setValue(127);
jslice_thresholder.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jslice_thresholderStateChanged(evt);
}
});
txt_slider.setEditable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn_imageChooser)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(433, 433, 433)
.addComponent(jslice_thresholder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txt_slider, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_image1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbl_threshold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(419, 419, 419))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(btn_imageChooser))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_threshold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lbl_image1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jslice_thresholder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btn_imageChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_imageChooserActionPerformed
String userDir = System.getProperty("user.home");
JFileChooser fileChooser = new JFileChooser(userDir + "/Desktop");
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
final BufferedImage img;
img = ImageIO.read(selectedFile);
temp_img=img;
ImageIcon imgicon= new ImageIcon(img);
lbl_image1.setIcon(imgicon);
int threshold=findThreshold(img);
txt_slider.setText(threshold+"");
jslice_thresholder.setValue(threshold);
setThreshold(threshold,img);
} catch (IOException ex) {
Logger.getLogger(threshold.class.getName()).log(Level.SEVERE, null, ex);
}
}
}//GEN-LAST:event_btn_imageChooserActionPerformed
private void jslice_thresholderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jslice_thresholderStateChanged
// TODO add your handling code here:
txt_slider.setText(jslice_thresholder.getValue()+"");
setThreshold(jslice_thresholder.getValue(), temp_img);
}//GEN-LAST:event_jslice_thresholderStateChanged
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(threshold.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(threshold.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(threshold.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(threshold.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new threshold().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btn_imageChooser;
private javax.swing.JLabel jLabel1;
private javax.swing.JSlider jslice_thresholder;
private javax.swing.JLabel lbl_image1;
private javax.swing.JLabel lbl_threshold;
private javax.swing.JTextField txt_slider;
// End of variables declaration//GEN-END:variables
private void setThreshold(int threshold,BufferedImage img) {
int width=img.getWidth();
int height=img.getHeight();
BufferedImage threshold_img=new BufferedImage(width, height, 6);
for (int j = 0; j < width; j++) {
for (int k = 0; k < height; k++) {
Color c = new Color(img.getRGB(j, k));
int red=c.getRed();
int green=c.getGreen();
int blue=c.getBlue();
if (red < threshold && green < threshold && blue < threshold) {
threshold_img.setRGB(j, k, new Color(0, 0, 0).getRGB());
}
else {
threshold_img.setRGB(j, k, new Color(255, 255, 255).getRGB());
}
}
}
lbl_threshold.setIcon(new ImageIcon(threshold_img));
}
private int findThreshold(BufferedImage img) {
int threshold;
int [] histData=new int[256];
WritableRaster raster = img.getRaster();
DataBufferByte data = (DataBufferByte)raster.getDataBuffer();
byte [] srcData=data.getData();
int ptr = 0;
while (ptr < srcData.length) {
int h = 0xFF & srcData[ptr];
histData[h]++;
ptr++;
}
int total = srcData.length;
float sum = 0;
for (int t = 0; t < 256; t++) {
sum += t * histData[t];
}
float sumB = 0;
int wB = 0;
int wF = 0;
float varMax = 0;
threshold = 0;
for (int t = 0; t < 256; t++) {
wB += histData[t];
if (wB == 0) {
continue;
}
wF = total - wB;
if (wF == 0) {
break;
}
sumB += (float) (t * histData[t]);
float mB = sumB / wB;
float mF = (sum - sumB) / wF;
float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
if (varBetween > varMax) {
varMax = varBetween;
threshold = t;
}
}
return threshold;
}
}