00001 /*!@file Robots2/Beobot2/LaneFollowing/TV_Lane/TV_Lane.H Ice Module to Log data */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Chin-Kai Chang <chinkaic@usc.edu> 00033 // $HeadURL: svn://ilab.usc.edu/trunk/saliency/src/Robots/Beobot2/LaneFollowing/TV_Lane/TV_Lane.H 00034 // $Id: TV_Lane.H 12962 2010-03-06 02:13:53Z irock $ 00035 // 00036 00037 #ifndef TV_LANE 00038 #define TV_LANE 00039 #include "Image/OpenCVUtil.H" 00040 #include "Component/ModelComponent.H" 00041 #include "Component/ModelParam.H" 00042 00043 #include "Robots/RobotBrain/RobotBrainComponent.H" 00044 #include "Util/Timer.H" 00045 00046 #include "Ice/RobotBrainObjects.ice.H" 00047 #include "Ice/RobotSimEvents.ice.H" 00048 #include <IceUtil/Thread.h> 00049 00050 //#include "Demo/SaliencyMT.H" 00051 #include "Robots/Beobot2/LaneFollowing/TV_Lane/SaliencyMT.H" 00052 #include "Robots/Beobot2/BeoCommon.H" 00053 00054 #include "Media/FrameSeries.H" 00055 00056 #include "Image/Image.H" 00057 #include "Image/Point3D.H" 00058 #include "Raster/Raster.H" 00059 00060 #include <vector> 00061 #include "Transport/FrameInfo.H" 00062 #include "Gist/SuperPixel.H" 00063 00064 00065 typedef struct _RegionInformation 00066 { 00067 uint start; 00068 uint end; 00069 00070 float minDistance; 00071 uint minIndex; 00072 } 00073 RegionInformation; 00074 00075 //! for loading simulation images 00076 class imageDB 00077 { 00078 std::string path;//Full path such as "../data/corner/HNB/2010_12_34/image00000" 00079 int start; 00080 int end; 00081 int current; 00082 public: 00083 imageDB(){} 00084 00085 imageDB(std::string p,int s,int e) 00086 { 00087 start = s; 00088 end = e; 00089 path = p; 00090 current = s; 00091 } 00092 Image<PixRGB<byte> > nextImg() 00093 { 00094 std::string imgpath = sformat("%s%05d.ppm",path.c_str(),current); 00095 LDEBUG("Image path is %s",imgpath.c_str()); 00096 if(current++ > end) 00097 current = start; 00098 return Raster::ReadRGB(imgpath.c_str()); 00099 } 00100 ~imageDB(){} 00101 }; 00102 00103 00104 00105 class kalman_filter 00106 { 00107 float est_var,sensor_var; 00108 float k; 00109 float est_x; 00110 public: 00111 kalman_filter(float ini_ev,float ini_sv) 00112 { 00113 est_var = ini_ev; 00114 sensor_var = ini_sv; 00115 k = est_var/(est_var + sensor_var); 00116 est_x = 0; 00117 } 00118 float update(float measure) 00119 { 00120 if(est_x == 0) 00121 { 00122 est_x = measure; 00123 return est_x; 00124 } 00125 est_x = est_x + 10*k*(measure - est_x);//update estimate x 00126 est_var = est_var*sensor_var/(est_var+sensor_var);//update p 00127 k = est_var/(est_var+sensor_var);//update k 00128 return est_x; 00129 } 00130 00131 }; 00132 00133 class TV_Lane: public RobotBrainComponent 00134 { 00135 public: 00136 00137 TV_Lane(OptionManager& mgr, 00138 const std::string& descrName = "TV_Lane", 00139 const std::string& tagName = "TV_Lane"); 00140 00141 ~TV_Lane(); 00142 00143 virtual void evolve(); 00144 00145 //! Get a message 00146 virtual void updateMessage (const RobotSimEvents::EventMessagePtr& eMsg, const Ice::Current&); 00147 00148 virtual void registerTopics(); 00149 00150 void start1(); 00151 00152 private: 00153 00154 void openDB(const std::string& path); 00155 void loadFrame(); 00156 00157 void loadDB(const std::string& path); 00158 void drawState(); 00159 void fitLine(std::vector<Point2D<int> > points, Point2D<int>&p1,Point2D<int>&p2); 00160 Image<PixRGB<byte> > getSuperPixel(Image<PixRGB<byte> > img); 00161 Image<PixRGB<byte> > getCannyEdge(Image<PixRGB<byte> > img, Image<PixRGB<byte> > &rawCannyImg); 00162 Image<PixRGB<byte> > getSaliency(Image<PixRGB<byte> > img); 00163 int getVotingCount(Image<PixRGB<byte> > img,int topX,int topY,int windowSize,PixRGB<byte> bgColor); 00164 PixRGB<byte> getBackgroundColor(Image<PixRGB<byte> >img); 00165 Image<float> getVotingBlock(Image<PixRGB<byte> > img); 00166 Image<float> updateVoteBlock(Image<float> map); 00167 Image<float> itsVoteMap; 00168 00169 Image<PixRGB<byte> > itsCurrImg; 00170 Image<PixRGB<byte> > itsProcImg; 00171 nub::soft_ref<SaliencyMT> itsSmt; 00172 nub::soft_ref<OutputFrameSeries> itsOfs; 00173 nub::soft_ref<InputFrameSeries> itsIfs; 00174 imageDB itsImageDB; 00175 kalman_filter itsKf; 00176 int itsKfx; 00177 //! Overall Display Image 00178 Image<PixRGB<byte> > itsDispImg; 00179 00180 void updateMotor(double tran, double rot); 00181 00182 IceUtil::Mutex its_Curr_Img_mutex; //!< locking log filename 00183 IceUtil::Mutex its_Curr_Mtr_mutex; //!< locking log filename 00184 int itsCurrImgID; 00185 double itsRcTransSpeed; 00186 double itsRcRotSpeed; 00187 int itsRemoteMode; 00188 int itsPrevProcImgID; 00189 Timer itsTimer; 00190 00191 // Compute probability given variance s,mean u 00192 // ##################################################################### 00193 double getNormal(double u, double s, double x){ 00194 double coefficient = 1.0/sqrt(2.0 * M_PI * s*s); 00195 return coefficient * exp(-(x-u)*(x-u)/(2.0 * s*s)); 00196 } 00197 double getNormalWeight(double x){ 00198 double u = 0; 00199 double s = 1; 00200 return getNormal(u,s,x)/getNormal(u,s,0); 00201 } 00202 /* 00203 float myLinearity(CvSeq *seq) 00204 { 00205 int i; 00206 CvPoint2D32f *p; 00207 float *x = new float[seq->total]; 00208 float *y = new float[seq->total]; 00209 float x_bar=0.0, y_bar=0.0; 00210 float u11=0.0, u20=0.0, u02=0.0; 00211 float linearity=0.0; 00212 for (i=0; i < seq->total; i++){ 00213 p=(CvPoint2D32f*)cvGetSeqElem(seq,i); 00214 x=p->x; 00215 y=p->y; 00216 } 00217 //x_bar, y_bar 00218 for (i=0; i < seq->total; i++){ 00219 x_bar+=x; 00220 y_bar+=y; 00221 } 00222 x_bar/=seq->total; 00223 y_bar/=seq->total; 00224 for (i=0; i < seq->total; i++){ 00225 u11+=((x-x_bar)*(y-y_bar)); 00226 u20+=pow(x-x_bar,2.0f); 00227 u02+=pow(y-y_bar,2.0f); 00228 } 00229 u11/=seq->total; 00230 u20/=seq->total; 00231 u02/=seq->total; 00232 linearity = sqrt(4*pow(u11,2.0f)+pow(u20-u02,2.0f))/(u20+u02); 00233 return linearity; 00234 }*/ 00235 }; 00236 #endif 00237 00238 // ###################################################################### 00239 /* So things look consistent in everyone's emacs... */ 00240 /* Local Variables: */ 00241 /* indent-tabs-mode: nil */ 00242 /* End: */