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 #include "Image/OpenCVUtil.H"
00037
00038 #include "GUI/XWinManaged.H"
00039
00040 #include "Image/Pixels.H"
00041 #include "Raster/Raster.H"
00042
00043 #include "Util/Timer.H"
00044 #include "Util/Types.H"
00045 #include "Util/log.H"
00046
00047 #include "Image/ColorOps.H"
00048 #include "Image/CutPaste.H"
00049 #include "Image/MathOps.H"
00050 #include "Image/DrawOps.H"
00051 #include "Image/FilterOps.H"
00052 #include "Image/Transforms.H"
00053
00054 #include "BeoSub/hysteresis.H"
00055 #include "VFAT/segmentImageTrackMC.H"
00056 #include "BeoSub/HoughTransform.H"
00057 #include "BeoSub/ColorTracker.H"
00058
00059 #include "BeoSub/CannyEdge.H"
00060 #include "BeoSub/IsolateColor.H"
00061 #include "rutz/compat_cmath.h"
00062
00063 #include "BeoSub/BeoSubPipe.H"
00064
00065 #include <cstdio>
00066 #include <cstdlib>
00067 #include <cstring>
00068 #include <iostream>
00069 #include <math.h>
00070 #include <vector>
00071 #include <cmath>
00072
00073
00074 BeoSubPipe::BeoSubPipe()
00075 {
00076 houghThreshold = 30;
00077 minThreshold = 10;
00078 maxThreshold = 50;
00079 sigma = .7;
00080 tlow = 0.2;
00081 thigh = .97;
00082 linescale =80;
00083
00084
00085 foundCount = 0;
00086
00087 itsWinInitialized = false;
00088 fNum = 0;
00089 }
00090
00091
00092 BeoSubPipe::~BeoSubPipe()
00093 { }
00094
00095
00096 std::vector<LineSegment2D> BeoSubPipe::getHoughLines
00097 (Image<byte> &cameraImage,
00098 Image< PixRGB <byte> > &outputImage)
00099 {
00100 std::vector <LineSegment2D> lines;
00101
00102 #ifndef HAVE_OPENCV
00103 LFATAL("OpenCV must be installed in order to use this function");
00104 #else
00105
00106 IplImage *edge = cvCreateImage( cvGetSize(img2ipl(cameraImage)), 8, 1 );
00107 cvCanny( img2ipl(luminance(cameraImage)), edge, 100, 150, 3 );
00108
00109
00110 Image< PixRGB<byte> >houghImage = ipl2gray(edge);
00111
00112
00113
00114 CvMemStorage* storage = cvCreateMemStorage(0);
00115
00116 outputImage.clear();
00117
00118 outputImage = toRGB(houghImage);
00119
00120
00121 CvSeq* cvlines = cvHoughLines2(edge, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80 ,30, 10);
00122
00123
00124
00125 for(int i = 0; i < cvlines->total; i++ )
00126 {
00127 CvPoint* line = (CvPoint*)cvGetSeqElem(cvlines,i);
00128 Point2D<int> pt1 = Point2D<int>(line[0].x,line[0].y);
00129 Point2D<int> pt2 = Point2D<int>(line[1].x,line[1].y);
00130
00131
00132 lines.push_back(LineSegment2D(pt1,pt2));
00133 drawLine(outputImage, pt1, pt2, PixRGB<byte>(255,0,0));
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 #endif // HAVE_OPENCV
00156
00157 return lines;
00158 }
00159
00160
00161
00162 float BeoSubPipe::pipeOrientation
00163 (Image< PixRGB <byte> > &cameraImage, Image<PixRGB <byte> > &outputImage)
00164 {
00165
00166
00167
00168 Timer tim(1000000);
00169 uint w = cameraImage.getWidth();
00170 uint h = cameraImage.getHeight();
00171 Image<PixRGB<byte> > dispImage(w*2,h*2,NO_INIT);
00172
00173
00174
00175 if(!itsWinInitialized)
00176 {
00177 itsWin.reset
00178 (new XWinManaged(Dims(w*2,h*2),
00179 w+10, 0, "Pipe Direction Output"));
00180 itsWinInitialized = true;
00181 }
00182
00183 Image<byte> orangeIsoImage(w,h, ZEROS);
00184
00185 inplacePaste(dispImage, cameraImage, Point2D<int>(0,0));
00186
00187
00188 tim.reset();
00189 float orange = isolateOrange(cameraImage, orangeIsoImage);
00190 uint64 t = tim.get();
00191 LINFO("isoOrange[%f] takes: %f ms", orange, (float)t/1000.0);
00192 inplacePaste(dispImage, toRGB(orangeIsoImage), Point2D<int>(w,0));
00193
00194
00195
00196
00197 std::vector <LineSegment2D> lines;
00198 unsigned char *edge;
00199 char *dirfilename = NULL;
00200 canny(orangeIsoImage.getArrayPtr(), h, w,
00201 sigma, tlow, thigh, &edge, dirfilename);
00202
00203 Image<byte> edgeImage(edge, w, h);
00204 inplacePaste(dispImage, toRGB(edgeImage), Point2D<int>(0,h));
00205
00206 Image< PixRGB<byte> > houghImage = edgeImage;
00207 tim.reset();
00208 lines = houghTransform(edgeImage, M_PI/180, 1, houghThreshold, houghImage);
00209 t = tim.get();
00210 LINFO("houghTransform takes: %f ms", (float)t/1000.0);
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 inplacePaste(dispImage, outputImage, Point2D<int>(0,h));
00223
00224
00225
00226 uint totlines=0;
00227
00228 float pipeDir = getPipeDir(lines, 20, totlines);
00229
00230 Point2D<int> p(cameraImage.getWidth()/2, cameraImage.getHeight()/2);
00231 Point2D<int> p2((int)(cameraImage.getWidth()/2+cos(pipeDir)*linescale),
00232 (int)(cameraImage.getHeight()/2+sin(pipeDir)*linescale));
00233 outputImage = houghImage;
00234
00235
00236
00237
00238 drawLine(outputImage, p, p2, PixRGB <byte> (255, 255,0), 3);
00239
00240
00241
00242
00243
00244 inplacePaste(dispImage, outputImage, Point2D<int>(w, h));
00245 drawLine(dispImage, Point2D<int>(0,h),Point2D<int>(w*2-1,h),
00246 PixRGB<byte>(255,255,255),1);
00247 drawLine(dispImage, Point2D<int>(w,0),Point2D<int>(w,h*2-1),
00248 PixRGB<byte>(255,255,255),1);
00249 writeText( dispImage, Point2D<int>(0,0), sformat("Frame: %6d", fNum).c_str(),
00250 PixRGB<byte>(255,0,0));
00251 writeText( dispImage, Point2D<int>(w,0), sformat("Segment Color").c_str(),
00252 PixRGB<byte>(255,0,0));
00253 writeText( dispImage, Point2D<int>(0,h), sformat("Detect Edge").c_str(),
00254 PixRGB<byte>(255,0,0));
00255 writeText( dispImage, Point2D<int>(w,h), sformat("Identify Pipe").c_str(),
00256 PixRGB<byte>(255,0,0));
00257
00258
00259
00260
00261 itsWin->drawImage(dispImage, 0, 0);
00262
00263 fNum++;
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 return 0.0;
00291 }
00292
00293
00294 std::vector<LineSegment2D> BeoSubPipe::getPipes
00295 (const std::vector<LineSegment2D> lines)
00296 {
00297 uint nLines = lines.size();
00298 if(nLines == 0) { LDEBUG("*** NO LINES ***"); }
00299
00300 std::vector< std::vector<LineSegment2D> > pipeLines;
00301
00302
00303 for(uint r = 0; r < nLines; r++)
00304 {
00305 int lnIndex = -1;
00306
00307
00308 for(uint c = 0; c < pipeLines.size(); c++)
00309 {
00310 if(lines[r].angleBetween(pipeLines[c][0]) < 5*(M_PI/180))
00311 {
00312 lnIndex = c;
00313 break;
00314 }
00315
00316 }
00317
00318
00319 if(lnIndex > 0)
00320 {
00321 pipeLines[lnIndex].push_back(lines[r]);
00322
00323
00324 Point2D<int> newPt1 = Point2D<int>(((lines[r].point1().i + pipeLines[lnIndex][0].point1().i)/2),
00325 ((lines[r].point1().j + pipeLines[lnIndex][0].point1().j)/2));
00326
00327 Point2D<int> newPt2 = Point2D<int>(((lines[r].point2().i + pipeLines[lnIndex][0].point2().i)/2),
00328 ((lines[r].point2().j + pipeLines[lnIndex][0].point2().j)/2));
00329
00330 pipeLines[lnIndex][0] = LineSegment2D(newPt1,newPt2);
00331
00332 }
00333
00334 else
00335 {
00336 std::vector<LineSegment2D> newCntrLines;
00337 newCntrLines.push_back(lines[r]);
00338 pipeLines.push_back(newCntrLines);
00339 }
00340 }
00341
00342 std::vector<LineSegment2D> centerPipeLines;
00343
00344 Point2D<int> two = Point2D<int>(2,2);
00345
00346 for(uint c = 0; c < pipeLines.size(); c++)
00347 {
00348 if(pipeLines[c].size() == 2)
00349 {
00350 Point2D<int> endPoint1 = Point2D<int>((pipeLines[c][0].point1()+pipeLines[c][1].point1())/two);
00351 Point2D<int> endPoint2 = Point2D<int>((pipeLines[c][0].point2()+pipeLines[c][1].point2())/two);
00352
00353 centerPipeLines.push_back(LineSegment2D(endPoint1,endPoint2));
00354 }
00355 }
00356
00357 return centerPipeLines;
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 }
00509
00510
00511 float BeoSubPipe::getPipeDir
00512 (const std::vector<LineSegment2D> lines, const float thresh, uint &tot)
00513 {
00514 uint nLines = lines.size();
00515 if(nLines == 0) { LINFO("*** NO LINES ***"); return avgPipeAngle; }
00516
00517
00518 float sumLength = 0.0; float meanLength = 0.0;
00519 for (uint i = 0; i < nLines; i++) sumLength += lines[i].length();
00520 meanLength = sumLength / nLines;
00521
00522
00523 float sumsqr = 0.0;
00524 for (uint i = 0; i < nLines; i++)
00525 sumsqr += pow((float)(lines[i].length()) - meanLength, 2.0);
00526 float stdevLength = sqrt(sumsqr / (nLines-1));
00527
00528
00529 LINFO ("Mean: %f StdDev: %f", meanLength, stdevLength);
00530 std::vector<LineSegment2D> nlines;
00531 for(uint i = 0; i < nLines; i++)
00532 {
00533 if (lines[i].length() > (meanLength - stdevLength) &&
00534 lines[i].length() < (meanLength + stdevLength) )
00535 nlines.push_back(lines[i]);
00536 }
00537
00538 if (nlines.size() == 0)
00539 { LINFO("*** NO LINE LENGTH WITHIN STD DEV ***"); return avgPipeAngle; }
00540
00541 std::vector<LineSegment2D> flines = nlines;
00542 LINFO("\n\n\n\nSum the Average...");
00543 double sumAngle = 0;
00544 std::vector<LineSegment2D>::iterator itr = flines.begin();
00545 while(itr < flines.end())
00546 {
00547 float angle = (*itr).angle();
00548 LINFO("Line Angle[%4d,%4d][%4d,%4d]: %f",
00549 (*itr).point1().i, (*itr).point1().j,
00550 (*itr).point2().i, (*itr).point2().j,
00551 angle * 180/M_PI);
00552
00553
00554 if(!isnan(angle))
00555 {
00556 sumAngle += angle; itr++;
00557 }
00558 else
00559 { sumAngle += 90.0;
00560
00561 }
00562 }
00563 if (flines.size() == 0) return avgPipeAngle;
00564
00565
00566
00567 float meanAngle = sumAngle / flines.size();
00568 float stdAngleSum = 0.0;
00569 float stdAngleSqr = 0.0;
00570 for(uint i = 0; i < flines.size(); i++)
00571 {
00572 float angle = flines[i].angle();
00573 stdAngleSum = angle - meanAngle;
00574 stdAngleSqr += (stdAngleSum * stdAngleSum);
00575 }
00576 float stdevAngle = stdAngleSqr / flines.size();
00577 double stdtemp = sqrt((double)stdevAngle);
00578 stdevAngle = (float)stdtemp;
00579
00580
00581 sumAngle = 0.0; itr = flines.begin();
00582 while(itr < flines.end())
00583 {
00584 float angle = (*itr).angle();
00585 if(angle >= (meanAngle - stdevAngle) &&
00586 angle <= (meanAngle + stdevAngle))
00587 { sumAngle += angle; itr++; }
00588 else itr = flines.erase(itr);
00589 }
00590
00591 if (flines.size() != 0) sumAngle /= flines.size(); else return avgPipeAngle;
00592 if (sumAngle > 0) sumAngle -= M_PI;
00593
00594 float avgAngle = sumAngle;
00595
00596
00597 uint angleBuffSize = angleBuff.size();
00598
00599 bool isWithinStdDev = false;
00600
00601 if(angleBuff.size() < 2
00602 || (fabs(avgAngle - avgPipeAngle) <= stdDevPipeAngle ))
00603 {
00604 stdDevAngleCount = 0;
00605 isWithinStdDev = true;
00606 }
00607 else
00608 {
00609 stdDevAngleCount++;
00610 }
00611
00612 if(stdDevAngleCount > 5)
00613 {
00614 angleBuff.clear();
00615 isWithinStdDev = true;
00616 }
00617
00618 if(isWithinStdDev)
00619 {
00620 if(angleBuffSize >= 30)
00621 {
00622 angleBuff.pop_front();
00623 }
00624
00625 angleBuff.push_back(avgAngle);
00626
00627 angleBuffSize = angleBuff.size();
00628 }
00629
00630 if(angleBuffSize > 0)
00631 {
00632 float sumBuffAngle = 0.0;
00633 std::list<float>::iterator it;
00634
00635 for(it = angleBuff.begin(); it != angleBuff.end(); ++it)
00636 {
00637 sumBuffAngle += (*it);
00638 }
00639
00640 avgPipeAngle = sumBuffAngle / angleBuffSize;
00641
00642
00643
00644
00645 float sqrStdDevAngle = 0.0;
00646 for(it = angleBuff.begin(); it != angleBuff.end(); ++it)
00647 {
00648 sqrStdDevAngle = ((*it - avgPipeAngle) * (*it - avgPipeAngle));
00649 }
00650
00651 float stdevAngle = sqrStdDevAngle / angleBuffSize;
00652 double stdTempAngle = sqrt((double)stdevAngle);
00653 stdDevPipeAngle = (float)stdTempAngle;
00654 }
00655 else
00656 {
00657 avgPipeAngle = avgAngle;
00658 }
00659
00660
00661
00662
00663
00664
00665 return avgPipeAngle;
00666 }
00667
00668
00669
00670
00671
00672