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
00038 #include "Robots/Beobot2/LaneFollowing/TV_Lane/TV_Lane.H"
00039
00040 #include "Raster/Raster.H"
00041 #include "Util/sformat.H"
00042 #include "Image/Image.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/ColorOps.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/MatrixOps.H"
00048 #include "Image/CutPaste.H"
00049 #include "Image/Transforms.H"
00050 #include "Image/Normalize.H"
00051 #include "Util/Timer.H"
00052 #include "Ice/BeobotEvents.ice.H"
00053
00054
00055 #include "SIFT/Keypoint.H"
00056 #include "SIFT/VisualObject.H"
00057 #include "SIFT/VisualObjectMatch.H"
00058 #include "Transport/FrameInfo.H"
00059
00060 #include "Ice/IceImageUtils.H"
00061
00062 #include "Component/ModelParam.H"
00063 #include "Component/ModelOptionDef.H"
00064
00065 #include "GUI/DebugWin.H"
00066
00067 #include <map>
00068 #define FOLDER "../data/logs"
00069
00070 #define DBNAME "outdoor"
00071 #define IMAGE_PREFIX "image_0000000000"
00072 #define WINDOW_SIZE 5
00073
00074 TV_Lane::TV_Lane(OptionManager& mgr,
00075 const std::string& descrName, const std::string& tagName) :
00076 RobotBrainComponent(mgr, descrName, tagName),
00077 itsSmt(new SaliencyMT(mgr,descrName,tagName)),
00078 itsOfs(new OutputFrameSeries(mgr)),
00079 itsIfs(new InputFrameSeries(mgr)),
00080 itsKf(80.0,30.0),
00081 itsKfx(0),
00082 itsTimer(1000000)
00083 {
00084 setModelParamVal("InputFameDims",Dims(320,240),MC_RECURSE|MC_IGNORE_MISSING);
00085 addSubComponent(itsSmt);
00086
00087 itsOfs->addFrameDest("display");
00088 addSubComponent(itsOfs);
00089
00090 addSubComponent(itsIfs);
00091 }
00092
00093
00094
00095
00096 TV_Lane::~TV_Lane()
00097 { }
00098
00099
00100 void TV_Lane::start1()
00101 {
00102
00103 }
00104
00105
00106 void TV_Lane::registerTopics()
00107 {
00108
00109 this->registerSubscription("CameraMessageTopic");
00110 }
00111
00112
00113 void TV_Lane::evolve()
00114 {
00115
00116 drawState();
00117 itsOfs->writeRGB(itsDispImg, "display", FrameInfo("TV_nav",SRC_POS));
00118
00119 }
00120
00121 Image<PixRGB<byte> > TV_Lane::getSuperPixel(Image<PixRGB<byte> > img)
00122 {
00123 if(!img.initialized())
00124 return Image<PixRGB<byte> >(320,240,ZEROS);
00125
00126 float sigma = .5; uint k = 500; uint minSize = 20;
00127 int num_ccs;
00128 Image<PixRGB<byte> > sp_img = segment_image(img,sigma,k,minSize,num_ccs);
00129
00130 int w = sp_img.getWidth();
00131 int h = sp_img.getHeight();
00132
00133
00134
00135
00136 std::vector<PixRGB<byte> > color_map;
00137 std::vector<int> color_map_count;
00138
00139 PixRGB<byte> roadColor;
00140 for(int i = -10; i<= 10;i++)
00141 {
00142 PixRGB<byte> tmp_color = sp_img.getVal((w/2) + i,h-5);
00143
00144 bool notfound = true;
00145
00146
00147
00148 for(int j = 0; j < (int)color_map.size() && notfound ; j++)
00149 {
00150 if(color_map[j] == tmp_color)
00151 {
00152 notfound = false;
00153 color_map_count[j]++;
00154 }
00155 }
00156 if(notfound)
00157 {
00158
00159 color_map.push_back(tmp_color);
00160 color_map_count.push_back(0);
00161 }
00162 }
00163
00164 if(color_map.size() != 1)
00165 {
00166
00167 int max = color_map_count[0];
00168 int max_index = 0;
00169 for(int i = 1; i < (int)color_map_count.size() ; i++)
00170 {
00171 if(max < color_map_count[i])
00172 {
00173 max = color_map_count[i];
00174 max_index = i;
00175 }
00176 }
00177 roadColor = color_map[max_index];
00178 LINFO("Max count color have count %d",max);
00179 }
00180 else
00181 {
00182 roadColor = color_map[0];
00183 LINFO("Only one color (%d,%d,%d)",roadColor.red(),roadColor.green(),roadColor.blue());
00184 }
00185
00186
00187 Image<PixRGB<byte> > output(w,h,ZEROS);
00188 for(int y = 0 ; y < h ; y ++)
00189 {
00190 for(int x = 0 ; x < w ; x ++)
00191 {
00192 PixRGB<byte> c = sp_img.getVal(x,y);
00193
00194
00195 if(c == roadColor) {
00196
00197
00198 output.setVal(x,y,PixRGB<byte>(255,0,0));
00199
00200
00201 }
00202 else{
00203 output.setVal(x,y,PixRGB<byte>(0,0,0));
00204
00205
00206 }
00207
00208 }
00209 }
00210 if(!output.initialized())
00211 return img;
00212 return output;
00213
00214
00215
00216 }
00217
00218
00219 int TV_Lane::getVotingCount(Image<PixRGB<byte> > img,int topX,int topY,int windowSize,PixRGB<byte> bgColor)
00220 {
00221 if(!img.initialized())
00222 return 0;
00223
00224 int w = img.getWidth();
00225 int h = img.getHeight();
00226 int count = 0;
00227 for(int i = topX ; i < topX + windowSize ; i++){
00228 for(int j = topY ; j < topY + windowSize ; j++){
00229
00230 if(i < w && j < h){
00231 PixRGB<byte> tmp_color = img.getVal(i,j);
00232 if(tmp_color != bgColor)
00233 count++;
00234 }
00235 }
00236 }
00237 return count;
00238
00239 }
00240
00241 PixRGB<byte> TV_Lane::getBackgroundColor(Image<PixRGB<byte> >img)
00242 {
00243
00244 int w = img.getWidth();
00245 int h = img.getHeight();
00246
00247 std::vector<PixRGB<byte> > color_map;
00248 std::vector<int> color_map_count;
00249 for(int i = 0; i < w; i++){
00250 for(int j = 0 ; j < h; j++){
00251
00252 if(i < w && j < h){
00253 PixRGB<byte> tmp_color = img.getVal(i,j);
00254 bool notfound = true;
00255
00256
00257 for(int k = 0; k < (int)color_map.size() && notfound ; k++)
00258 {
00259 if(color_map[k] == tmp_color)
00260 {
00261 notfound = false;
00262 color_map_count[k]++;
00263 }
00264 }
00265 if(notfound)
00266 {
00267
00268 color_map.push_back(tmp_color);
00269 color_map_count.push_back(0);
00270 }
00271 }
00272 }
00273 }
00274 PixRGB<byte> bgColor;
00275 if(color_map.size() != 1){
00276
00277 int max = color_map_count[0];
00278 int max_index = 0;
00279 for(int i = 1; i < (int)color_map_count.size() ; i++){
00280 if(max < color_map_count[i]){
00281 max = color_map_count[i];
00282 max_index = i;
00283 }
00284 }
00285 bgColor= color_map[max_index];
00286 LINFO("Max count color have count %d",max);
00287 }else{
00288 bgColor = color_map[0];
00289 }
00290
00291 return bgColor;
00292
00293
00294 }
00295
00296 Image<PixRGB<byte> > TV_Lane::getCannyEdge(Image<PixRGB<byte> > img,Image<PixRGB<byte> > &rawCannyImg){
00297
00298
00299
00300 IplImage *cvImg = cvCreateImage(cvGetSize(img2ipl(luminance(img))),8,1);
00301
00302 IplConvKernel* element = 0;
00303 element = cvCreateStructuringElementEx(2,2,1,1,CV_SHAPE_ELLIPSE,0);
00304 cvErode(img2ipl(luminance(img)),cvImg,element,3);
00305 cvDilate(img2ipl(luminance(img)),cvImg,element,1);
00306 CvMemStorage *s = cvCreateMemStorage(0);
00307 CvSeq *lines = 0;
00308
00309
00310
00311 Image<PixRGB<byte> > edgeImg = toRGB(ipl2gray(cvImg));
00312 return edgeImg;
00313 rawCannyImg = edgeImg;
00314
00315 lines = cvHoughLines2(cvImg,s,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,80,30,10);
00316
00317 int lx1 = 0,lx2 = 0,ly1 = 0,ly2 = 0,rx1 = 0,rx2 =0 ,ry1 = 0,ry2 = 0;
00318 LINFO("Total Line %d",lines->total);
00319 for(int i = 0;i<MIN(lines->total,100);i++)
00320 {
00321 CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
00322 Point2D<int> pt1 = Point2D<int>(line[0].x,line[0].y);
00323 Point2D<int> pt2 = Point2D<int>(line[1].x,line[1].y);
00324 int dx = pt2.i - pt1.i;
00325 int dy = pt2.j - pt1.j;
00326 int midx = (pt1.i+pt2.i)/2;
00327 int midy = (pt1.j+pt2.j)/2;
00328 float slope = 0;
00329 if(dx*dy != 0)
00330 slope = dy/dx;
00331 PixRGB<byte > line_color;
00332 if(slope == 0.0)
00333 line_color = PixRGB<byte>(200,0,0);
00334 else if(slope < 0.0 && slope > -10.0)
00335 {
00336 line_color = PixRGB<byte>(128,128,200);
00337 LDEBUG("Left edge Road!!!! %f",slope);
00338 lx1 = pt2.i - (pt2.j*slope);
00339 ly1 = 0;
00340 lx2 = pt2.i -(pt2.j-240)*slope;
00341 ly2 = 240;
00342 }
00343 else if(slope <= -10.0 )
00344 line_color = PixRGB<byte>(0,128,0);
00345 else if(slope > 0.0 && slope < 10.0)
00346 {
00347 line_color = PixRGB<byte>(0,255,0);
00348 rx1 = pt2.i - pt2.j*slope;
00349 ry1 = 0;
00350 rx2 = pt2.i -(pt2.j-240)*slope;
00351 ry2 = 240;
00352 }
00353 else {
00354 line_color = PixRGB<byte>(0,0,128);
00355 }
00356 if(slope != 0.0)
00357 {
00358 drawLine(edgeImg,pt1,pt2,line_color,2);
00359 char buffer[128];
00360 sprintf(buffer,"%1.2f",slope);
00361 writeText(edgeImg,Point2D<int>(midx,midy),buffer,line_color,PixRGB<byte>(0,0,0),SimpleFont::FIXED(8));
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 }
00380 return edgeImg;
00381
00382
00383 }
00384
00385
00386 Image<float> TV_Lane::getVotingBlock(Image<PixRGB<byte> > img)
00387 {
00388 if(!img.initialized())
00389 return Image<float>(320,240,ZEROS);
00390
00391
00392 int w = img.getWidth();
00393 int h = img.getHeight();
00394 PixRGB<byte> bgColor = getBackgroundColor(img);
00395 Image<float> vote_map(w/WINDOW_SIZE + 1,h/WINDOW_SIZE +1,ZEROS);
00396 char buffer[200];
00397 for(int y = 0 ; y < h ; y += WINDOW_SIZE)
00398 {
00399 for(int x = 0 ; x < w ; x += WINDOW_SIZE)
00400 {
00401 int count = getVotingCount(img,x,y,WINDOW_SIZE,bgColor);
00402 sprintf(buffer,"%2d",count/10);
00403
00404
00405 int ww = WINDOW_SIZE*WINDOW_SIZE;
00406 float weight = (ww-count);
00407
00408 if(x < w && y < h)
00409 vote_map.setVal(x/WINDOW_SIZE,y/WINDOW_SIZE,weight);
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 }
00422 }
00423
00424
00425 Image<PixRGB<byte> > output(w,h,ZEROS);
00426 for(int y = 0 ; y < h ; y ++)
00427 {
00428 for(int x = 0 ; x < w ; x ++)
00429 {
00430
00431 }
00432 }
00433 return vote_map;
00434
00435
00436
00437 }
00438
00439
00440 Image<float> TV_Lane::updateVoteBlock(Image<float> map)
00441 {
00442 int w = map.getWidth();
00443 int h = map.getHeight();
00444 LINFO("Do it w,h = %d %d",w,h);
00445 Image<float> new_map(w,h,ZEROS);
00446 int ww = WINDOW_SIZE*WINDOW_SIZE;
00447 for(int y = 0 ; y < h ; y ++)
00448 {
00449 for(int x = 0 ; x < w ; x ++ )
00450 {
00451 float root_weight = map.getVal(x,y);
00452
00453 float left_w = 0,right_w = 0,up_w = 0,down_w = 0;
00454
00455 if(x-1 > 0)
00456 left_w = map.getVal(x-1,y);
00457 if(x+1 < w)
00458 right_w = map.getVal(x+1,y);
00459 if(y-1 > 0)
00460 up_w = map.getVal(x,y-1);
00461 if(y+1 < h)
00462 down_w = map.getVal(x,y+1);
00463
00464
00465
00466 if(root_weight > ww/2)
00467 {
00468 bool keepgoing = true;
00469 int length = 0;
00470 std::vector<Point2D<int> >line;
00471 int y2 = y;
00472 while(keepgoing)
00473 {
00474
00475 if(y2+1 < h)
00476 {
00477 keepgoing = false;
00478 for(int child = 0;child < 1;child++)
00479 {
00480 if(x+child > 0 && x+child < w)
00481 {
00482 float childWeight = map.getVal(x+child,y2+1);
00483 if(childWeight >= root_weight)
00484 {
00485 if(length >60)
00486 LINFO("(%d,%d) weight %f count %d",x+child,y2+1,childWeight,length);
00487 if(!keepgoing)
00488 length++;
00489 keepgoing = true;
00490 line.push_back(Point2D<int>(x+child,y2+1));
00491 new_map.setVal(x+child,y2+1,childWeight);
00492
00493 }
00494
00495 }
00496
00497 }
00498 y2++;
00499 }else{
00500 keepgoing = false;
00501 }
00502
00503 }
00504 if(length > 70){
00505 LINFO("find line length:%d",length);
00506
00507 length = 0;
00508 }
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 }
00571 }
00572 return new_map;
00573 }
00574
00575 void TV_Lane::drawState()
00576 {
00577 Image<PixRGB<byte> > voteImg;
00578 int w=0,h=0;
00579 if(!itsCurrImg.initialized()){
00580 itsCurrImg = itsIfs->readRGB();
00581
00582 itsVoteMap = getVotingBlock(itsCurrImg);
00583 voteImg = normalizeFloat(itsVoteMap,FLOAT_NORM_0_255);
00584 w = itsCurrImg.getWidth()/2;
00585 h = itsCurrImg.getHeight()/2;
00586 itsDispImg.resize(w*4,h,ZEROS);
00587
00588 inplacePaste(itsDispImg,rescale(voteImg,w,h),Point2D<int>(w,0));
00589
00590 itsVoteMap = updateVoteBlock(itsVoteMap);
00591 voteImg = normalizeFloat(itsVoteMap,FLOAT_NORM_0_255);
00592 inplacePaste(itsDispImg,rescale(voteImg,w,h),Point2D<int>(w*2,0));
00593
00594 Image<PixRGB<byte> > cannyImg;
00595 cannyImg = getCannyEdge(voteImg,cannyImg);
00596
00597 inplacePaste(itsDispImg, rescale(itsCurrImg,w,h), Point2D<int>(0, 0));
00598 inplacePaste(itsDispImg,rescale(cannyImg,w,h),Point2D<int>(w*3,0));
00599
00600 }else{
00601 w = itsCurrImg.getWidth()/2;
00602 h = itsCurrImg.getHeight()/2;
00603
00604 }
00605
00606
00607
00608 Raster::waitForKey();
00609 itsVoteMap = updateVoteBlock(itsVoteMap);
00610 LINFO("Do Once1");
00611 voteImg = normalizeFloat(itsVoteMap,FLOAT_NORM_0_255);
00612 LINFO("Do Once2");
00613 inplacePaste(itsDispImg,rescale(voteImg,w,h),Point2D<int>(w*2,0));
00614 LINFO("Do Once3");
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629 }
00630
00631
00632
00633 void TV_Lane:: updateMessage
00634 (const RobotSimEvents::EventMessagePtr& eMsg, const Ice::Current&)
00635 {
00636
00637 if(eMsg->ice_isA("::BeobotEvents::CameraMessage"))
00638 {
00639
00640 BeobotEvents::CameraMessagePtr cameraMsg =
00641 BeobotEvents::CameraMessagePtr::dynamicCast(eMsg);
00642
00643 int currRequestID = cameraMsg->RequestID;
00644 Image<PixRGB<byte> > img = Ice2Image<PixRGB<byte> >(cameraMsg->image);
00645
00646 LDEBUG("Got a CameraMessage with Request ID = %d", currRequestID);
00647
00648 its_Curr_Img_mutex.lock();
00649 itsCurrImg = img;
00650 itsCurrImgID = cameraMsg->RequestID;
00651 its_Curr_Img_mutex.unlock();
00652 }
00653
00654
00655 else if(eMsg->ice_isA("::BeobotEvents::MotorMessage"))
00656 {
00657 BeobotEvents::MotorMessagePtr mtrMsg =
00658 BeobotEvents::MotorMessagePtr::dynamicCast(eMsg);
00659 LDEBUG("Got a MotorMessage with Request ID = %d: RC Trans %f, Rot %f",
00660 mtrMsg->RequestID, itsRcTransSpeed, itsRcRotSpeed);
00661 its_Curr_Mtr_mutex.lock();
00662 itsRemoteMode = mtrMsg->rcMode;
00663 itsRcTransSpeed = mtrMsg->rcTransVel;
00664 itsRcRotSpeed = mtrMsg->rcRotVel;
00665 its_Curr_Mtr_mutex.unlock();
00666 }
00667
00668
00669
00670 }
00671
00672
00673
00674
00675
00676