-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExampleIO.java
More file actions
executable file
·240 lines (203 loc) · 6.87 KB
/
ExampleIO.java
File metadata and controls
executable file
·240 lines (203 loc) · 6.87 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bric.Image;
//IMPORTS
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import bric.Cluster.KMeans;
import bric.Cluster.DBSCAN;
import bric.Cluster.SpectralClustering;
import bric.Cluster.Cluster;
import java.io.FileWriter;
/**
*
* @author BrianCarlsen
*/
public class ExampleIO implements Runnable{
private String name;
private String filePath;
private String op;
private int[][] image;
public ExampleIO (int[][] i, String rw, String fp, String n) {
rw = rw.toLowerCase();
image = i;
filePath = fp;
name = n;
if (rw.equals("read") || rw.equals("r") )
op = "r";
else if (rw.equals("write") || rw.equals("w") )
op = "w";
else
op = "other";
}
public void setImage(int[][] i) {
image = i;
}
public void setFilePath(String fp) {
filePath = fp;
}
public void setName(String n) {
name = n;
}
public int[][] getImage() {
return image;
}
public String getFilePath() {
return filePath;
}
public String getName() {
return name;
}
public void writeImageToFile(String filePath) throws IOException{
try {
File f = File.createTempFile(name + "_EX_", "", new File(filePath));
PrintWriter out = new PrintWriter(f);
int i = 0;
int j = 0;
for (int [] r : image) {
for (int c : r) {
out.println(i + ", " + j + ", " + c);
++j;
}
++i;
}
out.close();
}
catch (IOException e) {
throw e;
}
}
public int[][] readImageFromFile (File f) {
HashMap<Integer[], Integer> t = new HashMap<Integer[], Integer>();
int width = 0;
int height = 0;
try {
Scanner in = new Scanner(f);
String line;
while ( (line = in.nextLine()) != null ) {
String[] tokens = line.split(", ");
Integer[] p = new Integer[2];
p[0] = Integer.parseInt(tokens[0]);
p[1] = Integer.parseInt(tokens[1]);
Integer v = new Integer(Integer.parseInt(tokens[2]));
if (p[0] > height)
height = ++p[0];
if (p[1] > width)
width = ++p[1];
t.put(p, v);
}
}
catch (FileNotFoundException e) {
System.out.println(e.toString());
}
int[][] im = new int[height][width];
for (Integer[] p : t.keySet()) {
im[p[0]][p[1]] = t.get(p);
}
return im;
}
public void readImageFromFile(String fp) {
File f = new File(filePath + name);
readImageFromFile(f);
}
public void makeExample() throws IOException{
int cornerT = 10000;
int edgeT = 100;
ImageLayer il = new ImageLayer(image, image[0].length, image.length, ColorType.RED);
Filter sobelVertical = new Filter(new int[][]{{-1, 1},
{-2, 2},
{-1, 1}},
2, 3);
Filter sobelHorizontal = new Filter(new int[][]{{-1, -2, -1},
{1, 2, 1}},
3, 2);
int numCorners = FeatureDetection.detectCorners
(il, sobelHorizontal, sobelVertical, cornerT).size();
int numEdges = FeatureDetection.detectEdges(il, sobelVertical, edgeT).size();
numEdges += FeatureDetection.detectEdges(il, sobelHorizontal, edgeT).size();
Cluster[] kMeansCluster;
Cluster[] spectralCluster;
Cluster[] dbCluster;
ArrayList<double[]> activePixels = getActivePixels(100);
double[][] activePixelsArray = new double[activePixels.size()][2];
activePixels.toArray(activePixelsArray);
KMeans km = new KMeans(activePixels, 0);
km.smartCluster(100, 1, 10);
kMeansCluster = km.getClusters();
/*
SpectralClustering sc = new SpectralClustering(activePixelsArray);
sc.cluster(km.getNumberOfClusters());
spectralCluster = sc.getClusters();
*/
DBSCAN db = new DBSCAN(activePixelsArray, 1.5, 9);
db.cluster();
dbCluster = db.getClusters();
try {
//File f = File.createTempFile(name + "_EX_", "", new File(filePath));
File f = new File(filePath);
FileWriter out = new FileWriter(f, true);
String s = numCorners + ", ";
s += numEdges + ", ";
s += kMeansCluster.length + ", ";
//out.println(spectralCluster.length);
s += dbCluster.length;
out.write(s);
out.write("\r\n");
out.close();
}
catch (IOException e) {
throw e;
}
}
private ArrayList<double[]> getActivePixels(double thresh) {
ArrayList<double[]> active = new ArrayList<double[]>();
int i = 0;
for (int[] r : image) {
int j = 0;
for (int c : r) {
if (c >= thresh) {
double[] t = {(double) i, (double) j};
active.add(t);
}
++j;
}
++i;
}
return active;
}
@Override
public void run() {
try {
if (op.equals("r"))
readImageFromFile(filePath);
else if (op.equals("w"))
writeImageToFile(filePath);
else {
makeExample();}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
int[][] im = new int[10][10];
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
im[i][j] = (int)(Math.random() * 255);
for (int i = 0; i < 10; ++i) {
im[2][i] = 255;
im[i][4] = 255;
}
String fp = System.getProperty("user.dir");
ExampleIO eio = new ExampleIO(im, "e", fp + "\\webotsTestFile.txt", "");
Thread t = new Thread(eio);
t.start();
}
}