Fake signature.cpp
Jump to navigation
Jump to search
- two-step g++ compilation:
g++ -Wall -c fake_signature.cpp
g++ fake_signature.o -lgsl -o new_create_sigs
#include <string> #include <vector> #include <iostream> #include <iomanip> #include <sstream> #include <fstream> // These two includes are POSIX environment-specific // They are used to manipulate directories #include <sys/types.h> #include <sys/stat.h> #include <errno.h> // This code uses the GNU Scientific Library // To get the latest version, download and unzip the tarball, ./configure, make, sudo make install #include <gsl/gsl_rng.h> #include <gsl/gsl_randist.h> #include <gsl/gsl_statistics_double.h> using namespace std; enum WNDCHARM_ERROR { WC_NO_ERROR, WC_SIG_ARY_NOT_INITIALIZED, WC_NOT_LONG_ENOUGH, WC_CANT_WRITE_SIGS_TO_FILE, WC_CANT_WRITE_DUMMY_IMG_FILE }; class SignatureSet { public: SignatureSet(); SignatureSet(string imgname, int num_sigs); WNDCHARM_ERROR AddSignature( string sig_name, double sig_val ); WNDCHARM_ERROR InitializeSigArray ( int new_length ); void PrintSigs(); WNDCHARM_ERROR WriteSigsToFile(); WNDCHARM_ERROR CreateDummyImageFile(); string name; int class_id; private: vector<string> my_signature_names; vector<double> my_signature_vals; int size; int current_index; }; SignatureSet::SignatureSet () { size = 0; current_index = -1; } SignatureSet::SignatureSet( string imgname, int num_sigs ) { name = imgname; InitializeSigArray( num_sigs ); } WNDCHARM_ERROR SignatureSet::InitializeSigArray( int new_length ) { my_signature_names.resize( new_length ); my_signature_vals.resize( new_length ); size = new_length; current_index = 0; return WC_NO_ERROR; } WNDCHARM_ERROR SignatureSet::AddSignature( string sig_name, double sig_val ) { if( current_index == -1) { return WC_SIG_ARY_NOT_INITIALIZED; } if( current_index >= size ) { return WC_NOT_LONG_ENOUGH; } my_signature_names[ current_index ] = sig_name; my_signature_vals[ current_index ] = sig_val; current_index++; return WC_NO_ERROR; } void SignatureSet::PrintSigs() { cout << endl << "Image " << name << endl; if( my_signature_names.size() != my_signature_vals.size() ) { cout << "ERROR: The number of signature labels and signature values don't match." << endl; return; } vector<string>::iterator names_it = my_signature_names.begin(); vector<double>::iterator vals_it = my_signature_vals.begin(); for( ; names_it !=my_signature_names.end(); ++names_it, ++vals_it ) { cout << "\t" << *names_it << "\t" << *vals_it << endl; //cout << "names : " << *names_it << endl; //cout << *vals_it << endl; } cout << endl; } WNDCHARM_ERROR SignatureSet::WriteSigsToFile() { ofstream myfile; ostringstream filename; // TODO : this is unix specific code filename << "classes/" << setw(3) << setfill('0') << class_id << "/" << name << "_0_0.sig"; myfile.open( filename.str().c_str() ); if( !myfile ) { cerr << "Couldn't open file " << filename.str() << " for writing. " << endl; return WC_CANT_WRITE_SIGS_TO_FILE; } /* Format : Index of the class this image belongs to file name of image sig val sig name ... */ myfile << class_id << endl; myfile << name << endl; vector<string>::iterator names_it = my_signature_names.begin(); vector<double>::iterator vals_it = my_signature_vals.begin(); for( ; names_it !=my_signature_names.end(); ++names_it, ++vals_it ) { myfile << *vals_it << " " << *names_it << endl; } myfile.close(); return WC_NO_ERROR; } WNDCHARM_ERROR SignatureSet::CreateDummyImageFile() { ofstream myfile; ostringstream filename; // TODO : this is unix specific code filename << "classes/"<< setw(3) << setfill('0') << class_id << "/" << name << ".tif"; myfile.open( filename.str().c_str() ); if( !myfile ) { cerr << "Couldn't open file " << filename.str() << " for writing. " << endl; return WC_CANT_WRITE_DUMMY_IMG_FILE; } myfile.close(); return WC_NO_ERROR; } int main() { const int num_classes = 15; const int num_images_per_class = 100; const int num_signatures = 50; WNDCHARM_ERROR ret_val; // sigma_add is added to the variance of the gaussian signature distribution const int sigma_add = 1; vector< vector<SignatureSet> > image_classes( num_classes, vector<SignatureSet>( num_images_per_class ) ); gsl_rng * r; r = gsl_rng_alloc (gsl_rng_mt19937); // select random number generator double sig_val; ostringstream image_name; ostringstream sig_name; ostringstream class_dir_name; int signature_index, image_index, class_index; int image_count = 0; if( -1 == mkdir( "classes", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) ) { cerr << "Cannot create top-level dir: " << strerror( errno ) << endl; return -1; } for( class_index = 0; class_index < num_classes; class_index++ ) { class_dir_name.str(""); class_dir_name << "classes/" << setw(3) << setfill('0') << class_index; if( -1 == mkdir( class_dir_name.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) ) { cerr << "cannot create directory " << class_dir_name.str() << ": " << strerror( errno ) << endl; return -2; } for( image_index = 0; image_index < num_images_per_class; image_index++ ) { image_name.str(""); image_name << "Image" << setw(3) << setfill('0') << image_count; image_count++; image_classes[class_index][image_index].name = image_name.str(); image_classes[class_index][image_index].class_id = class_index; image_classes[class_index][image_index].InitializeSigArray( num_signatures ); for( signature_index = 1; signature_index <= num_signatures; signature_index++) { sig_name.str(""); sig_name << "Signature" << signature_index; sig_val = gsl_ran_gaussian( r, ( signature_index ) + sigma_add ) + class_index * 10; ret_val = image_classes[class_index][image_index].AddSignature( sig_name.str(), sig_val ); if( WC_NO_ERROR != ret_val ) { cerr << "ERROR: Couldn't add signature. Error code: " << int( ret_val ) << ". Exiting..." << endl; return -3; } } image_classes[class_index][image_index].WriteSigsToFile(); image_classes[class_index][image_index].CreateDummyImageFile(); } } gsl_rng_free (r); //calculate fisher_scores //vector<double> fisher_scores; return 0; }