OpticalFlow.C

00001 /*!@file Beobot/Landmark.C Landmark class for localization */
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: Christian Siagian <siagian@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/Landmark.C $
00035 // $Id: Landmark.C 14116 2010-10-08 08:34:50Z siagian $
00036 //
00037 
00038 #include "Robots/Beobot2/Navigation/FOE_Navigation/OpticalFlow.H"
00039 
00040 // ######################################################################
00041 OpticalFlow::OpticalFlow
00042 (std::vector<rutz::shared_ptr<FlowVector> > flowVectors, 
00043  Dims dims):
00044   itsFlowVectors(flowVectors),
00045   itsImageDims(dims)
00046 {
00047   itsFlowFieldComputed   = false;
00048   itsFlowVectorsComputed = true;
00049 
00050   computeFlowLocations();
00051   itsFlowLocationsComputed = true;
00052 }
00053 
00054 // ######################################################################
00055 OpticalFlow::OpticalFlow
00056 (lobot::triple<Image<float>,Image<float>,Image<float> > flowField):
00057   itsFlowField(flowField)
00058 {
00059   itsFlowFieldComputed   = true;
00060   itsFlowVectorsComputed = false;
00061   itsFlowLocationsComputed = false;
00062 
00063   itsImageDims = itsFlowField.first.getDims();
00064 }
00065 
00066 // ######################################################################
00067 OpticalFlow::~OpticalFlow()
00068 { }
00069 
00070 // ######################################################################
00071 void OpticalFlow::computeFlowLocations()
00072 {
00073   // must have flow vectors first
00074   if(!itsFlowVectorsComputed)
00075     {
00076       computeFlowVectors();
00077     }
00078 
00079   itsFlowLocations.clear();
00080   for(uint i = 0; i < itsFlowVectors.size(); i++)
00081     {
00082       itsFlowLocations.push_back(itsFlowVectors[i]->p1);
00083     }
00084   itsFlowLocationsComputed = true;
00085 }
00086 
00087 // ######################################################################
00088 std::vector<Point2D<float> > OpticalFlow::getFlowLocations()
00089 {
00090   // must have flow locations first
00091   if(!itsFlowLocationsComputed)
00092     {
00093       computeFlowLocations();
00094     }
00095   
00096   return itsFlowLocations;
00097 }
00098 
00099 // ######################################################################
00100 std::vector<rutz::shared_ptr<FlowVector> > OpticalFlow::getFlowVectors()
00101 {
00102   // compute the flow vectors from the flow field first
00103   if(!itsFlowVectorsComputed)
00104     {
00105       computeFlowVectors();
00106     }
00107 
00108   return itsFlowVectors;
00109 }
00110 
00111 // ######################################################################
00112 Dims OpticalFlow::getImageDims()
00113 {
00114   return itsImageDims;
00115 }
00116 
00117 // ######################################################################
00118 void OpticalFlow::computeFlowVectors()
00119 {      
00120   LFATAL("NEED DEBUGGING TO CONFIRM THAT IT WORKS - then delete this line");
00121 
00122   itsFlowVectors.clear();
00123 
00124   // get iterators of the to go through all the flow field
00125   Image<float> dir = itsFlowField.first;
00126   Image<float> len = itsFlowField.second;
00127   Image<float> val = itsFlowField.third;
00128   Image<float>::iterator dptr = dir.beginw(), stop = dir.endw();
00129   Image<float>::iterator lptr = len.beginw();
00130   Image<float>::iterator vptr = val.beginw();
00131 
00132   //uint width  = dir.getWidth();
00133   uint height = dir.getHeight();
00134   uint i = 0;   uint j = 0;
00135   while(dptr != stop)
00136     {
00137       float fval = *vptr++;
00138       
00139       if(fval > 0.0)
00140         {                   
00141           rutz::shared_ptr<FlowVector>
00142             (new FlowVector
00143              (Point2D<float>(i,j), *dptr++, *lptr++, fval));
00144         }
00145 
00146       if(j >= height){ j = 0; i++; }
00147     }
00148   itsImageDims = dir.getDims();
00149 
00150   itsFlowVectorsComputed = true;
00151 }
00152 
00153 // ######################################################################
00154 void OpticalFlow::computeFlowField()
00155 {  
00156   LFATAL("NEED DEBUGGING TO CONFIRM THAT IT WORKS - then delete this line");
00157 
00158   // get iterators of the to go through all the flow field
00159   Image<float> dir(itsImageDims, ZEROS);
00160   Image<float> len(itsImageDims, ZEROS);
00161   Image<float> val(itsImageDims, ZEROS);
00162 
00163   // we are going to assume we have a sparse flow field
00164   // and random access on the field
00165   for(uint v = 0; v < itsFlowVectors.size(); v++)
00166     {
00167       Point2D<float> pt = itsFlowVectors[v]->p1;
00168       uint i = pt.i;
00169       uint j = pt.j;
00170       float ang  = itsFlowVectors[v]->angle;
00171       float mag  = itsFlowVectors[v]->mag;
00172       float fval = itsFlowVectors[v]->val;
00173       
00174       if(fval > 0.0)
00175         {
00176           dir.setVal(i,j, ang );
00177           len.setVal(i,j, mag );
00178           val.setVal(i,j, fval);            
00179         }
00180     }
00181   itsImageDims = dir.getDims();
00182 
00183   itsFlowFieldComputed = true;
00184 }
00185 
00186 
00187 // ######################################################################
00188 lobot::triple<Image<float>,Image<float>,Image<float> > 
00189 OpticalFlow::getFlowField()
00190 {
00191   // compute the flow vectors from the flow field first
00192   if(!itsFlowFieldComputed)
00193     {
00194       computeFlowField();
00195     }
00196 
00197   return itsFlowField;
00198 }
00199 
00200 // ######################################################################
00201 Image<float> OpticalFlow::getDirectionField()
00202 {
00203   // compute the flow vectors from the flow field first
00204   if(!itsFlowFieldComputed)
00205     {
00206       computeFlowField();
00207     }
00208 
00209   return itsFlowField.first;
00210 }
00211 
00212 // ######################################################################
00213 Image<float> OpticalFlow::getVectorLengthField()
00214 {
00215   // compute the flow vectors from the flow field first
00216   if(!itsFlowFieldComputed)
00217     {
00218       computeFlowField();
00219     }
00220 
00221   return itsFlowField.second;
00222 }
00223 
00224 // ######################################################################
00225 Image<float> OpticalFlow::getFlowStrengthField()
00226 {
00227   // compute the flow vectors from the flow field first
00228   if(!itsFlowFieldComputed)
00229     {
00230       computeFlowField();
00231     }
00232 
00233   return itsFlowField.third;
00234 }
00235 
00236 // ######################################################################
00237 /* So things look consistent in everyone's emacs... */
00238 /* Local Variables: */
00239 /* indent-tabs-mode: nil */
00240 /* End: */
Generated on Sun May 8 08:05:36 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3