RG_Lane.C

00001 /*!@file Robots2/Beobot2/LaneFollowing/RG_Lane/RG_Lane.C */
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/RG_Lane.C
00034 // $ $Id: RG_Lane.C 13084 2010-03-30 02:42:00Z kai $
00035 //
00036 //////////////////////////////////////////////////////////////////////////
00037 #include "Robots/Beobot2/LaneFollowing/RG_Lane/RG_Lane.H"
00038 
00039 #include "Raster/Raster.H"
00040 #include "Util/sformat.H"
00041 #include "Image/Image.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/ColorOps.H"   // for luminance()
00044 #include "Image/ShapeOps.H"   // for rescale()
00045 #include "Image/MathOps.H"    // for stdev()
00046 #include "Image/MatrixOps.H"  // for matrixMult()
00047 #include "Image/CutPaste.H"   // for inplacePaste()
00048 #include "Image/Transforms.H" // for segmentObject()
00049 #include "Image/OpenCVUtil.H"
00050 #include "Util/Timer.H"
00051 
00052 #include "SIFT/Keypoint.H"
00053 #include "SIFT/VisualObject.H"
00054 #include "SIFT/VisualObjectMatch.H"
00055 #include "Transport/FrameInfo.H"
00056 
00057 #include "Ice/IceImageUtils.H"
00058 
00059 #include "Component/ModelParam.H"
00060 #include "Component/ModelOptionDef.H"
00061 
00062 #include "GUI/DebugWin.H" // for SHOWIMG()
00063 
00064 #define FOLDER  "../data/logs"
00065 #define DBNAME  "Lane"
00066 #define IMAGE_PREFIX    "image_0000000000"
00067 
00068 // ######################################################################
00069 RG_Lane::RG_Lane(OptionManager& mgr,
00070                              const std::string& descrName, const std::string& tagName) :
00071   RobotBrainComponent(mgr, descrName, tagName),
00072   itsOfs(new OutputFrameSeries(mgr)),
00073         itsKf(80.0,30.0),
00074         itsKfx(0),
00075   itsTimer(1000000)
00076 {
00077         itsOfs->addFrameDest("display");//Add default --out=display
00078   addSubComponent(itsOfs);
00079 
00080   // load image db
00081         LINFO("load db");
00082   openDB(sformat("%s/%s/%s.conf",FOLDER,DBNAME,DBNAME));
00083                 
00084 }
00085 
00086 // ######################################################################
00087 void RG_Lane::openDB(const std::string& path)
00088 {
00089         FILE *dbconf = fopen(path.c_str(),"r");
00090         if(dbconf == NULL){
00091                 LFATAL("Can not open file: %s",path.c_str());
00092         }else{
00093                 char line[512];
00094                         while(fgets(line,sizeof line,dbconf)!= NULL)
00095                         {
00096                                 LINFO("Got line[%s]",line);
00097                                 //Skip the line start at#
00098                                 if(line[0] != '#')
00099                                 {
00100                                         char subdb[256];
00101                                         int start,end;
00102                                         int ret = sscanf(line,"%s %d %d",subdb,&start,&end);
00103                                         if(ret == 3){
00104                                                 LINFO("Got [%s],start[%d],end[%d]",subdb,start,end);
00105                                                 std::string path = sformat("%s/%s/%s/%s",FOLDER,DBNAME,subdb,IMAGE_PREFIX);
00106                                                 imageDB tmpDB(path,start,end);
00107                                                 itsImageDB = tmpDB;
00108                                         }
00109                                 }                       
00110                         }
00111         }
00112 }
00113 
00114 // ######################################################################
00115 RG_Lane::~RG_Lane()
00116 { }
00117 
00118 // ######################################################################
00119 void RG_Lane::start1()
00120 { }
00121 
00122 // ######################################################################
00123 void RG_Lane::registerTopics()
00124 {
00125   // subscribe to all sensor data
00126   //this->registerSubscription("CameraMessageTopic");
00127 }
00128 
00129 // ######################################################################
00130 void RG_Lane::evolve()
00131 {
00132   // if simulation
00133         loadFrame();
00134   drawState();  
00135   itsOfs->writeRGB(itsDispImg, "display", FrameInfo("RG_nav",SRC_POS));
00136         //itsOfs->writeRGB(itsDispImg, "RG_nav", FrameInfo("RG_nav",SRC_POS));
00137 }
00138 
00139 
00140 // ######################################################################
00141 void RG_Lane::loadFrame()
00142 {
00143   // load teach image data
00144   itsTimer.reset();
00145                         itsCurrImg =  itsImageDB.nextImg();
00146 
00147   LDEBUG("Time for Load %f",
00148         itsTimer.get()/1000.0);
00149 }
00150 // ######################################################################
00151 void RG_Lane::drawState()
00152 {
00153   uint w = 320, h = 240;
00154 
00155   itsDispImg.resize(w*2, 2*h, NO_INIT);
00156   inplacePaste(itsDispImg, itsCurrImg, Point2D<int>(0, 0));
00157 
00158         IplImage *cvImg = cvCreateImage(cvGetSize(img2ipl(itsCurrImg)),8,1);
00159 //      IplImage *cvImgColor = cvCreateImage(cvGetSize(img2ipl(itsCurrImg)),8,3);
00160         
00161         CvMemStorage *s = cvCreateMemStorage(0);
00162         CvSeq *lines = 0;
00163 
00164 
00165         cvCanny(img2ipl(luminance(itsCurrImg)),cvImg,100,150,3);
00166         itsProcImg = toRGB(ipl2gray(cvImg)); 
00167 
00168         inplacePaste(itsDispImg, toRGB(ipl2gray(cvImg)), Point2D<int>(w, 0));
00169 //      cvCvtColor(cvImg,cvImgColor,CV_GRAY2BGR);
00170         
00171         lines = cvHoughLines2(cvImg,s,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,80,30,10);
00172         
00173         int lx1 = 0,lx2 = 0,ly1 = 0,ly2 = 0,rx1 = 0,rx2 =0 ,ry1 = 0,ry2 = 0;
00174         for(int i = 0;i<MIN(lines->total,100);i++)
00175         {
00176                 CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
00177     Point2D<int> pt1 = Point2D<int>(line[0].x,line[0].y);
00178     Point2D<int> pt2 = Point2D<int>(line[1].x,line[1].y);
00179                 int dx = pt2.i - pt1.i;
00180                 int dy = pt2.j - pt1.j;
00181                 int midx = (pt1.i+pt2.i)/2;
00182                 int midy = (pt1.j+pt2.j)/2;
00183                 float slope = 0;
00184                 if(dx*dy != 0)
00185                         slope = dy/dx;  
00186                 PixRGB<byte > line_color;
00187                 if(slope == 0.0)
00188                         line_color = PixRGB<byte>(200,0,0);//Dark Red
00189                 else if(slope < 0.0 && slope > -10.0)   
00190                 {
00191                         line_color = PixRGB<byte>(128,128,200);//Light blue
00192                         LDEBUG("Left edge Road!!!! %f",slope);
00193                         lx1 = pt2.i - (pt2.j*slope);
00194                         ly1 = 0;
00195                         lx2 = pt2.i -(pt2.j-240)*slope;
00196                         ly2 = 240;
00197                 }
00198                 else if(slope <= -10.0 )        
00199                         line_color = PixRGB<byte>(0,128,0);//Green
00200                 else if(slope > 0.0 && slope < 10.0)    
00201                 {
00202                         line_color = PixRGB<byte>(0,255,0);//light green
00203                         rx1 = pt2.i - pt2.j*slope;
00204                         ry1 = 0;
00205                         rx2 = pt2.i -(pt2.j-240)*slope;
00206                         ry2 = 240;
00207                 }
00208                 else {
00209                         line_color = PixRGB<byte>(0,0,128);//blue
00210                 }
00211                 if(slope != 0.0)
00212                 {
00213                         drawLine(itsProcImg,pt1,pt2,line_color,2);
00214                         char buffer[128];
00215                         sprintf(buffer,"%1.2f",slope);
00216                         writeText(itsProcImg,Point2D<int>(midx,midy),buffer,line_color,PixRGB<byte>(0,0,0),SimpleFont::FIXED(8));
00217                 }//end slope != 0.0
00218 
00219                 //find center line
00220                         int midx1 = (lx1+rx1)/2;
00221                         //int midx2 = (lx2+rx2)/2;
00222                         //int mid_dx = midx1 -midx2;
00223                         if(midx1 != 0){
00224                                 float fkfx = itsKf.update(midx1*1.0);
00225                                 LINFO("update kalman filter old_kfx[%d],measure[%d],new_kfx[%f]",itsKfx,midx1,fkfx);
00226 
00227                                 itsKfx = (int)fkfx;
00228                         }
00229                 //LINFO("Slope %f",slope);
00230                         drawLine(itsProcImg,Point2D<int>(itsKfx,0),Point2D<int>(itsKfx,240),PixRGB<byte>(255,255,0),2);//Yellow line
00231 
00232 
00233         }//end for loop
00234         
00235         inplacePaste(itsDispImg,itsProcImg, Point2D<int>(0, h));
00236 //SHOWIMG(ipl2rgb(cvImgColor));
00237 //      IplImage *cvImg = cvCreateImage(cvGetSize(img2ipl(itsCurrImg)),8,1);
00238 
00239 //      cvPyrSegmentation(img2ipl(itsCurrImg),cvImg,s,&comp,20,40,2);
00240 }
00241 // ######################################################################
00242 void RG_Lane:: updateMessage (const RobotSimEvents::EventMessagePtr& eMsg, const Ice::Current&)
00243 {
00244 LINFO("hi");
00245 
00246 }
00247 // ######################################################################
00248 /* So things look consistent in everyone's emacs... */
00249 /* Local Variables: */
00250 /* indent-tabs-mode: nil */
00251 /* End: */
Generated on Sun May 8 08:05:36 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3