Computes a set of random projections of the gray-scale ROI.
Description:
Computes scalar product of the gray-scale patch and many random vectors (projections).
Inspired by Randomfaces approach. Due to high dimensionality of the patch and small number of projections, no orthogonalisation is done as the projections are assumed to be nearly orthogonal.
/** Author: jena Computes scalar product of the gray-scale patch and many random vectors (projections). Inspired by Randomfaces approach. Due to high dimensionality of the patch and small number of projections, no orthogonalisation is done as the projections are assumed to be nearly orthogonal. */#include <iostream>#include <stdlib.h>#include <mash/heuristic.h>usingnamespaceMash;usingnamespacestd;//------------------------------------------------------------------------------// Declaration of the heuristic class//------------------------------------------------------------------------------classRandomProjections:publicHeuristic{//_____ Construction / Destruction __________public:RandomProjections();virtual~RandomProjections();//_____ Implementation of Heuristic __________public://--------------------------------------------------------------------------/// @brief Called once at the creation of the heuristic////// Pre-computes all the data that will never change during the life-time of/// the heuristic////// When this method is called, the 'roi_extent' attribute is initialized////// @remark The implementation of this method is optional//--------------------------------------------------------------------------virtualvoidinit();//--------------------------------------------------------------------------/// @brief Called once when the heuristic is destroyed////// Frees the memory allocated by the init() method////// @remark This method must be implemented if init() is used and allocated/// some memory//--------------------------------------------------------------------------virtualvoidterminate();//--------------------------------------------------------------------------// Returns the number of features this heuristic computes//// When this method is called, the 'roi_extent' attribute is initialized//--------------------------------------------------------------------------virtualunsignedintdim();//--------------------------------------------------------------------------// Called once per image, before any computation //// Pre-computes from a full image the data the heuristic will need to compute// features at any coordinates in the image//// When this method is called, the following attributes are initialized:// - roi_extent// - image//--------------------------------------------------------------------------virtualvoidprepareForImage();//--------------------------------------------------------------------------// Called once per image, after any computation //// Frees the memory allocated by the prepareForImage() method//--------------------------------------------------------------------------virtualvoidfinishForImage();//--------------------------------------------------------------------------// Called once per coordinates, before any computation//// Pre-computes the data the heuristic will need to compute features at the// given coordinates//// When this method is called, the following attributes are initialized:// - roi_extent// - image// - coordinates//--------------------------------------------------------------------------virtualvoidprepareForCoordinates();//--------------------------------------------------------------------------// Called once per coordinates, after any computation //// Frees the memory allocated by the prepareForCoordinates() method//--------------------------------------------------------------------------virtualvoidfinishForCoordinates();//--------------------------------------------------------------------------// Computes the specified feature//// When this method is called, the following attributes are initialized:// - roi_extent// - image// - coordinates//--------------------------------------------------------------------------virtualscalar_tcomputeFeature(unsignedintfeature_index);//_____ Attributes __________protected:staticconstunsignedintNUM_FEATURES=200;float**projections;};//------------------------------------------------------------------------------// Creation function of the heuristic//------------------------------------------------------------------------------extern"C"Heuristic*new_heuristic(){returnnewRandomProjections();}/************************* CONSTRUCTION / DESTRUCTION *************************/RandomProjections::RandomProjections(){}RandomProjections::~RandomProjections(){}/************************* IMPLEMENTATION OF Heuristic ************************/voidRandomProjections::init(){unsignedintroi_size=roi_extent*2+1;unsignedintroi_size2=roi_size*roi_size;inti,j;projections=newfloat*[NUM_FEATURES];for(i=0;i<NUM_FEATURES;++i){projections[i]=newfloat[roi_size2];}srand((unsigned)time(NULL));for(i=0;i<NUM_FEATURES;++i){// generate random vectorfloat*proj=projections[i];floatsum=0;for(j=0;j<roi_size2;++j){floatr=((float)rand()/(float)RAND_MAX);*proj++=r;sum+=r;}// vector normalisationproj=projections[i];for(j=0;j<roi_size2;++j){*proj++/=sum;}}}voidRandomProjections::terminate(){for(inti=0;i<NUM_FEATURES;++i)delete[]projections[i];delete[]projections;}unsignedintRandomProjections::dim(){returnNUM_FEATURES;}voidRandomProjections::prepareForImage(){}voidRandomProjections::finishForImage(){}voidRandomProjections::prepareForCoordinates(){}voidRandomProjections::finishForCoordinates(){}scalar_tRandomProjections::computeFeature(unsignedintfeature_index){unsignedintx0=coordinates.x-roi_extent;unsignedinty0=coordinates.y-roi_extent;unsignedintroi_size=roi_extent*2+1;floatfvalue=0.0;float*proj=projections[feature_index];byte_t**pLines=image->grayLines();for(inty=0;y<roi_size;++y){byte_t*line=pLines[y0+y]+x0;for(intx=0;x<roi_size;++x){fvalue+=((float)*line)**proj;++line;++proj;}}returnfvalue;}