test-sampleH2SV2.C

Go to the documentation of this file.
00001 /*!@file BeoSub/test-sampleH2SV2.C Grab image from camra and test for H2SV1 pixel data */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Zack Gossman <gossman@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/test-sampleH2SV2.C $
00035 // $Id: test-sampleH2SV2.C 8145 2007-03-20 19:48:53Z rjpeters $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/FrameGrabberConfigurator.H"
00040 #include "GUI/XWindow.H"
00041 #include "Image/DrawOps.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Raster/Raster.H"
00045 #include "Transport/FrameIstream.H"
00046 #include "Util/Timer.H"
00047 #include "Util/Types.H"
00048 #include "Util/log.H"
00049 
00050 #include <cstdio>
00051 #include <cstdlib>
00052 #include <cstring>
00053 
00054 #define NAVG 20
00055 
00056 /*! This test grabs from a camera and then finds the average RGB and H2SV1 pixel values acros a predetermined area */
00057 int main(const int argc, const char **argv)
00058 {
00059   // instantiate a model manager:
00060   ModelManager manager("H2SV2 Tester");
00061 
00062   // Instantiate our various ModelComponents:
00063   nub::soft_ref<FrameGrabberConfigurator>
00064     gbc(new FrameGrabberConfigurator(manager));
00065   manager.addSubComponent(gbc);
00066 
00067   // Parse command-line:
00068   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00069 
00070   // do post-command-line configs:
00071   nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00072   if (gb.isInvalid())
00073     LFATAL("You need to select a frame grabber type via the "
00074            "--fg-type=XX command-line option for this program "
00075            "to be useful");
00076 
00077                 gb->setModelParamVal("FrameGrabberSubChan", 0);
00078                 gb->setModelParamVal("FrameGrabberBrightness", 128);
00079 gb->setModelParamVal("FrameGrabberHue", 180);
00080   //Load in config file for camera FIX: put in a check whether config file exists!
00081   manager.loadConfig("camconfig.pmap");
00082 
00083   // let's get all our ModelComponent instances started:
00084   manager.start();
00085 
00086   //int imgWidth = gb->getWidth();
00087  // int imgHeight = gb->getHeight();
00088 
00089   float avgR = 0.0;
00090   float avgG = 0.0;
00091   float avgB = 0.0;
00092   float avgH1 = 0.0;
00093   float avgH2 = 0.0;
00094   float avgS = 0.0;
00095   float avgV = 0.0;
00096 
00097   int count = 0;
00098 
00099         int sampleWidth = 20;
00100         int sampleHeight = 20;
00101         int centerX = gb->getWidth()/2;// gb->getWidth()/2; //sampleWidth + 1;
00102          int centerY = gb->getHeight()/2; //70; // gb->getHeight()/2; //imgHeight/2;
00103 
00104   // get ready for main loop:
00105   XWindow win(gb->peekDims(), 0, 0, "test-sample window");
00106   Timer tim; uint64 t[NAVG]; int frame = 0;
00107 
00108   // get the frame grabber to start streaming:
00109   gb->startStream();
00110 
00111   while(1) {
00112     count++;
00113     tim.reset();
00114 
00115     Image< PixRGB<byte> > ima = gb->readRGB();
00116 
00117     //draw rectangle from which the sample will be taken
00118 
00119 
00120    /* uint64 t0 = tim.get();  // to measure display time
00121     t[frame % NAVG] = tim.get();
00122     t0 = t[frame % NAVG] - t0;
00123     if (t0 > 20000ULL) LINFO("Display took %lluus", t0);*/
00124 
00125 
00126     // compute and show framerate over the last NAVG frames:
00127     if (frame % NAVG == 0 && frame > 0)
00128       {
00129         uint64 avg = 0ULL; for (int i = 0; i < NAVG; i ++) avg += t[i];
00130         // NOTE: commented out this unused variable to avoid compiler warnings
00131         // float avg2 = 1000.0F / float(avg) * float(NAVG);
00132         //printf("Framerate: %.1f fps\n", avg2);
00133       }
00134     frame ++;
00135 
00136         float h1[2];
00137         float h2[2];
00138         float s[2];
00139         float v[2];
00140         h1[0] = h1[1] = h2[0] = h2[1] = s[0] = s[1] = v[0] = v[1] = -1;
00141 
00142         float r[2], g[2], b[2];
00143         r[0] = r[1] = g[0] = g[1] = b[0] = b[1] = -1;
00144 
00145         float temp = 0;
00146     for(int i = -sampleWidth/2; i< sampleWidth/2; i++){
00147       for(int j = -sampleHeight/2; j < sampleHeight/2; j++){
00148 
00149 
00150 
00151         avgR += (ima.getVal((centerX)+i, (centerY)+j)).red();
00152         avgG += (ima.getVal((centerX)+i, (centerY)+j)).green();
00153         avgB += (ima.getVal((centerX)+i, (centerY)+j)).blue();
00154 
00155         avgH1 += (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).H1();
00156         temp = (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).H1();
00157         if (temp < h1[0] || h1[0] == -1) h1[0] = temp;
00158         if (temp > h1[1] || h1[1] == -1) h1[1] = temp;
00159 
00160         avgH2 += (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).H2();
00161         temp = (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).H2();
00162          if (temp < h2[0] || h2[0] == -1) h2[0] = temp;
00163         if (temp > h2[1] || h2[1] == -1) h2[1] = temp;
00164 
00165        avgS += (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).S();
00166         temp = (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).S();
00167         if (temp < s[0] || s[0] == -1) s[0] = temp;
00168         if (temp > s[1] || s[1] == -1) s[1] = temp;
00169 
00170 
00171         avgV += (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).V();
00172         temp = (PixH2SV2 <float> (ima.getVal((centerX)+i, (centerY)+j))).V();
00173         if (temp < v[0]|| v[0] == -1) v[0] = temp;
00174         if (temp > v[1]|| v[1] == -1) v[1] = temp;
00175 
00176 
00177         //ima.setVal(centerX+i, centerY+j, PixRGB <byte> (0, 0, 255));
00178       }
00179     }
00180 
00181     drawRectEZ(ima, Rectangle::tlbrI((centerY)-sampleHeight/2,(centerX)-sampleWidth/2,(centerY)+sampleHeight/2,(centerX)+sampleWidth/2),
00182                PixRGB<byte>(225,
00183                             20,
00184                             20),2);
00185     win.drawImage(ima);
00186 
00187     if(count == 20){
00188       count = 0;
00189       avgR /= 8000; avgG /= 8000; avgB /= 8000;
00190       printf("\nR: %f G: %f B: %f \n", avgR, avgG, avgB);
00191       avgH1 /= 8000; avgH2 /= 8000; avgS /= 8000; avgV /= 8000;
00192       printf("H1: %f H2: %f S: %f V: %f\n", avgH1, avgH2, avgS, avgV);
00193       avgR = 0; avgG = 0; avgB = 0; avgH1 = 0; avgH2 = 0; avgS = 0; avgV = 0;
00194       printf("Max H1: %f H2: %f S: %f V: %f\n", h1[1], h2[1], s[1], v[1]);
00195 
00196       printf("Min H1: %f H2: %f S: %f V: %f\n", h1[0], h2[0], s[0], v[0]);
00197 
00198    }
00199 
00200   }
00201 
00202   // stop all our ModelComponents
00203   manager.stop();
00204 
00205   // all done!
00206   return 0;
00207 }
00208 
00209 // ######################################################################
00210 /* So things look consistent in everyone's emacs... */
00211 /* Local Variables: */
00212 /* indent-tabs-mode: nil */
00213 /* End: */
Generated on Sun May 8 08:40:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3