00001 /*!@file Neuro/SegmenterService.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/SegmenterService.C $ 00035 // $Id: SegmenterService.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #include "NeovisionII/SegmenterService.H" 00039 #include "Image/ColorOps.H" 00040 #include "GUI/DebugWin.H" 00041 00042 SegmenterI::SegmenterI(OptionManager& mgr, 00043 nub::ref<OutputFrameSeries> ofs, 00044 const std::string& descrName, 00045 const std::string& tagName ) : 00046 ModelComponent(mgr, descrName, tagName), 00047 itsOfs(ofs) 00048 { 00049 //Subscribe to the various topics 00050 IceStorm::TopicPrx topicPrx; 00051 itsTopicsSubscriptions.push_back(TopicInfo("RetinaTopic", topicPrx)); 00052 itsTopicsSubscriptions.push_back(TopicInfo("PrefrontalCortexTopic", topicPrx)); 00053 00054 nub::ref<EnvSegmenterConfigurator> esec(new EnvSegmenterConfigurator(mgr)); 00055 addSubComponent(esec); 00056 00057 itsSeg = esec->getSeg(); 00058 00059 } 00060 00061 SegmenterI::~SegmenterI() 00062 { 00063 unsubscribeSimEvents(); 00064 } 00065 00066 void SegmenterI::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 Segmenter Topic 00074 IceStorm::TopicPrx topic; 00075 try { 00076 topic = topicManager->retrieve("SegmenterTopic"); //check if the Retina topic exists 00077 } catch (const IceStorm::NoSuchTopic&) { 00078 topic = topicManager->create("SegmenterTopic"); //The retina topic does not exists, create 00079 } 00080 //Make a one way visualCortex 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 Ice::Exception& e) { 00097 cerr << e << endl; 00098 } 00099 00100 } 00101 } 00102 00103 void SegmenterI::unsubscribeSimEvents() 00104 { 00105 //Unsubscribe from all the topics we are registerd to 00106 for(uint i=0; i<itsTopicsSubscriptions.size(); i++) 00107 { 00108 itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx); 00109 } 00110 } 00111 00112 00113 //The VC excepts a retina image and posts a saliency map 00114 void SegmenterI::evolve(const SimEvents::EventMessagePtr& eMsg, 00115 const Ice::Current&) 00116 { 00117 if (eMsg->ice_isA("::SimEvents::SegmenterBiasMessage")){ //Set the tracker location 00118 SimEvents::SegmenterBiasMessagePtr msg = SimEvents::SegmenterBiasMessagePtr::dynamicCast(eMsg); 00119 00120 itsRegionsToSeg.clear(); 00121 for(uint i=0; i<msg->regionsToSeg.size(); i++) 00122 { 00123 itsRegionsToSeg.push_back(msg->regionsToSeg[i]); 00124 } 00125 } else if(eMsg->ice_isA("::SimEvents::RetinaMessage")){ 00126 00127 if (itsRegionsToSeg.size() > 0) 00128 { 00129 for(uint i=0; i<itsRegionsToSeg.size(); i++) 00130 { 00131 SimEvents::RetinaMessagePtr msg = SimEvents::RetinaMessagePtr::dynamicCast(eMsg); 00132 Image<PixRGB<byte> > retinaImg = Ice2Image<PixRGB<byte> > (msg->img); 00133 00134 Image<byte> foamask; 00135 Image<PixRGB<byte> > segmentdisp; 00136 00137 //center to segment 00138 Point2D<int> locToSeg(itsRegionsToSeg[i].loc.i, itsRegionsToSeg[i].loc.j); 00139 const Rectangle foa = itsSeg->getFoa(retinaImg, locToSeg, 00140 &foamask, &segmentdisp); 00141 00142 00143 //return the current track location 00144 SimEvents::SegmenterMessagePtr sMsg = new SimEvents::SegmenterMessage; 00145 SimEvents::SegInfo segInfo; 00146 segInfo.rect.tl.i = foa.left(); 00147 segInfo.rect.tl.j = foa.top(); 00148 segInfo.rect.br.i = foa.rightO(); 00149 segInfo.rect.br.j = foa.bottomO(); 00150 Image<PixRGB<byte> > cropImg = crop(retinaImg, foa); 00151 00152 // itsOfs->writeRGB(cropImg, "Segment", FrameInfo("Segement", SRC_POS)); 00153 00154 segInfo.img = Image2Ice(cropImg); 00155 sMsg->segLocs.push_back(segInfo); 00156 itsEventsPub->evolve(sMsg); 00157 } 00158 } else { 00159 SimEvents::RetinaMessagePtr msg = SimEvents::RetinaMessagePtr::dynamicCast(eMsg); 00160 Image<PixRGB<byte> > retinaImg = Ice2Image<PixRGB<byte> > (msg->img); 00161 00162 Image<byte> foamask; 00163 Image<PixRGB<byte> > segmentdisp; 00164 00165 //center to segment 00166 Point2D<int> locToSeg(-1,-1); 00167 const Rectangle foa = itsSeg->getFoa(retinaImg, locToSeg, 00168 &foamask, &segmentdisp); 00169 00170 //return the current segment 00171 SimEvents::SegmenterMessagePtr sMsg = new SimEvents::SegmenterMessage; 00172 SimEvents::SegInfo segInfo; 00173 segInfo.rect.tl.i = foa.left(); 00174 segInfo.rect.tl.j = foa.top(); 00175 segInfo.rect.br.i = foa.rightO(); 00176 segInfo.rect.br.j = foa.bottomO(); 00177 Image<PixRGB<byte> > cropImg = crop(retinaImg, foa); 00178 segInfo.img = Image2Ice(cropImg); 00179 sMsg->segLocs.push_back(segInfo); 00180 itsEventsPub->evolve(sMsg); 00181 00182 } 00183 00184 } 00185 } 00186 00187 /////////////////////////// The VC Service to init the retina and start as a deamon /////////////// 00188 class SegmenterService : public Ice::Service { 00189 protected: 00190 virtual bool start(int, char* argv[]); 00191 virtual bool stop() { 00192 if (itsMgr) 00193 delete itsMgr; 00194 return true; 00195 } 00196 00197 private: 00198 Ice::ObjectAdapterPtr itsAdapter; 00199 ModelManager *itsMgr; 00200 }; 00201 00202 bool SegmenterService::start(int argc, char* argv[]) 00203 { 00204 00205 itsMgr = new ModelManager("SegmenterService"); 00206 00207 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*itsMgr)); 00208 itsMgr->addSubComponent(ofs); 00209 00210 nub::ref<SegmenterI> seg(new SegmenterI(*itsMgr, ofs)); 00211 itsMgr->addSubComponent(seg); 00212 00213 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0); 00214 00215 char adapterStr[255]; 00216 sprintf(adapterStr, "default -p %i", BrainObjects::SegmenterPort); 00217 itsAdapter = communicator()->createObjectAdapterWithEndpoints("SegmenterAdapter", 00218 adapterStr); 00219 00220 Ice::ObjectPtr object = seg.get(); 00221 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("Segmenter")); 00222 seg->initSimEvents(communicator(), objectPrx); 00223 itsAdapter->activate(); 00224 00225 itsMgr->start(); 00226 00227 return true; 00228 } 00229 00230 // ###################################################################### 00231 int main(int argc, char** argv) { 00232 00233 SegmenterService svc; 00234 return svc.main(argc, argv); 00235 } 00236 00237