bindata2blockbindata.cc

  1 const char *help = "\
  2 progname: bindata2blockbindata.cc\n\
  3 code2html: This program reads a bindata and decomposes it into blocks.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageGray.h"
  8 #include "ipIdentity.h"
  9 #include "ipBlock.h"
 10 #include "DiskXFile.h"
 11 #include "CmdLine.h"
 12 
 13 using namespace Torch;
 14 
 15 int main(int argc, char *argv[])
 16 {
 17 	char *filename_in;
 18 	int width;
 19 	int height;
 20 	char *filename_out;
 21 	int block_size;
 22 	int block_overlap;
 23 	bool block_to_frame;
 24 	bool verbose;
 25 	bool display;
 26 	bool blocknorm;
 27 	bool unnorm;
 28 	bool norm;
 29 	
 30 	CmdLine cmd;
 31 	cmd.setBOption("write log", false);
 32 	cmd.info(help);
 33 	cmd.addText("\nArguments:");
 34 	cmd.addSCmdArg("imagefile in", &filename_in, "image file in");
 35 	cmd.addICmdArg("width", &width, "width");
 36 	cmd.addICmdArg("height", &height, "height");
 37 	cmd.addSCmdArg("output filename", &filename_out, "output bindata filename");
 38 	cmd.addText("\nBlock decomposition Options:");
 39 	cmd.addBCmdOption("-blocktoframe", &block_to_frame, false, "a block becomes a frame");
 40 	cmd.addICmdOption("-block", &block_size, 8, "block size");
 41 	cmd.addICmdOption("-overlap", &block_overlap, 4, "block overlap");
 42 	cmd.addBCmdOption("-blocknorm", &blocknorm, false, "mean/stdv normalize blocks");
 43 	cmd.addBCmdOption("-unnorm", &unnorm, false, "unnormalize input image");
 44 	cmd.addBCmdOption("-norm", &norm, false, "normalize output image");
 45 	cmd.addText("\nMisc Options:");
 46 	cmd.addBCmdOption("-display", &display, false, "display dct features");
 47 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 48 	cmd.read(argc, argv);
 49 	
 50 	//
 51 	int n_images;
 52 	int image_size;
 53 	DiskXFile *pf_in;
 54 	int n_inputs;
 55 	int n_patterns;
 56 	DiskXFile *pf_out;
 57 
 58 	// Load the image
 59 	pf_in = new DiskXFile(filename_in, "r");
 60 	pf_in->read(&n_images, sizeof(int), 1);
 61 	pf_in->read(&image_size, sizeof(int), 1);
 62 
 63 	if(image_size != width * height)
 64 		error("Incorrect image size %d != %dx%d.", image_size, width, height);
 65 
 66 	Sequence *seqimage = new Sequence(1, image_size);
 67 	
 68 	// feature image machine to apply for each block
 69 		
 70 	int n_block, n_output;
 71 	int rowblocks;
 72 	int colblocks;
 73 	
 74 	// block by block convolution
 75 	ipCore *ip_id = new ipIdentity(block_size, block_size, "gray");
 76 	ipBlock *ip = new ipBlock(width, height, "gray", ip_id, block_size*block_size, block_size, block_overlap);
 77 
 78 	n_block = ip->n_block;
 79         n_output = block_size*block_size;
 80 	rowblocks = colblocks = block_size;
 81 	ip->setBOption("verbose", verbose);
 82 
 83 	if(verbose)
 84 	{
 85 		print("-> n_block = %d\n", n_block);
 86 		print("-> n_output = %d\n", n_output);
 87 	}
 88 
 89 	//
 90 	pf_out = new DiskXFile(filename_out, "w");
 91 	if(pf_out == NULL)
 92 	{
 93   		error("Opening bindata file %s", filename_out);      
 94     		return 0;
 95 	}
 96 	
 97 	//
 98 	int frame_size;
 99 	
100 	if(block_to_frame)
101 	{
102 	  	if(verbose) print("Block to frame: each block becomes a frame.\n");
103 		
104 		n_inputs = n_output;
105 		frame_size = n_inputs;
106 
107 		n_patterns = n_images * n_block;
108 	}
109 	else
110 	{
111 		n_inputs = n_block * n_output;
112 		frame_size = n_inputs;
113 		n_patterns = n_images;
114 	}
115 
116 	if(verbose)
117 	{
118 		print("Output file ...\n");
119 		print("   n_inputs = %d\n", frame_size);
120 		print("   n_patterns = %d\n", n_patterns);  
121 	}
122 
123 	//
124 	pf_out->write(&n_patterns, sizeof(int), 1);
125 	pf_out->write(&frame_size, sizeof(int), 1);
126 	
127 	//
128 	real *blocks_mean = NULL;
129 	real *blocks_stdv = NULL;
130 
131 	real *data_out = new real [n_inputs];
132 
133 	for(int p = 0 ; p < n_images ; p++)
134 	{
135 	   	pf_in->read(seqimage->frames[0], sizeof(real), image_size);
136 		
137 		if(unnorm)
138 		{
139 			for(int i = 0 ; i < image_size ; i++)
140 			{
141 				int z_ = (int) (seqimage->frames[0][i] * 255.0);
142 
143 				seqimage->frames[0][i] = z_;
144 			}
145 		}
146 
147 		// Apply the pattern machine to each block of the image
148 		ip->process(seqimage);
149 
150 		if(norm)
151 		{
152 			for(int b = 0 ; b < n_block ; b++)
153 			{
154 				real *src_ = ip->seq_out->frames[b];
155 				
156 				for(int i = 0 ; i < n_output ; i++)
157 					src_[i] /= 255.0;
158 			}
159 		}
160 
161 		if(blocknorm)
162 		{
163 			blocks_mean = new real [n_output];
164 			blocks_stdv = new real [n_output];
165 			
166 			for(int i = 0 ; i < n_output ; i++)
167 			{
168 				blocks_mean[i] = 0;
169 				blocks_stdv[i] = 0;
170 			}
171 			
172 			for(int b = 0 ; b < n_block ; b++)
173 			{
174 				real *src_ = ip->seq_out->frames[b];
175 				
176 				for(int i = 0 ; i < n_output ; i++)
177 				{
178 					real z = src_[i];
179 					blocks_mean[i] += z;
180 					blocks_stdv[i] += z*z;
181 				}
182 			}
183 
184 			for(int i = 0 ; i < n_output ; i++)
185 			{
186 				blocks_mean[i] /= (real) n_block;
187 				blocks_stdv[i] /= (real) n_block;
188 				blocks_stdv[i] -= blocks_mean[i]*blocks_mean[i];
189 				if(blocks_stdv[i] <= 0)
190 				{
191 					warning("MeanVarNorm: input column %d has a null stdv. Replaced by 1.", i);
192 					blocks_stdv[i] = 1.;
193 				}
194 				else blocks_stdv[i] = sqrt(blocks_stdv[i]);
195 			}
196 			
197 			for(int b = 0 ; b < n_block ; b++)
198 			{
199 				real *src_ = ip->seq_out->frames[b];
200 				
201 				for(int i = 0 ; i < n_output ; i++)
202 					src_[i] = (src_[i] - blocks_mean[i]) / blocks_stdv[i];
203 			}
204 		}
205 
206 		if(block_to_frame)
207 		{
208 			for(int b = 0 ; b < n_block ; b++)
209 			{
210 			   	int x = (b % rowblocks);
211 				int y = (b / rowblocks);
212 				
213 		   		if(display == true) 
214 					print("Block %d (%dx%d): ", b, x, y);
215 			
216 				real *output_ = ip->seq_out->frames[b];
217 			
218 				for(int i = 0 ; i < n_output ; i++)
219 				{
220 		   			if(display == true) print("%g ", output_[i]);
221 
222 		   			data_out[i] = output_[i];
223 				}
224 
225 		   		if(display == true) print("\n");
226 	
227 				for(int i = 0 ; i <  n_inputs ; i++)
228 					pf_out->write(&data_out[i], sizeof(real), 1);   
229 			}
230 		}
231 		else
232 		{
233 			int j = 0;
234 		
235 			for(int b = 0 ; b < n_block ; b++)
236 			{
237 		   		if(display == true) print("Block %d: ", b);
238 			
239 				real *output_ = ip->seq_out->frames[b];
240 			
241 				for(int i = 0 ; i < n_output ; i++)
242 				{
243 		   			if(display == true) print("%g ", output_[i]);
244 
245 		   			data_out[j] = output_[i];
246 				
247 					j++;
248 				}
249 		   		if(display == true) print("\n");
250 			}
251 		
252 			for(int i = 0 ; i <  n_inputs ; i++)
253 				pf_out->write(&data_out[i], sizeof(real), 1);   
254 		}
255 	}
256 		
257 	if(blocknorm)
258 	{
259 		delete [] blocks_mean;
260 		delete [] blocks_stdv;
261 	}
262 	
263 	delete [] data_out;
264 	delete pf_out;
265 	delete pf_in;
266 	
267 
268 	// Free all
269 	delete ip_id;
270 	delete ip;
271 	delete seqimage;
272 
273 	return 0;
274 }