00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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"
00044 #include "Image/ShapeOps.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/MatrixOps.H"
00047 #include "Image/CutPaste.H"
00048 #include "Image/Transforms.H"
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"
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");
00078 addSubComponent(itsOfs);
00079
00080
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
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
00126
00127 }
00128
00129
00130 void RG_Lane::evolve()
00131 {
00132
00133 loadFrame();
00134 drawState();
00135 itsOfs->writeRGB(itsDispImg, "display", FrameInfo("RG_nav",SRC_POS));
00136
00137 }
00138
00139
00140
00141 void RG_Lane::loadFrame()
00142 {
00143
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
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
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);
00189 else if(slope < 0.0 && slope > -10.0)
00190 {
00191 line_color = PixRGB<byte>(128,128,200);
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);
00200 else if(slope > 0.0 && slope < 10.0)
00201 {
00202 line_color = PixRGB<byte>(0,255,0);
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);
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 }
00218
00219
00220 int midx1 = (lx1+rx1)/2;
00221
00222
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
00230 drawLine(itsProcImg,Point2D<int>(itsKfx,0),Point2D<int>(itsKfx,240),PixRGB<byte>(255,255,0),2);
00231
00232
00233 }
00234
00235 inplacePaste(itsDispImg,itsProcImg, Point2D<int>(0, h));
00236
00237
00238
00239
00240 }
00241
00242 void RG_Lane:: updateMessage (const RobotSimEvents::EventMessagePtr& eMsg, const Ice::Current&)
00243 {
00244 LINFO("hi");
00245
00246 }
00247
00248
00249
00250
00251