-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAFinder.java
More file actions
589 lines (502 loc) · 16.2 KB
/
Copy pathSAFinder.java
File metadata and controls
589 lines (502 loc) · 16.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
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
package sa.finder;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Box;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class SAFinder {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SAFinder window = new SAFinder();
window.frmSafinder.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SAFinder() {
initialize();
}
private JFrame frmSafinder;
// make variables to store the before images
private BufferedImage img;
private BufferedImage previousImg;
private BufferedImage undoImg;
private JSlider slider;
private JLabel label;
private JLabel orginLabel;
private JTextField textField;
private JCheckBox cbRed;
private JCheckBox cbBlue;
private JCheckBox cbGreen;
private int[] colors = { 0, 0, 0 };
private int erodeCount = 0;
private int dilateCount = 0;
private int xCoor = 0;
private int yCoor = 0;
private boolean calBtnPress = false;
private boolean mouseOn = false;
private double sFactor = 1;
private double ppu = 1;
private void initialize() {
// Get the dimensions of the screen that this application is running
// and set the width to half of that screen and height to a 3/4th of the
// screen
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
final int width = (screenSize.width) / 2;
final int height = (screenSize.height) / 2 + (screenSize.height) / 4;
/////////////// Make the frame and set its dimension
/////////////// /////////////////////
frmSafinder = new JFrame();
frmSafinder.setTitle("SAFinder");
frmSafinder.setBounds(100, 100, width, height);
frmSafinder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSafinder.setResizable(false);
/////////////// create new components //////////////////////////////////
JToolBar toolBar = new JToolBar();
Component horizontalStrut = Box.createHorizontalStrut(80);
JButton btnOpenFile = new JButton("Open File");
JButton btnThreshold = new JButton("Threshold Img");
JButton btnDilate = new JButton("Dilate");
JButton btnErode = new JButton("Erode");
JButton btnCalculate = new JButton("Calculate Surface Area");
JButton btnCalibrate = new JButton("Calibrate");
JButton btnCommit = new JButton("Commit");
JButton btnRevert = new JButton("Revert");
label = new JLabel("");
label.setVerticalAlignment(SwingConstants.TOP);
label.setHorizontalAlignment(SwingConstants.LEFT);
orginLabel = new JLabel("");
orginLabel.setVerticalAlignment(SwingConstants.TOP);
orginLabel.setHorizontalAlignment(SwingConstants.LEFT);
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setColumns(10);
JToolBar sliderToolBar = new JToolBar();
cbRed = new JCheckBox("Red");
cbRed.setEnabled(false);
cbBlue = new JCheckBox("Blue");
cbBlue.setEnabled(false);
cbGreen = new JCheckBox("Green");
cbGreen.setEnabled(false);
slider = new JSlider();
slider.setEnabled(false);
slider.setSnapToTicks(true);
slider.setMaximum(255);
slider.setVisible(true);
///////////// add the components to the tool bars ////////////////
toolBar.add(horizontalStrut);
toolBar.add(btnOpenFile);
toolBar.add(btnThreshold);
toolBar.add(btnDilate);
toolBar.add(btnErode);
toolBar.add(btnCalibrate);
toolBar.add(btnCalculate);
toolBar.add(btnCommit);
toolBar.add(btnRevert);
sliderToolBar.add(cbRed);
sliderToolBar.add(cbGreen);
sliderToolBar.add(cbBlue);
sliderToolBar.add(slider);
//////////////// Add action to the buttons and slider //////////////
btnOpenFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
btnThreshold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threshold();
}
});
btnDilate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dilating();
}
});
btnErode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
eroding();
}
});
btnCalibrate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ppu = 1;
textField.setText("Enter pixels per unit length in the original image");
textField.setEditable(true);
}
});
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate();
}
});
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(img != null){
OpenEdit oe = new OpenEdit();
undoImg = oe.clone(img);
textField.setText("Committed");
reset();
} else {
textField.setText("Nothing to commit");
}
}
});
btnRevert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(undoImg != null){
OpenEdit oe = new OpenEdit();
img = oe.clone(undoImg);
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
textField.setText("Reverted");
reset();
} else {
textField.setText("Nothing to revert");
}
}
});
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
sliderChange();
}
});
cbRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cbRed();
}
});
cbGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cbGreen();
}
});
cbBlue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cbBlue();
}
});
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calibrate(e);
}
});
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clicked(e);
}
});
/////////////////// set the layout of the frame
/////////////////// /////////////////////////
GroupLayout groupLayout = new GroupLayout(frmSafinder.getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(toolBar,
GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup().addGap(196)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE).addGap(192))
.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(sliderToolBar,
GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup().addContainerGap()
.addComponent(orginLabel, GroupLayout.PREFERRED_SIZE, 343, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 343, GroupLayout.PREFERRED_SIZE)))
.addContainerGap()));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup().addContainerGap()
.addComponent(toolBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(sliderToolBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 546, GroupLayout.PREFERRED_SIZE)
.addComponent(orginLabel, GroupLayout.PREFERRED_SIZE, 546, GroupLayout.PREFERRED_SIZE))
.addContainerGap()));
frmSafinder.getContentPane().setLayout(groupLayout);
}
/////////////////// methods that are used in the action section
/////////////////// ///////////////////////////
private void openFile() {
OpenEdit oe = new OpenEdit();
img = oe.GetImage(undoImg);
if (img != null) {
reset();
// clear the label of text and set the slider to not work
// until threshold button is pressed
orginLabel.setText(null);
// get the scaleing factor
sFactor = getSF(img,label);
System.out.println(sFactor);
// resize the image and set both the labels to the image
img = oe.ResizeImage(img, orginLabel);
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
int h = img.getHeight();
int w = img.getWidth();
textField.setText(Integer.toString(h) + "x" + Integer.toString(w));
// keep track of the previous image for editing and undoing
previousImg = oe.clone(img);
undoImg = oe.clone(img);
} else {
textField.setText("No image selected");
}
}
// method to set the slider and check box to work if the threshold
// button is pressed. Checks to make sure img is not null
private void threshold() {
if (img != null) {
cbRed.setEnabled(true);
cbBlue.setEnabled(true);
cbGreen.setEnabled(true);
mouseOn = false;
textField.setText("Please check a color channel for thresholding");
erodeCount = 0;
dilateCount = 0;
} else {
textField.setText("Need to open a file before thresholding");
}
}
// method to set the colors arr 0 (red) to 1 if check 0 if not.
private void cbRed() {
if (cbRed.isSelected()) {
slider.setEnabled(true);
textField.setText("");
colors[0] = 1;
} else {
colors[0] = 0;
}
}
// method to set the colors arr 1 (green) to 1 if check 0 if not.
private void cbGreen() {
if (cbGreen.isSelected()) {
slider.setEnabled(true);
textField.setText("");
colors[1] = 1;
} else {
colors[1] = 0;
}
}
// method to set the colors arr 2 (blue) to 1 if check 0 if not.
private void cbBlue() {
if (cbBlue.isSelected()) {
slider.setEnabled(true);
textField.setText("");
colors[2] = 1;
} else {
colors[2] = 0;
}
}
// method to threshold the value based on the slider value
private void sliderChange() {
Manipulate manip = new Manipulate();
OpenEdit oe = new OpenEdit();
// Reset the image to the original image so that thresholding can be
// done using the
// original and not altered
img = oe.clone(previousImg);
// get the value of the slider
int thresLevel = slider.getValue();
if (colors[0] == 0 && colors[1] == 0 && colors[2] == 0) {
slider.setEnabled(false);
textField.setText("Please select a channel for thresholding");
} else {
label.setText(null);
// threshold the image and pit it into the label
manip.thresholdImage(img, thresLevel, colors);
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
// output threshold level
String outValue = Integer.toString(thresLevel);
textField.setText(outValue);
}
}
// method to for dilating of the image
private void dilating() {
Manipulate manip = new Manipulate();
// checks to make sure the image is not null
if (img != null) {
// uncheck all the buttons
uncheck();
slider.setEnabled(false);
mouseOn = false;
// dilate the image and set it to the icon
img = manip.dilate(img);
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
// a count to show how many times erosion is needed after dilating
if (erodeCount != 0) {
erodeCount--;
}
// increment the dilation count
dilateCount++;
StringBuilder sb = new StringBuilder();
// output to the text field
sb.append("erode count: ");
sb.append(erodeCount);
sb.append("; dilate count: ");
sb.append(dilateCount);
textField.setText(sb.toString());
} else {
textField.setText("Need a image before dilating");
}
}
// method for eroding of the image, comments same as dilating
private void eroding() {
Manipulate manip = new Manipulate();
if (img != null) {
uncheck();
slider.setEnabled(false);
mouseOn = false;
img = manip.erode(img);
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
if (dilateCount != 0) {
dilateCount--;
}
erodeCount++;
StringBuilder sb = new StringBuilder();
sb.append("erode count: ");
sb.append(erodeCount);
sb.append("; dilate count: ");
sb.append(dilateCount);
textField.setText(sb.toString());
} else {
textField.setText("Need a image before eroding");
}
}
// method to turn on the mouse for activating the click event
private void calculate(){
if(img != null){
mouseOn = true;
} else {
textField.setText("Need image for calculating surface area");
}
}
// method to uncheck all the checkboxes
private void uncheck() {
cbRed.setSelected(false);
cbBlue.setSelected(false);
cbGreen.setSelected(false);
colors[0] = 0;
colors[1] = 0;
colors[2] = 0;
cbRed.setEnabled(false);
cbBlue.setEnabled(false);
cbGreen.setEnabled(false);
}
// method to find the surface area using findSA based on a mouse click
private void clicked(MouseEvent e) {
if (img != null && mouseOn) {
xCoor = e.getX();
yCoor = e.getY();
if (xCoor > img.getWidth() || yCoor > img.getHeight()) {
textField.setText("coor out of range");
} else {
textField.setText("xCoor: " + String.valueOf(xCoor) + "; yCoor: " + String.valueOf(yCoor));
}
findSA(sFactor);
}
}
private void calibrate(ActionEvent e){
String input = textField.getText();
try {
ppu = Double.parseDouble(input);
textField.setText(String.valueOf(ppu));
} catch (NumberFormatException err) {
textField.setText("Not a valid number for calibrating");
}
}
// method to calculate get the number of pixel
private void findSA(double sFactor) {
System.out.println(sFactor);
if (img != null) {
OpenEdit oe = new OpenEdit();
Manipulate manip = new Manipulate();
// check to see if it is the first time clicking calculate
// if so, set the previous image to the image right before
// clicking calculate
if (calBtnPress == false) {
previousImg = oe.clone(img);
calBtnPress = true;
}
// reset the image to the current image for flooding
img = oe.clone(previousImg);
// get the number of pixels from flooding. When a image is
// passed to a method, it is similar to passing by reference
int pixCount = manip.flood(img, xCoor, yCoor);
double result = (pixCount) / ppu;
System.out.println(pixCount);
// set the icon to the image
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
// output the number of pixels
textField.setText("Surface Area: " + String.valueOf(result));
} else {
textField.setText("Need image to calculate surface area");
}
}
// method to get the scaleing factor
private double getSF(BufferedImage img, JLabel label) {
OpenEdit oe = new OpenEdit();
// get the scaling factor for an image
double wFactor = oe.getScaleFactor(img.getWidth(), label.getWidth());
double hFactor = oe.getScaleFactor(img.getHeight(), label.getHeight());
double sFactor = 1.0;
if (wFactor < hFactor) {
sFactor = wFactor;
} else {
sFactor = hFactor;
}
return(sFactor);
}
// reset the values when a new image is opened
private void reset() {
cbRed.setSelected(false);
cbGreen.setSelected(false);
cbBlue.setSelected(false);
cbRed.setEnabled(false);
cbGreen.setEnabled(false);
cbBlue.setEnabled(false);
slider.setEnabled(false);
colors[0] = 0;
colors[1] = 0;
colors[2] = 0;
erodeCount = 0;
dilateCount = 0;
xCoor = 0;
yCoor = 0;
calBtnPress = false;
mouseOn = false;
}
}