ForegroundDetectionChannel.C

Go to the documentation of this file.
00001 /*!@file Channels/ForegroundDetectionChannel.C Wrapper around OpenCV implementation
00002  * of "Foreground Object Detection from Videos Containing Complex Background" by Huang,
00003  * et. al. in ACMMM 2003*/
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00006 // by the University of Southern California (USC) and the iLab at USC.  //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Randolph Voorhies
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Channels/ForegroundDetectionChannel.C $
00036 // $Id: ForegroundDetectionChannel.C 14605 2011-03-15 02:25:06Z dparks $
00037 //
00038 
00039 
00040 #include "Channels/ForegroundDetectionChannel.H"
00041 
00042 #include "Image/DrawOps.H"
00043 #include "Image/Kernels.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Channels/ChannelOpts.H"
00047 #include "Component/ModelOptionDef.H"
00048 
00049 
00050 ForegroundDetectionChannel::ForegroundDetectionChannel(OptionManager& mgr) :
00051   SingleChannel(mgr, "ForegroundDetectionChannel", "ForegroundDetectionChannel", FOREGROUND, rutz::shared_ptr<PyrBuilder<float> >()),
00052   itsMap(),
00053   itsLevelSpec(&OPT_LevelSpec, this)
00054 {
00055 #ifdef HAVE_OPENCV
00056   itsStatModel_cv = NULL;
00057 #else
00058   LFATAL("OpenCV is needed for Foreground Detection Channel!");
00059 #endif
00060 }
00061 
00062 // ######################################################################
00063 ForegroundDetectionChannel::~ForegroundDetectionChannel()
00064 {  }
00065 
00066 // ######################################################################
00067 bool ForegroundDetectionChannel::outputAvailable() const
00068 { return itsMap.initialized(); }
00069 
00070 // ######################################################################
00071 uint ForegroundDetectionChannel::numSubmaps() const
00072 {
00073   return 1;
00074 }
00075 
00076 // ######################################################################
00077 Dims ForegroundDetectionChannel::getMapDims() const
00078 {
00079   if (!this->hasInput())
00080     LFATAL("Oops! I haven't received any input yet");
00081 
00082   const Dims indims = this->getInputDims();
00083 
00084   return Dims(indims.w() >> itsLevelSpec.getVal().mapLevel(),
00085               indims.h() >> itsLevelSpec.getVal().mapLevel());
00086 
00087 }
00088 
00089 // ######################################################################
00090 void ForegroundDetectionChannel::getFeatures(const Point2D<int>& locn,
00091                               std::vector<float>& mean) const
00092 {
00093   LFATAL("not implemented");
00094 }
00095 
00096 // ######################################################################
00097 void ForegroundDetectionChannel::getFeaturesBatch(std::vector<Point2D<int>*> *locn,
00098                                    std::vector<std::vector<float> > *mean,
00099                                    int *count) const
00100 {
00101   LFATAL("not implemented");
00102 }
00103 
00104 
00105 // ######################################################################
00106 void ForegroundDetectionChannel::doInput(const InputFrame& inframe)
00107 {
00108 
00109 #ifdef HAVE_OPENCV
00110   ASSERT(inframe.colorByte().initialized());
00111   LINFO("Input to Foreground Detection Channel ok.");
00112 
00113   //Convert the input frame to opencv format
00114   IplImage* inFrame_cv = img2ipl(inframe.colorByte());
00115 
00116   //If the statistics model has not been created (i.e. this is the first frame),
00117   //then create it.
00118   if(itsStatModel_cv == NULL)
00119     itsStatModel_cv = cvCreateFGDStatModel( inFrame_cv );
00120 
00121   //Update the statistics model
00122   cvUpdateBGStatModel( inFrame_cv, itsStatModel_cv );
00123 
00124   //Assign the foreground and background maps
00125   //OpenCV clears out the foreground and background memory at the beginning
00126   //of every icvUpdateFGDStatModel, so let's do a deep copy of the image
00127   //data just to be safe.
00128   //Also, because the source image is byte-valued, let's divide by 255 to get
00129   //a true probability
00130   itsForegroundMap = ipl2gray(itsStatModel_cv->foreground).deepcopy() / 255.0;
00131 
00132   //Rescale the image to the correct dimensions
00133   itsMap = rescale(itsForegroundMap, this->getMapDims());
00134 
00135   float mi, ma;
00136   getMinMax(itsMap,mi, ma);
00137   LINFO("FOREGROUND MAP RANGE: [%f .. %f]", mi, ma);
00138 
00139   //Free the memory allocated to the input frame - OpenCV makes it's own deep
00140   //copy of this data internally.
00141   cvReleaseImage( &inFrame_cv );
00142 
00143 #endif
00144 
00145 
00146 }
00147 
00148 // ######################################################################
00149 Image<float> ForegroundDetectionChannel::getSubmap(const uint index) const
00150 {
00151   if (index != 0)
00152     LFATAL("got submap index = %u, but I have only one submap", index);
00153 
00154   return itsMap;
00155 }
00156 
00157 // ######################################################################
00158 Image<float> ForegroundDetectionChannel::getRawCSmap(const uint idx) const
00159 {
00160   return Image<float>();
00161 }
00162 
00163 // ######################################################################
00164 std::string ForegroundDetectionChannel::getSubmapName(const uint index) const
00165 {
00166   return std::string("ForegroundOutput");
00167 }
00168 
00169 
00170 // ######################################################################
00171 Image<float> ForegroundDetectionChannel::getOutput()
00172 { return itsMap; }
00173 
00174 
00175 // ######################################################################
00176 /* So things look consistent in everyone's emacs... */
00177 /* Local Variables: */
00178 /* indent-tabs-mode: nil */
00179 /* End: */
Generated on Sun May 8 08:40:21 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3