-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentClass.java
More file actions
307 lines (250 loc) · 10.4 KB
/
segmentClass.java
File metadata and controls
307 lines (250 loc) · 10.4 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
package imagesegmentation;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javafx.scene.layout.Border;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class segmentClass extends JPanel{
static Image image;
static ImageIcon image2;
static JLabel outputImage;
public Mat source ;
static int SEG_MIN = 0;
static int SEG_MAX = 255;
static int SEG_INIT = 127;
static JPanel panel1 = new JPanel();
static JPanel panel2 = new JPanel();
static JLabel label2 = new JLabel( );
static JLabel label3 = new JLabel( );
static JLabel label4 = new JLabel( );
static JLabel label5 = new JLabel( );
static JLabel label6 = new JLabel( );
public static void main(String[] args) {
try{
//frame
JFrame frame = new JFrame("Image Segmantation");
frame.setSize(1500,650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout());
GridBagConstraints co = new GridBagConstraints();
co.weightx = 0.5;
co.fill = GridBagConstraints.HORIZONTAL;
co.gridx = 0;
co.gridy = 0;
frame.add(panel1,co);
co.gridx = 0;
co.gridy = 1;
frame.add(panel2,co);
javax.swing.border.Border blackline = BorderFactory.createLineBorder(Color.black);
TitledBorder title = BorderFactory.createTitledBorder( blackline, "Original Image");
title.setTitleJustification(TitledBorder.CENTER);
panel1.setBorder(title);
TitledBorder title2 = BorderFactory.createTitledBorder( blackline, "Segmented Images");
title.setTitleJustification(TitledBorder.CENTER);
panel2.setBorder(title2);
//postavljanje slika u panele; "images.jpg" je hardcodirana input slika na kojoj se vrši segmentacija
ImageIcon image1 = new ImageIcon("images.jpg");
JLabel label1 = new JLabel(image1);
panel1.add(label1);
//postavljanje layout-a, naslova i slika u panel2 - tu ce se prikazivati sehmentirane slike
panel2.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
JLabel labela = new JLabel("Binary Thresh");
c.gridx = 0;
c.gridy = 0;
panel2.add(labela,c);
JLabel labelb = new JLabel("Binary Inverted");
c.gridx = 1;
c.gridy = 0;
panel2.add(labelb,c);
JLabel labelc = new JLabel("Thresh to Zero");
c.gridx = 2;
c.gridy = 0;
panel2.add(labelc,c);
JLabel labeld = new JLabel("Thresh to Zero Inv");
c.gridx = 3;
c.gridy = 0;
panel2.add(labeld,c);
JLabel labele = new JLabel("Truncate Thresh");
c.gridx = 4;
c.gridy = 0;
panel2.add(labele,c);
c.gridx = 0;
c.gridy = 1;
panel2.add(label2,c);
c.gridx = 1;
c.gridy = 1;
panel2.add(label3,c);
c.gridx = 2;
c.gridy = 1;
panel2.add(label4,c);
c.gridx = 3;
c.gridy = 1;
panel2.add(label5,c);
c.gridx = 4;
c.gridy = 1;
panel2.add(label6,c);
//pozivanje metode za segmentaciju slike - ovjde je pozivamo sa inicijalnom vrijednosti segmentacije (127,255)
Mat source = SetSource();
SegmentImageBinary(SEG_INIT,source);
Mat sourceB = SetSource();
SegmentImageBinaryInv(SEG_INIT,sourceB);
Mat source0 = SetSource();
SegmentImageZero(SEG_INIT,source0);
Mat source1 = SetSource();
SegmentImageZeroInverted(SEG_INIT,source1);
Mat source2 = SetSource();
SegmentImageTrunc(SEG_INIT,source2);
//implementacija slidera
final JSlider Segmentation = new JSlider(JSlider.HORIZONTAL,
SEG_MIN, SEG_MAX, SEG_INIT);
Segmentation.setMajorTickSpacing(20);
Segmentation.setMinorTickSpacing(5);
Segmentation.setPaintTicks(true);
Segmentation.setPaintLabels(true);
c.ipady = 20;
c.gridwidth = 5;
c.gridx = 0;
c.gridy = 2;
panel2.add(Segmentation,c);
TitledBorder title3 = BorderFactory.createTitledBorder( blackline, "Move the slider to change thresholding value");
title.setTitleJustification(TitledBorder.CENTER);
Segmentation.setBorder(title3);
frame.setVisible(true);
Segmentation.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
//ovdje pozivamo metodu za segmentiranje slike sa vrijednosti koja je trenutno oznacena na slideru
Mat source = SetSource();
SegmentImageBinary(Segmentation.getValue(),source);
Mat sourceB = SetSource();
SegmentImageBinaryInv(Segmentation.getValue(),sourceB);
Mat source0 = SetSource();
SegmentImageZero(Segmentation.getValue(),source0);
Mat source1 = SetSource();
SegmentImageZeroInverted(Segmentation.getValue(),source1);
Mat source2 = SetSource();
SegmentImageTrunc(Segmentation.getValue(),source2);
}
});
}catch (Exception e) {
System.out.println("error: " + e.getMessage());
}
}
public static Mat SetSource() {
try {
image = ImageIO.read(new File("images.jpg"));
}
catch (IOException e) {
System.out.println("error: " + e.getMessage());
}
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat source = Highgui.imread("images.jpg",
Highgui.CV_LOAD_IMAGE_COLOR);
return source;
}
//metoda za segmentaciju slike
public static void SegmentImageBinary(int seg, Mat source) {
//thresh binary funkcija
Mat destination = source;
Imgproc.threshold(source,destination,seg,255,Imgproc.THRESH_BINARY);
//pretvaranje buffered image u sliku za prikaz
BufferedImage image5=matToBufferedImage(destination);
ImageIcon imageIcon1 = new ImageIcon(image5);
label2.setIcon(imageIcon1);
}
public static void SegmentImageBinaryInv(int seg, Mat source) {
//thresh binary inverted funkcija
Mat destination = source;
Imgproc.threshold(source,destination,seg,255,Imgproc.THRESH_BINARY_INV);
//pretvaranje buffered image u sliku za prikaz
BufferedImage image6=matToBufferedImage(destination);
ImageIcon imageIcon2 = new ImageIcon(image6);
label3.setIcon(imageIcon2);
}
public static void SegmentImageZero(int seg, Mat source) {
//thresh to zefo funkcija
Mat destination = source;
Imgproc.threshold(source,destination,seg,255,Imgproc.THRESH_TOZERO);
//pretvaranje buffered image u sliku za prikaz
BufferedImage image4=matToBufferedImage(destination);
ImageIcon imageIcon = new ImageIcon(image4);
label4.setIcon(imageIcon);
}
public static void SegmentImageZeroInverted(int seg, Mat source) {
//thresh to zero inverted funkcija
Mat destination = source;
Imgproc.threshold(source,destination,seg,255,Imgproc.THRESH_TOZERO_INV);
//pretvaranje buffered image u sliku za prikaz
BufferedImage image7=matToBufferedImage(destination);
ImageIcon imageIcon3 = new ImageIcon(image7);
label5.setIcon(imageIcon3);
}
public static void SegmentImageTrunc(int seg, Mat source) {
//thresh truncate funkcija
Mat destination = source;
Imgproc.threshold(source,destination,seg,255,Imgproc.THRESH_TRUNC);
//pretvaranje buffered image u sliku za prikaz
BufferedImage image8=matToBufferedImage(destination);
ImageIcon imageIcon4 = new ImageIcon(image8);
label6.setIcon(imageIcon4);
}
//pretvaranje slike iz buffered image formata kojeg vraca algoritam za segmentaciju u icon image koji se prikazuje na sucelju
public static BufferedImage matToBufferedImage(Mat bgr) {
int width = bgr.width();
int height = bgr.height();
BufferedImage image;
WritableRaster raster;
if (bgr.channels()==1) {
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
raster = image.getRaster();
byte[] px = new byte[1];
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
bgr.get(y,x,px);
raster.setSample(x, y, 0, px[0]);
}
}
}
else {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
raster = image.getRaster();
byte[] px = new byte[3];
int[] rgb = new int[3];
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
bgr.get(y,x,px);
rgb[0] = px[2];
rgb[1] = px[1];
rgb[2] = px[0];
raster.setPixel(x,y,rgb);
}
}
}
return image;
}
}