forked from tekrei/JavaExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountRices.java
More file actions
139 lines (126 loc) · 5.18 KB
/
Copy pathCountRices.java
File metadata and controls
139 lines (126 loc) · 5.18 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
package jdip;
import com.jhlabs.image.MinimumFilter;
import com.jhlabs.image.ReduceNoiseFilter;
import com.jhlabs.image.ThresholdFilter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
public class CountRices extends JFrame {
private JTextField txtThreshold = null;
private JLabel lblOriginal = null;
private JLabel lblAfterFiltering = null;
private JLabel lblModified = null;
private JLabel lblCount = null;
private JPanel pnlView;
private BufferedImage originalImage;
private CountRices() {
super();
initialize();
}
public static void main(String[] args) {
new CountRices();
}
private void initialize() {
this.setLayout(new BorderLayout());
this.setSize(new Dimension(800, 325));
this.getContentPane().add(getPnlView(), BorderLayout.CENTER);
lblCount = new JLabel();
lblCount.setHorizontalAlignment(SwingConstants.CENTER);
lblCount.setVerticalAlignment(SwingConstants.CENTER);
this.getContentPane().add(lblCount, BorderLayout.SOUTH);
txtThreshold = new JTextField("173");
txtThreshold.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
try {
count(Integer.parseInt(txtThreshold.getText()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
this.getContentPane().add(txtThreshold, BorderLayout.NORTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
Image tempImage = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("rice.png"));
System.out.print("Loading Image");
while (tempImage.getHeight(null) <= 0 || tempImage.getWidth(null) <= 0) {
System.out.print(".");
}
System.out.println();
lblOriginal.setIcon(new ImageIcon(tempImage));
originalImage = new BufferedImage(tempImage.getWidth(null), tempImage.getHeight(null), BufferedImage.TYPE_USHORT_GRAY);
Graphics2D g2 = originalImage.createGraphics();
g2.drawImage(tempImage, null, null);
count(173);
}
private void count(int threshold) {
BufferedImage image = new BufferedImage(originalImage.getWidth(null), originalImage.getHeight(null), BufferedImage.TYPE_USHORT_GRAY);
Graphics2D g2 = image.createGraphics();
g2.drawImage(originalImage, null, null);
image = (new MinimumFilter()).filter(image, null);
image = (new ThresholdFilter(threshold)).filter(image, null);
image = (new ReduceNoiseFilter()).filter(image, null);
BufferedImage image2 = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_USHORT_GRAY);
g2 = image2.createGraphics();
g2.drawImage(image, null, null);
lblAfterFiltering.setIcon(new ImageIcon(image2));
lblCount.setText(getRiceCount(image));
lblModified.setIcon(new ImageIcon(image));
}
private String getRiceCount(BufferedImage image) {
// Nesneleri saymak lazim
int countOfRices = 0;
for (int row = 0; row < image.getWidth(); row++) {
for (int column = 0; column < image.getHeight(); column++) {
if (checkAndConvert(image, row, column)) {
countOfRices++;
}
}
}
return Integer.toString(countOfRices);
}
private boolean checkAndConvert(BufferedImage image, int row, int column) {
if (isWhite(image, row, column)) {
image.setRGB(row, column, 0);
checkAndConvert(image, row + 1, column);
checkAndConvert(image, row - 1, column);
checkAndConvert(image, row, column + 1);
checkAndConvert(image, row, column - 1);
return true;
}
return false;
}
private boolean isWhite(BufferedImage image, int row, int column) {
try {
Color color = new Color(image.getRGB(row, column));
if (color.equals(Color.WHITE)) {
return true;
}
} catch (Exception e) {
}
return false;
}
private JPanel getPnlView() {
if (pnlView == null) {
pnlView = new JPanel();
lblOriginal = new JLabel();
lblOriginal.setHorizontalAlignment(SwingConstants.CENTER);
lblOriginal.setVerticalAlignment(SwingConstants.CENTER);
pnlView.add(lblOriginal);
lblAfterFiltering = new JLabel();
lblAfterFiltering.setHorizontalAlignment(SwingConstants.CENTER);
lblAfterFiltering.setVerticalAlignment(SwingConstants.CENTER);
pnlView.add(lblAfterFiltering);
lblModified = new JLabel();
lblModified.setHorizontalAlignment(SwingConstants.CENTER);
lblModified.setVerticalAlignment(SwingConstants.CENTER);
pnlView.add(lblModified);
}
return pnlView;
}
}