binfacenormalize.cc
1 const char *help = "\
2 progname: binfacenormalize.cc\n\
3 code2html: This program reads faces in a bindata file and normalize them.\n\
4 version: Torch3 vision2.0, 2004-2005\n\
5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
6
7 #include "ipMirror.h"
8 #include "ipNormBestFitLinear.h"
9 #include "ipHistoEqual.h"
10 #include "ipSmoothGaussian3.h"
11 #include "ipSobel.h"
12 #include "DiskXFile.h"
13 #include "CmdLine.h"
14
15 using namespace Torch;
16
17 int main(int argc, char **argv)
18 {
19 char *filename;
20 char *filename_out;
21 int width, height;
22 float sigma;
23 bool mirroradd;
24 bool nolinear;
25 bool noenhance;
26 bool nosmooth;
27 bool dosobel;
28 real sobel_threshold;
29 bool quantize;
30 int qmax;
31 bool verbose;
32 bool norm;
33 bool unnorm;
34
35 // Construct the command line
36 CmdLine cmd;
37 cmd.setBOption("write log", false);
38
39 // Put the help line at the beginning
40 cmd.info(help);
41
42 cmd.addText("\nArguments:");
43 cmd.addSCmdArg("bindata image filename", &filename, "bindata image filename");
44 cmd.addICmdArg("width", &width, "width");
45 cmd.addICmdArg("height", &height, "height");
46 cmd.addText("\nOptions:");
47 cmd.addSCmdOption("-o", &filename_out, "process.bindata", "bindata output file");
48 cmd.addRCmdOption("-sigma", &sigma, 0.25, "Gaussian kernel");
49 cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
50 cmd.addBCmdOption("-mirroradd", &mirroradd, false, "mirroradd");
51 cmd.addBCmdOption("-nolinear", &nolinear, false, "no linear light correction");
52 cmd.addBCmdOption("-noenhance", &noenhance, false, "no enhancement by histogram normalization");
53 cmd.addBCmdOption("-nosmooth", &nosmooth, false, "no smoothing");
54 cmd.addBCmdOption("-dosobel", &dosobel, false, "apply sobel edge detection");
55 cmd.addRCmdOption("-sthreshold", &sobel_threshold, 120, "threshold for Sobel");
56 cmd.addBCmdOption("-quantize", &quantize, false, "quantize");
57 cmd.addICmdOption("-qmax", &qmax, 64, "qmax levels");
58 cmd.addBCmdOption("-norm", &norm, false, "normalize the output");
59 cmd.addBCmdOption("-unnorm", &unnorm, false, "unnormalize the input");
60
61 cmd.read(argc, argv);
62
63 if(verbose)
64 {
65 print("Image info:\n");
66 print(" width = %d\n", width);
67 print(" height = %d\n", height);
68 }
69
70
71 int n_patterns;
72 int n_inputs;
73
74 //
75 //
76 DiskXFile *file = new DiskXFile(filename, "r");
77
78 file->read(&n_patterns, sizeof(int), 1);
79 file->read(&n_inputs, sizeof(int), 1);
80
81 if(verbose)
82 {
83 print("Reading bindata file (%s)\n", filename);
84 print(" n_inputs = %d\n", n_inputs);
85 print(" n_patterns = %d\n", n_patterns);
86 }
87
88 if(n_inputs != width * height)
89 {
90 delete file;
91
92 error("Incorrect size provided %d <> %dx%d", n_inputs, width, height);
93 }
94
95 int P = n_patterns;
96
97 //
98 //
99 Sequence *input = new Sequence(1, n_inputs);
100
101 //
102 //
103 DiskXFile *file_out = new DiskXFile(filename_out, "w");
104
105 if(mirroradd) n_patterns *= 2;
106 file_out->write(&n_patterns, sizeof(int), 1);
107 file_out->write(&n_inputs, sizeof(int), 1);
108
109 //
110 //
111 ipCore *linear = NULL;
112 ipCore *enhancing = NULL;
113 ipCore *smoothing = NULL;
114 ipCore *mirroring = NULL;
115 ipCore *sobel = NULL;
116
117 if(!nolinear)
118 {
119 linear = new ipNormBestFitLinear(width, height, "gray");
120 linear->setBOption("verbose", verbose);
121 }
122
123 if(!noenhance)
124 {
125 enhancing = new ipHistoEqual(width, height, "gray");
126 enhancing->setBOption("verbose", verbose);
127 }
128
129 if(!nosmooth)
130 {
131 smoothing = new ipSmoothGaussian3(width, height, "gray", sigma);
132 smoothing->setBOption("verbose", verbose);
133 }
134
135 if(mirroradd)
136 {
137 mirroring = new ipMirror(width, height, "gray");
138 mirroring->setBOption("verbose", verbose);
139 }
140
141 if(dosobel)
142 {
143 sobel = new ipSobel(width, height, "gray");
144 sobel->setBOption("verbose", verbose);
145 sobel->setROption("threshold", sobel_threshold);
146 }
147
148 Sequence *output = new Sequence(1, n_inputs);
149
150 for(int p = 0 ; p < P ; p++)
151 {
152 //
153 file->read(input->frames[0], sizeof(real), n_inputs);
154
155 if(unnorm)
156 for(int i = 0 ; i < n_inputs ; i++) input->frames[0][i] *= 255.0;
157
158 //
159 if(quantize)
160 for(int i = 0 ; i < n_inputs ; i++)
161 {
162 int quantized = (int) (qmax * input->frames[0][i]) / 255;
163 input->frames[0][i] = (quantized*255)/qmax;
164 }
165
166 output->copy(input);
167
168 //
169 if(linear != NULL)
170 {
171 linear->process(output);
172 output->copyFrom(linear->seq_out->frames[0]);
173 }
174
175 //
176 if(enhancing != NULL)
177 {
178 enhancing->process(output);
179 output->copyFrom(enhancing->seq_out->frames[0]);
180 }
181
182 //
183 if(smoothing != NULL)
184 {
185 smoothing->process(output);
186 output->copyFrom(smoothing->seq_out->frames[0]);
187 }
188
189 if(sobel != NULL)
190 {
191 sobel->process(output);
192 output->copyFrom(sobel->seq_out->frames[2]);
193 }
194
195 //
196 if(norm)
197 for(int i = 0 ; i < n_inputs ; i++) output->frames[0][i] /= 255.0;
198
199 file_out->write(output->frames[0], sizeof(real), n_inputs);
200
201 if(mirroradd)
202 {
203 mirroring->process(output);
204
205 output->copyFrom(mirroring->seq_out->frames[0]);
206
207 file_out->write(output->frames[0], sizeof(real), n_inputs);
208 }
209 }
210
211 delete input;
212 delete output;
213 delete file_out;
214 delete file;
215 if(linear != NULL) delete linear;
216 if(enhancing != NULL) delete enhancing;
217 if(smoothing != NULL) delete smoothing;
218 if(mirroring != NULL) delete mirroring;
219 if(dosobel) delete sobel;
220
221 return 0;
222 }