forked from KatrinaArnold/SigRecNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSig.cpp
More file actions
440 lines (379 loc) · 8.94 KB
/
Sig.cpp
File metadata and controls
440 lines (379 loc) · 8.94 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
#include "Sig.h"
using namespace std;
Sig::Sig()
{
normalized = false;
projected = false;
}
Sig::Sig(string path)
{
load(path);
}
/* Default Input Depth */
int Sig::getInDepth()
{
return 8;
}
/* Default Input Width */
int Sig::getInWidth()
{
return 512;
}
/* Default Input Height */
int Sig::getInHeight()
{
return 128;
}
/* Default Normalization factor
* width and height get divided by this value to create the normalized image
*/
int Sig::getFactor()
{
return 4;
}
/* load signature from a BMP file */
bool Sig::load(string path)
{
/* reset flags */
normalized = false;
projected = false;
/* Read signature from file */
if (!bsig.ReadFromFile(path.c_str())) {
cout << "Error: Unable to read file" << endl;
exit(0);
}
/*
* Check if the signature is in the expected format
*/
/* Check Depth */
if (bsig.TellBitDepth() != getInDepth()) {
cerr << "Error in " << path << ": BMP depth must be "
<< getInDepth() << "." << endl;
exit(1);
}
/* Check Width */
if (bsig.TellWidth() != getInWidth()) {
cerr << "Error in " << path << ": BMP width must be "
<< getInWidth() << "." << endl;
exit(1);
}
/* Check Height */
if (bsig.TellHeight() != getInHeight()) {
cerr << "Error in " << path << ": BMP height must be "
<< getInHeight() << "." << endl;
exit(1);
}
return true;
}
/* check if signature is already normalized */
bool Sig::isnormalized()
{
return normalized;
}
/*
* Normalize the signature
* Aligns it to the upper left corner and streches it without changing
* the aspect ratio.
*/
bool Sig::normalize()
{
BMP tempsig, auxsig;
RGBApixel pixel;
int nw, nh, startx, starty, endx, endy, tw, th;
bool flag = false;
if (normalized)
return true; // image is already normalized
nw = bsig.TellWidth();
nh = bsig.TellHeight();
/*
* Align Signature to the Left
*/
/* search for the first non-empty collumn (left to right) */
for (int xx = 0; xx < nw; xx++) {
for (int yy = 0; yy < nh; yy++) {
pixel = bsig.GetPixel(xx, yy);
if (pixelIsBlack(pixel)) {
startx = xx;
flag = true;
break;
}
}
if (flag)
break;
}
/* copy to a temporary BMP with new starting x */
tempsig.SetBitDepth(8);
tempsig.SetSize(nw, nh);
RangedPixelToPixelCopy(bsig, startx, nw-1, 0, nh-1, tempsig, 0, 0);
flag = false; // reset flag
/*
* Align Signature up
*/
/* search for the first non-empty line (top to bottom) */
for (int yy = 0; yy < nh; yy++) {
for (int xx = 0; xx < nw; xx++) {
pixel = bsig.GetPixel(xx, yy);
if (pixelIsBlack(pixel)) {
starty = yy;
flag = true;
break;
}
}
if (flag)
break;
}
/* copy back with new values (both x and y) */
bsig.SetSize(nw, nh); // blank original first - using SetSize to do it
RangedPixelToPixelCopy(tempsig, 0, nw-1, starty, nh-1, bsig, 0, 0);
// signature now starts at the top left corner
/*
* Scale the image
*/
/* search for the last non-empty line (top to bottom) */
for (int yy = 0; yy < nh; yy++) {
for (int xx = 0; xx < nw; xx++) {
pixel = bsig.GetPixel(xx, yy);
if (pixelIsBlack(pixel)) {
endy = yy;
}
}
}
/* search for the last non-empty column (left to right) */
for (int xx = 0; xx < nw; xx++) {
for (int yy = 0; yy < nh; yy++) {
pixel = bsig.GetPixel(xx, yy);
if (pixelIsBlack(pixel)) {
endx = xx;
}
}
}
/* copy to a temporary and auxilary BMP with new sizes */
tempsig.SetSize(endx+1, endy+1);
RangedPixelToPixelCopy(bsig, 0, endx+1, 0, endy+1, tempsig, 0, 0);
auxsig.SetBitDepth(8);
auxsig.SetSize(endx+1, endy+1);
RangedPixelToPixelCopy(bsig, 0, endx+1, 0, endy+1, auxsig, 0, 0);
// tempsig contains the minimum ammount of white pixels
// i.e. the signature occuppies the entire space of the image
// current image size is end+1 * endy+1
/* use auxsig to determine scaling factor */
tw = getInWidth() / getFactor();
th = getInHeight() / getFactor();
if ((nw - auxsig.TellWidth()) < (nh - auxsig.TellHeight())) {
do {
Rescale(auxsig, 'W', tw);
tw--;
} while (auxsig.TellHeight() > th);
Rescale(tempsig, 'W', tw);
}
else {
do {
Rescale(auxsig, 'H', th);
th--;
} while (auxsig.TellWidth() > tw);
Rescale(tempsig, 'H', th);
}
nw = getInWidth() / getFactor();
nh = getInHeight() / getFactor();
// copy back the image
bsig.SetSize(nw, nh); // blank original first - using SetSize to do it
RangedPixelToPixelCopy(tempsig, 0, nw-1, 0, nh-1, bsig, 0, 0);
normalized = true; // set the normalized flag
return true;
}
/*
* Save the signature in normalized form
* Normalize it if necessary.
* The resulting file will be in path + .bmp so if path = "nn/x" the file will
* be saved as nn/x.bmp
*/
bool Sig::saveNormalized(string path)
{
string fPath = path + ".bmp";
normalize();
bsig.WriteToFile(fPath.c_str());
return true;
}
/* check if signature projections have been extracted */
bool Sig::isprojected()
{
return projected;
}
/*
* Extracts Projections at 0º, 90º, 1280º and 270º
* normalize if necessary
*/
bool Sig::projections()
{
RGBApixel pixel, BLACK;
int nw, nh, xx, yy;
if(projected)
return true;
normalize();
BLACK.Alpha = BLACK.Red = BLACK.Blue = BLACK.Green = 0;
nw = bsig.TellWidth();
nh = bsig.TellHeight();
/*
* Extract projection at 0º
*/
proj[0].SetBitDepth(8);
proj[0].SetSize(nw, nh);
/* get the first Y for every X */
for (xx = 0; xx < nw; xx++) {
for (yy = 0; yy < nh; yy++) {
pixel = bsig.GetPixel(xx, yy);
if (!pixelIsWhite(pixel)) {
proj[0].SetPixel(xx, yy, BLACK);
break; // go to next xx
}
}
}
/*
* Extract projection at 90º
*/
proj[1].SetBitDepth(8);
proj[1].SetSize(nw, nh);
/* get the first X for every Y */
for (yy = 0; yy < nh; yy++) {
for (xx = 0; xx < nw; xx++) {
pixel = bsig.GetPixel(xx, yy);
if (!pixelIsWhite(pixel)) {
proj[1].SetPixel(xx, yy, BLACK);
break; // go to next yy
}
}
}
/*
* Extract projection at 180º
*/
proj[2].SetBitDepth(8);
proj[2].SetSize(nw, nh);
/* get the last Y for every X */
for (xx = 0; xx < nw; xx++) {
for (yy = nh-1; yy >= 0; yy--) {
pixel = bsig.GetPixel(xx, yy);
if (!pixelIsWhite(pixel)) {
proj[2].SetPixel(xx, yy, BLACK);
break; // go to next xx
}
}
}
/*
* Extract projection at 270º
*/
proj[3].SetBitDepth(8);
proj[3].SetSize(nw, nh);
/* get the last X for every Y */
for (yy = 0; yy < nh; yy++) {
for (xx = nw-1; xx >= 0; xx--) {
pixel = bsig.GetPixel(xx, yy);
if (!pixelIsWhite(pixel)) {
proj[3].SetPixel(xx, yy, BLACK);
break; // go to next yy
}
}
}
return true;
}
/*
* Save signature projections
* Create them if necessary.
*/
bool Sig::saveProjections(string path)
{
string fPath;
ostringstream s;
projections();
for (int ii = 0; ii < 4; ii++) {
fPath = path + ".p";
s.str("");
s << ii;
fPath += s.str();
fPath += ".bmp";
proj[ii].WriteToFile(fPath.c_str());
}
return true;
}
/*
* Convert the signature and it's projections to an array.
* Normalizes and projects if necessary
* Divides images into blocks 32x4
*/
double *Sig::toArray()
{
RGBApixel pixel;
int ii, nw, nh;
double *array;
int dx, dy, arrSize, pixden;
normalize();
projections();
nw = bsig.TellWidth();
nh = bsig.TellHeight();
// TODO: this needs to NOT be hardcoded
arrSize = 8*2; // blocks for the signature and for the projections
arrSize *= 2; // currently using 2 projections
arrSize += 1; // for the terminating value
TERM = (double) -1.0; // set value of the terminator
array = new double [arrSize];
ii = 0; dx = 16; dy = 16; // dx and dy divide X and Y respectively into blocks
/* Initialize array */
for (int jj = 0; jj < arrSize; jj++)
array[jj] = (double) 0.0;
/* for the signature *
pixden = 0; // pixel density in a block
for (int yy = 0; yy < nh-dy+1; yy+=dy) {
for (int xx = 0; xx < nw-dx+1; xx+=dx) {
for (int ny = 0; ny < dy; ny++) {
for (int nx = 0; nx < dx; nx++) {
pixel = bsig.GetPixel(xx+nx, yy+ny);
if (pixelIsBlack(pixel))
pixden++;
} // nx
} // ny
array[ii] = (double) pixden;
pixden = 0;
ii++;
} // xx
} // yy
/* for the projections */
pixden = 0;
for (int pp = 0; pp < 2; pp++) { // currently takes only projs 0º and 90º
for (int yy = 0; yy < nh-dy+1; yy+=dy) {
for (int xx = 0; xx < nw-dx+1; xx+=dx) {
for (int ny = 0; ny < dy; ny++) {
for (int nx = 0; nx < dx; nx++) {
pixel = proj[pp].GetPixel(xx+nx, yy+ny);
if (pixelIsBlack(pixel))
pixden++;
} // nx
} // ny
array[ii] = (double) pixden;
pixden = 0;
ii++;
} // xx
} // yy
} // pp
array[ii] = TERM;
return array;
}
/*
* Save signature projections
* Create them if necessary.
* TODO: NOT DONE .
*/
bool Sig::saveArray(string filepath)
{
int ii;
double *array;
ofstream file(filepath.c_str());
array = toArray();
/* Write */
for (ii = 0; array[ii] != TERM; ii++)
file << array[ii] << " "; // " " will have to be ignored
file << endl; // endl will also have to be ignored i.e. ignore.in(1)
file.close();
return true;
}
Sig::~Sig()
{
}