SaliencyMapService.C

00001 /*!@file Neuro/SaliencyMapService.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Lior Elazary
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/SaliencyMapService.C $
00035 // $Id: SaliencyMapService.C 12962 2010-03-06 02:13:53Z irock $
00036 //
00037 
00038 #include "NeovisionII/SaliencyMapService.H"
00039 #include "Image/ColorOps.H"
00040 #include "Image/ShapeOps.H"
00041 
00042 SaliencyMapI::SaliencyMapI(OptionManager& mgr,
00043     const std::string& descrName,
00044     const std::string& tagName ) :
00045   ModelComponent(mgr, descrName, tagName)
00046 {
00047     itsSMap = nub::soft_ref<EnvSaliencyMap>(new EnvSaliencyMap(mgr));
00048     addSubComponent(itsSMap);
00049 
00050     //Subscribe to the various topics
00051     IceStorm::TopicPrx topicPrx;
00052     TopicInfo tInfo("VisualCortexTopic", topicPrx);
00053     itsTopicsSubscriptions.push_back(tInfo);
00054 
00055     itsTopicsSubscriptions.push_back(TopicInfo("HippocampusTopic", topicPrx));
00056 
00057 
00058 }
00059 
00060 SaliencyMapI::~SaliencyMapI()
00061 {
00062   unsubscribeSimEvents();
00063 }
00064 
00065 
00066 void SaliencyMapI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx)
00067 {
00068   //Get the IceStorm object
00069   Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00070   IceStorm::TopicManagerPrx topicManager =
00071     IceStorm::TopicManagerPrx::checkedCast(obj);
00072 
00073   //Create a SaliencyMap Topic
00074   IceStorm::TopicPrx topic;
00075   try {
00076     topic = topicManager->retrieve("SaliencyMapTopic"); //check if the Retina topic exists
00077   } catch (const IceStorm::NoSuchTopic&) {
00078     topic = topicManager->create("SaliencyMapTopic"); //The retina topic does not exists, create
00079   }
00080   //Make a one way SaliencyMap message publisher for efficency
00081   Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00082   itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00083 
00084   //Subscribe the SimulationViewer to theTopics
00085   itsObjectPrx = objectPrx;
00086   //subscribe  to the topics
00087   for(uint i=0; i<itsTopicsSubscriptions.size();  i++)
00088   {
00089     try {
00090       IceStorm::QoS qos;
00091       itsTopicsSubscriptions[i].topicPrx =
00092         topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str()); //Get the
00093       itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx); //Subscribe to the retina topic
00094     } catch (const IceStorm::NoSuchTopic&) {
00095       LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str());
00096     } catch (const char* msg) {
00097       LINFO("Error %s", msg);
00098     } catch (const Ice::Exception& e) {
00099       cerr << e << endl;
00100     }
00101 
00102   }
00103 }
00104 
00105 
00106 void SaliencyMapI::unsubscribeSimEvents()
00107 {
00108   //Unsubscribe from all the topics we are registerd to
00109   for(uint i=0; i<itsTopicsSubscriptions.size();  i++)
00110   {
00111     itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx);
00112   }
00113 }
00114 
00115 
00116 
00117 //Get  a visualCortex Output from the visual cortex and compute the top N most salient locations
00118 void SaliencyMapI::evolve(const SimEvents::EventMessagePtr& eMsg,
00119       const Ice::Current&)
00120 {
00121   if(eMsg->ice_isA("::SimEvents::VisualCortexMessage")){
00122     SimEvents::VisualCortexMessagePtr msg = SimEvents::VisualCortexMessagePtr::dynamicCast(eMsg);
00123     Image<byte> vco = Ice2Image<byte>(msg->vco);
00124 
00125 
00126     Point2D<int> scaled_maxpos(-1,-1);
00127 
00128     //Apply the bias
00129     if (itsBiasImg.initialized())
00130       itsSMap->setBiasImg(itsBiasImg);
00131 
00132     const EnvSaliencyMap::State smstate = itsSMap->getSalmap(vco, scaled_maxpos);
00133 
00134     //send a Saliency Map message
00135     SimEvents::SaliencyMapMessagePtr sMapMsg = new SimEvents::SaliencyMapMessage;
00136     sMapMsg->smap = Image2Ice(smstate.salmap);
00137 
00138 
00139     SimEvents::LocInfoSeq mostSalientLocSeq;
00140     for(uint i=0; i<smstate.nMostSalientLoc.size(); i++)
00141     {
00142       const EnvSaliencyMap::LocInfo envLocInfo = smstate.nMostSalientLoc[i];
00143 
00144       SimEvents::LocInfo locInfo;
00145       locInfo.lowresMaxpos.i = envLocInfo.lowres_maxpos.i;
00146       locInfo.lowresMaxpos.j = envLocInfo.lowres_maxpos.j;
00147       locInfo.fullresMaxpos.i = envLocInfo.fullres_maxpos.i;
00148       locInfo.fullresMaxpos.j = envLocInfo.fullres_maxpos.j;
00149       locInfo.maxval = envLocInfo.maxval;
00150       mostSalientLocSeq.push_back(locInfo);
00151     }
00152     sMapMsg->nMostSalientLoc = mostSalientLocSeq;
00153 
00154     //Post the saliency map state
00155     itsEventsPub->evolve(sMapMsg);
00156   }
00157 
00158   if(eMsg->ice_isA("::SimEvents::SaliencyMapBiasMessage")){
00159     SimEvents::SaliencyMapBiasMessagePtr msg = SimEvents::SaliencyMapBiasMessagePtr::dynamicCast(eMsg);
00160     if (msg->biasImg.width > 0)
00161       itsBiasImg = Ice2Image<byte>(msg->biasImg);
00162     else
00163       itsSMap->resetBiasImg();
00164   }
00165 
00166 
00167 }
00168 
00169 /////////////////////////// The Retina Service to init the retina and start as a deamon ///////////////
00170 class SaliencyMapService : public Ice::Service {
00171   protected:
00172     virtual bool start(int, char* argv[]);
00173     virtual bool stop() {
00174       if (itsMgr)
00175         delete itsMgr;
00176       return true;
00177     }
00178 
00179   private:
00180     Ice::ObjectAdapterPtr itsAdapter;
00181     ModelManager *itsMgr;
00182 };
00183 
00184 bool SaliencyMapService::start(int argc, char* argv[])
00185 {
00186 
00187         itsMgr = new ModelManager("SaliencyMapService");
00188 
00189   nub::ref<SaliencyMapI> smap(new SaliencyMapI(*itsMgr));
00190         itsMgr->addSubComponent(smap);
00191 
00192   itsMgr->setOptionValString(&OPT_EsmInertiaHalfLife, "60");
00193   itsMgr->setOptionValString(&OPT_EsmIorStrength, "8.0");
00194 
00195 
00196         itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00197 
00198   char adapterStr[255];
00199   sprintf(adapterStr, "default -p %i", BrainObjects::SaliencyMapPort);
00200         itsAdapter = communicator()->createObjectAdapterWithEndpoints("SaliencyMapAdapter",
00201       adapterStr);
00202 
00203         Ice::ObjectPtr object = smap.get();
00204   Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("SaliencyMap"));
00205   smap->initSimEvents(communicator(), objectPrx);
00206         itsAdapter->activate();
00207 
00208   itsMgr->start();
00209 
00210         return true;
00211 }
00212 
00213 // ######################################################################
00214 int main(int argc, char** argv) {
00215 
00216   SaliencyMapService svc;
00217   return svc.main(argc, argv);
00218 }
00219 
00220 
Generated on Sun May 8 08:05:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3