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 package com.jsaragih;
44
45 import java.io.BufferedReader;
46 import java.io.BufferedWriter;
47 import java.io.FileNotFoundException;
48 import java.io.FileReader;
49 import java.io.FileWriter;
50 import java.io.IOException;
51 import java.util.Scanner;
52
53 import org.openimaj.image.FImage;
54 import org.openimaj.image.analysis.algorithm.TemplateMatcher;
55 import org.openimaj.image.analysis.algorithm.TemplateMatcher.Mode;
56
57
58
59
60
61
62
63 public class Patch {
64 static { Tracker.init(); }
65
66
67 public int _t;
68
69
70 public double _a;
71
72
73 public double _b;
74
75
76 public FImage _W;
77
78 protected FImage im_ = new FImage(0, 0);
79 protected TemplateMatcher matcher;
80
81 FImage Grad(FImage im) {
82 FImage grad = new FImage(im.width, im.height);
83
84 for (int y = 1; y < im.height - 1; y++) {
85 for (int x = 1; x < im.width - 1; x++) {
86 float vx = im.pixels[y][x + 1] - im.pixels[y][x - 1];
87 float vy = im.pixels[y + 1][x] - im.pixels[y - 1][x];
88 grad.pixels[y][x] = vx * vx + vy * vy;
89 }
90 }
91 return grad;
92 }
93
94 final float SGN(float x) {
95 return (x < 0) ? 0 : 1;
96 }
97
98 FImage LBP(FImage im) {
99 FImage lp = new FImage(im.width, im.height);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 return lp;
123 }
124
125 void load(final String fname) throws FileNotFoundException {
126 BufferedReader br = null;
127 try {
128 br = new BufferedReader(new FileReader(fname));
129 Scanner sc = new Scanner(br);
130 read(sc, true);
131 } finally {
132 try {
133 br.close();
134 } catch (IOException e) {
135 }
136 }
137 }
138
139 void save(final String fname) throws IOException {
140 BufferedWriter bw = null;
141 try {
142 bw = new BufferedWriter(new FileWriter(fname));
143
144 write(bw);
145 } finally {
146 try {
147 if (bw != null)
148 bw.close();
149 } catch (IOException e) {
150 }
151 }
152 }
153
154 void write(BufferedWriter s) throws IOException {
155 s.write(IO.Types.PATCH.ordinal() + " " + _t + " " + _a + " " + _b + " ");
156 IO.writeImg(s, _W);
157 }
158
159 static Patch read(Scanner s, boolean readType) {
160 if (readType) {
161 int type = s.nextInt();
162 assert (type == IO.Types.PATCH.ordinal());
163 }
164
165 Patch p = new Patch();
166
167 p._t = s.nextInt();
168 p._a = s.nextDouble();
169 p._b = s.nextDouble();
170 p._W = IO.readImg(s);
171 p.matcher = new TemplateMatcher(p._W.clone(),
172 Mode.NORM_CORRELATION_COEFFICIENT);
173
174 return p;
175 }
176
177 Patch() {
178 }
179
180
181
182
183
184
185
186 public Patch(int t, double a, double b, FImage W) {
187 _t = t;
188 _a = a;
189 _b = b;
190 _W = W;
191 matcher = new TemplateMatcher(W.clone(),
192 Mode.NORM_CORRELATION_COEFFICIENT);
193 }
194
195 void response(FImage im, FImage resp) {
196 assert ((im.height >= _W.height) && (im.width >= _W.width));
197
198 int h = im.height - _W.height + 1;
199 int w = im.width - _W.width + 1;
200
201 if (resp.height != h || resp.width != w)
202 resp.internalAssign(new FImage(w, h));
203
204 FImage I;
205 if (_t == 0) {
206 I = im;
207 } else {
208 if (im_.height == im.height && im_.width == im.width) {
209 I = im_;
210 } else if (im_.height >= im.height && im_.width >= im.width) {
211 I = im_.extractROI(0, 0, im.width, im.height);
212 } else {
213 im_ = new FImage(im.width, im.height);
214 I = im_;
215 }
216
217 if (_t == 1) {
218 I = Grad(im);
219 } else if (_t == 2) {
220 I = LBP(im);
221 } else {
222 throw new RuntimeException("ERROR: Unsupported patch type!\n");
223 }
224 }
225
226 matcher.analyseImage(I);
227 FImage res = matcher.getResponseMap();
228
229 for (int y = 0; y < resp.height; y++)
230 for (int x = 0; x < resp.width; x++)
231 resp.pixels[y][x] = (float) (1.0 / (1.0 + Math
232 .exp(res.pixels[y][x] * _a + _b)));
233 }
234
235
236
237
238
239
240 public Patch copy() {
241 return new Patch(_t, _a, _b, _W);
242 }
243 }