The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mp_grabSamples.cc

Go to the documentation of this file.
00001 /*
00002  *  mp_grabSamples.cc
00003  * 
00004  *  Grab samples 
00005  *
00006  *  Created by Ian Fasel on Wed May 28 2003.
00007  * 
00008  *  Copyright (c) 2003 Machine Perception Laboratory
00009  *  University of California San Diego.
00010  */
00011 
00012 #include "mpisearchMex.h"
00013 #include <iostream>   //TESTING!
00014 
00015 extern "C" {
00016 #include <math.h>
00017 #include <string.h>
00018 #include <stdlib.h>
00019 #include <sys/time.h>
00020 #include <unistd.h>
00021 #include "mex.h" 
00022 #include "matrix.h"  //FOR TESTING. TAKE OUT!
00023 }
00024 
00025 class sampleGrabber : MPISearchMex {
00026  public:
00027   sampleGrabber(const mxArray ** ci, const int &max_num_patches_, mxArray* &patch_array) :
00028     MPISearchMex(ci,max_num_patches_,patch_array) {};
00029   ~sampleGrabber() {}
00030 
00031   void grab(RImage<float> &pixels, const double* index_flags);
00032 };
00033 
00034 extern "C" {
00035 void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[]);
00036 }
00037 
00038 void mexFunction(int nlhs,mxArray*plhs[],int nrhs, const mxArray*prhs[]) 
00039 {
00040   // cout << "entered mex function" << endl;
00041   // [samples] = mp_grabSamples(ci, img, index)
00042   // check arguments
00043   if (nlhs !=1 )
00044     mexErrMsgTxt("mp_grabSamples: requires 1 outputs");
00045   if (nrhs != 3)
00046     mexErrMsgTxt("mp_grabSamples: requires 3 inputs");
00047   
00048   // get pointers into Mex data
00049   const mxArray ** ci = &(prhs[0]);
00050   double * img = mxGetPr(prhs[1]);
00051   double * index_flags = mxGetPr(prhs[2]);
00052   int rows = static_cast<int>(mxGetM(prhs[1]));
00053   int cols = static_cast<int>(mxGetN(prhs[1]));
00054 
00055   // create image and make upright (i.e., transpose)
00056   RImage<float> pixels(cols, rows);
00057   for (int c = 0; c < cols; c++)
00058     for(int r = 0; r < rows; r++)
00059       pixels.array[cols*r+c] = static_cast<float>(*(img++));
00060   
00061   //cout << "created image" << endl;
00062   int num_samples = 0;
00063   int index_size = max(mxGetM(prhs[2]),mxGetN(prhs[2]));
00064   //cout << "mp_makeindex thinks there are "<< index_size << " patches."<< endl;
00065   for(int i = 0; i < index_size; ++i)
00066     if(index_flags[i]>0) ++num_samples;
00067 
00068   //cout << "Allocating space for " << num_samples << " patches" << endl; 
00069   // Initialize the MPISearchMex object
00070   sampleGrabber *grabber = new sampleGrabber((mxIsStruct(prhs[0]) ? ci : NULL), num_samples, plhs[0]);
00071 
00072   //cout << "created grabber" << endl;
00073   // grab the samples
00074   grabber->grab(pixels,index_flags);
00075   //cout << "Grabbed all samples" << endl;
00076   delete grabber;
00077 }
00078 
00079 void sampleGrabber::grab(RImage<float> &pixels, const double* index_flags){
00080   if(!stream.allocated){
00081     //cout << "MPISearchObjectDetector::search: stream not allocated. Allocating data" << endl;
00082     stream.init(pixels.width, pixels.height, data, 1);
00083   }
00084   integrateImages(pixels, stream);
00085 
00086   double * p = patches;
00087   int numWindows = 0;
00088   int scale_index;
00089   float scale_factor;
00090   int ind, x, y;
00091   char errStr[256];
00092 
00093   MPIImagePyramid<float>::const_iterator scale = stream.mpi->begin();
00094   MPIImagePyramid<float>::const_iterator last_scale = stream.mpi->end();
00095   for( ; scale != last_scale; ++scale){
00096     // get pointers to cached values for this scale
00097     scale_index = scale.getScale(scale_factor);
00098     //cout << "Scale: " << scale_index << ", scale_factor = " << scale_factor << endl;
00099     float sf2 = scale_factor * scale_factor;
00100     CornerCache<float> **corners = stream.corners[scale_index];
00101     float *fns = stream.fns[scale_index];
00102     CornerCache<float> *nw_c = stream.nw_c[scale_index];
00103     float nw_fn = stream.nw_fn[scale_index];
00104     MPIScaledImage<float>::const_iterator window = (*scale).begin(), last_window = (*scale).end();
00105     //cout << "Index = " << ind << ", x = " << x << ", y = " << y << endl;
00106     for( ; window != last_window; ++window, ++numWindows){
00107       if(index_flags[numWindows] > 0){
00108         // cout << "Grabbing a Patch at index " << numWindows << endl;
00109           // calculate the statistics for this sub window
00110         window.getIndex(ind);
00111         window.getCoords(x,y);
00112         //cout << "Index = " << ind << ", x = " << x << ", y = " << y << endl;
00113           double mean = 0.0;
00114           double mean2 = 0.0;
00115           for(int corner = 0; corner < 4; corner++) {
00116             mean  += window.getPixel0 (nw_c[corner].scaledIndex) * stream.norm_window[corner].value;
00117             mean2 += window.getPixel1 (nw_c[corner].scaledIndex) * stream.norm_window[corner].value;
00118           }
00119           mean /= nw_fn;
00120           mean2 /= nw_fn;
00121           double standard_deviation = sqrt(mean2 - mean*mean);
00122           if(standard_deviation < 0.00001){
00123             sprintf(errStr,"mp_grabSamples: std = 0 at index %d",ind); 
00124             mexErrMsgTxt(errStr);
00125           }
00126 
00127           double top_left = static_cast<double>(window.getScalePixel(0,0,0));
00128           double top_row;
00129           double left_column;
00130           double divisor = 1.0/(sf2 * standard_deviation * data.stdAdjusts[static_cast<int>(scale_factor+0.5f)-1]);
00131           double * oldp = p;
00132           for(int c = 0; c < data.patchsize; ++c){
00133             top_row = static_cast<double>(window.getScalePixel(0,c,0));
00134             for(int r = 0; r < data.patchsize; ++r){
00135               left_column = static_cast<double>(window.getScalePixel(0,0,r));
00136               *(p++) = (static_cast<double>(window.getScalePixel(0,c,r))
00137                         - left_column - top_row + top_left - (r)*(c)*sf2*mean)
00138                 * divisor;
00139             }
00140           }
00141       }
00142     }
00143   }
00144 }
00145 
00146 /*
00147  *
00148  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00149  *
00150  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00151  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00152  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00153  *
00154  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00155  *
00156  */

Generated on Mon Nov 8 17:07:42 2004 for MPT by  doxygen 1.3.9.1