normbindata.cc

  1 const char *help = "\
  2 progname: normbindata.cc\n\
  3 code2html: This program reads a bindata and normalizes it.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageGray.h"
  8 #include "DiskXFile.h"
  9 #include "CmdLine.h"
 10 
 11 using namespace Torch;
 12 
 13 int main(int argc, char *argv[])
 14 {
 15 	char *filename_in;
 16 	char *filename_out;
 17 	bool verbose;
 18 	
 19 	CmdLine cmd;
 20 	cmd.setBOption("write log", false);
 21 	cmd.info(help);
 22 	cmd.addText("\nArguments:");
 23 	cmd.addSCmdArg("imagefile in", &filename_in, "image file in");
 24 	cmd.addSCmdArg("output filename", &filename_out, "output bindata filename");
 25 	cmd.addText("\nOptions:");
 26 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 27 	cmd.read(argc, argv);
 28 	
 29 	//
 30 	int n_frames;
 31 	int frame_size;
 32 	DiskXFile *pf_in;
 33 	DiskXFile *pf_out;
 34 
 35 	// Load
 36 	pf_in = new DiskXFile(filename_in, "r");
 37 	pf_in->read(&n_frames, sizeof(int), 1);
 38 	pf_in->read(&frame_size, sizeof(int), 1);
 39 
 40 	Sequence *seqimage = new Sequence(1, frame_size);
 41 	
 42 	//
 43 	pf_out = new DiskXFile(filename_out, "w");
 44 	if(pf_out == NULL)
 45 	{
 46   		error("Opening bindata file %s", filename_out);      
 47     		return 0;
 48 	}
 49 	
 50 	if(verbose)
 51 	{
 52 		print("Output file ...\n");
 53 		print("   n_inputs = %d\n", frame_size);
 54 		print("   n_patterns = %d\n", n_frames);  
 55 	}
 56 
 57 	//
 58 	pf_out->write(&n_frames, sizeof(int), 1);
 59 	pf_out->write(&frame_size, sizeof(int), 1);
 60 	
 61 	//
 62 	real *mean = NULL;
 63 	real *stdv = NULL;
 64 
 65 	mean = new real [frame_size];
 66 	stdv = new real [frame_size];
 67 			
 68 	for(int i = 0 ; i < frame_size ; i++)
 69 	{
 70 		mean[i] = 0;
 71 		stdv[i] = 0;
 72 	}
 73 
 74 	for(int p = 0 ; p < n_frames ; p++)
 75 	{
 76 	   	pf_in->read(seqimage->frames[0], sizeof(real), frame_size);
 77 		
 78 		for(int i = 0 ; i < frame_size ; i++)
 79 		{
 80 			real z = seqimage->frames[0][i];
 81 			mean[i] += z;
 82 			stdv[i] += z*z;
 83 		}
 84 	}
 85 
 86 	for(int i = 0 ; i < frame_size ; i++)
 87 	{
 88 		mean[i] /= (real) n_frames;
 89 		stdv[i] /= (real) n_frames;
 90 		stdv[i] -= mean[i]*mean[i];
 91 		if(stdv[i] <= 0)
 92 		{
 93 			warning("MeanVarNorm: input column %d has a null stdv. Replaced by 1.", i);
 94 			stdv[i] = 1.;
 95 		}
 96 		else stdv[i] = sqrt(stdv[i]);
 97 	}
 98 
 99 	delete pf_in;
100 
101 	pf_in = new DiskXFile(filename_in, "r");
102 	pf_in->read(&n_frames, sizeof(int), 1);
103 	pf_in->read(&frame_size, sizeof(int), 1);
104 		
105 	for(int p = 0 ; p < n_frames ; p++)
106 	{
107 	   	pf_in->read(seqimage->frames[0], sizeof(real), frame_size);
108 
109 		for(int i = 0 ; i < frame_size ; i++)
110 		{
111 		   	real src_ = seqimage->frames[0][i];
112 			src_ = (src_ - mean[i]) / stdv[i];
113 
114 			pf_out->write(&src_, sizeof(real), 1);   
115 		}
116 	}
117 		
118 	delete [] mean;
119 	delete [] stdv;
120 	delete pf_in;
121 	delete pf_out;
122 	delete seqimage;
123 
124 	return 0;
125 }