Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Modules/ImageProcessor/RasterImageProcessor/RasterSpecialist.cpp

Go to the documentation of this file.
00001 /**
00002 * @file RasterSpecialist.cpp
00003 * 
00004 * This file contains the implementation of class RasterSpecialist.
00005 *
00006 * @author <a href="mailto:sadprofessor@web.de">Bernd Schmidt</a>
00007 */
00008 #include "RasterSpecialist.h"
00009 
00010 using namespace std;
00011 
00012 typedef list<list<RasterSpecialist::LinePair> > SegmentList;
00013 
00014 RasterSpecialist::RasterSpecialist(RasterImageProcessor &processor)
00015 : rip(&processor),
00016   Y_VALUE(0),
00017   U_VALUE(cameraResolutionHeight_ERS7),
00018   V_VALUE(2 * cameraResolutionHeight_ERS7),
00019   imageWidth(cameraResolutionWidth_ERS7),
00020   imageHeight(cameraResolutionHeight_ERS7) 
00021 { 
00022   postScanNeeded = false;
00023   preScanNeeded = false;
00024 }
00025 
00026 RasterSpecialist::~RasterSpecialist()
00027 {
00028 
00029 }
00030 
00031 RasterSpecialist::LinePair::LinePair():
00032   v1(0,0),v2(0,0),segment(-1),color(noColor)
00033 {
00034 
00035 }
00036 
00037 RasterSpecialist::LinePair::LinePair(Vector2<int> vec1, Vector2<int> vec2):
00038   v1(vec1),v2(vec2),segment(-1),color(noColor)
00039 {
00040 
00041 }
00042 
00043 RasterSpecialist::LinePair::LinePair(Vector2<int> vec1,Vector2<int> vec2,colorClass c):
00044   v1(vec1),v2(vec2),color(c)
00045 {
00046 
00047 }
00048 
00049 RasterSpecialist::Box::Box(int minX,int minY,int maxX,int maxY):
00050   minX(minX),maxX(maxX),minY(minY),maxY(maxY)
00051 {
00052     
00053 }
00054  
00055 bool RasterSpecialist::createGridSegments(std::list<GridLP>& lines,
00056   std::vector<std::list<GridLP> >& segments,int xSpace, int ySpace){
00057 if (lines.size() == 0) return false;
00058     int maxSegments=20;
00059   int segs[20];
00060   int finalSegs[20];
00061   int segCount = 0;
00062   int finalCount = 0;
00063   list<GridLP>::iterator it = 0;
00064   list<GridLP>::iterator rit = 0;
00065 
00066   for (unsigned int i=0;i<20;i++) {
00067      segs[i]=0;
00068      finalSegs[i]=0;
00069   }
00070 
00071   it=lines.begin();
00072   it ->segment = segCount;
00073   segs[segCount] = segCount;
00074   segCount++;
00075 
00076 
00077     for (it++;it!=lines.end();it++) {
00078     rit = it;
00079     int lead = -1;
00080     
00081     do{
00082       rit--;
00083       if((it->column - rit->column) > ySpace) break;
00084       if (!gridFit(*it,*rit,xSpace)) continue;
00085       if (lead == -1){
00086         //erstem Segment zuordnen
00087         lead = segs[rit->segment];
00088         it->segment = lead;
00089 
00090       }
00091       else{
00092         int newSeg = segs[rit->segment];
00093         if (newSeg == lead) continue;
00094         
00095         //weiteres segment gefunden
00096         //segmente werden vereinigt
00097         if (lead < newSeg)
00098           segs[newSeg] = lead;
00099         else segs[lead] = newSeg;
00100 
00101       }
00102     }while( (rit != lines.begin()));
00103     if (lead == -1){
00104       //neues Segment gefunden
00105       it->segment = segCount;
00106       segs[segCount] = segCount;
00107       segCount++;
00108       if (segCount>maxSegments-1)
00109         return false;
00110     }
00111     
00112   }
00113 
00114   // calculating final segment number
00115   for(int j=0;j<maxSegments;++j){
00116     if (j == segs[j]){ 
00117       finalSegs[j] = finalCount;
00118       segments.push_back(list<GridLP>());
00119       finalCount++;
00120     }
00121     else finalSegs[j] = finalSegs[segs[j]];
00122   }
00123 
00124   //collecting linepairs in calculated segments
00125   for(it = lines.begin();it != lines.end();++it)
00126     segments[finalSegs[it->segment]].push_back(*it);
00127 
00128   //OUTPUT(idText,text,"lines in segments "<<  linesAfter);
00129 //  OUTPUT(idText,text,"found segments "<<  segments.size());
00130 
00131   //segmentation finished successfully
00132   return true;
00133 }
00134 
00135 bool RasterSpecialist::createSegmentsFromLines2(std::list<LinePair>& lines,
00136   std::vector<std::list<LinePair> >& segments,int xSpace, int ySpace){
00137  //   OUTPUT(idText,text,"size before "<<  lines.size());
00138   if (lines.size() == 0) return false;
00139     const int maxSegments=50;
00140   int segs[maxSegments];
00141   int finalSegs[maxSegments];
00142   int segCount = 0;
00143   int finalCount = 0;
00144   list<LinePair>::iterator it = 0;
00145   list<LinePair>::iterator rit = 0;
00146 
00147   for (int i=0;i<maxSegments;i++) {
00148      segs[i]=0;
00149      finalSegs[i]=0;
00150   }
00151 
00152   it=lines.begin();
00153   it ->segment = segCount;
00154   segs[segCount] = segCount;
00155   segCount++;
00156 
00157 
00158     for (it++;it!=lines.end();it++) {
00159     rit = it;
00160     int lead = -1;
00161     
00162     do{
00163       rit--;
00164       if((it->v1.y - rit->v1.y) > ySpace) break;
00165       if (!linePairsFit(*it,*rit,xSpace)) continue;
00166       if (lead == -1){
00167         //erstem Segment zuordnen
00168         lead = segs[rit->segment];
00169         it->segment = lead;
00170 
00171       }
00172       else{
00173         int newSeg = segs[rit->segment];
00174         if (newSeg == lead) continue;
00175         
00176         //weiteres segment gefunden
00177         //segmente werden vereinigt
00178         if (lead < newSeg)
00179           segs[newSeg] = lead;
00180         else segs[lead] = newSeg;
00181 
00182       }
00183     }while( (rit != lines.begin()));
00184     if (lead == -1){
00185       //neues Segment gefunden
00186       it->segment = segCount;
00187       segs[segCount] = segCount;
00188       segCount++;
00189       if (segCount>maxSegments-1)
00190         return false;
00191     }
00192     
00193   }
00194 
00195   // calculating final segment number
00196   for(int j=0;j<maxSegments;++j){
00197     if (j == segs[j]){ 
00198       finalSegs[j] = finalCount;
00199       segments.push_back(list<LinePair>());
00200       finalCount++;
00201     }
00202     else finalSegs[j] = finalSegs[segs[j]];
00203   }
00204 
00205   //collecting linepairs in calculated segments
00206   for(it = lines.begin();it != lines.end();++it)
00207     segments[finalSegs[it->segment]].push_back(*it);
00208 
00209   //OUTPUT(idText,text,"lines in segments "<<  linesAfter);
00210 //  OUTPUT(idText,text,"found segments "<<  segments.size());
00211 
00212   //segmentation finished successfully
00213   return true;
00214 }
00215 
00216 
00217 /** Clusters the LinePairs and puts the segments in v */
00218 void RasterSpecialist::createSegmentsFromLines(std::list<LinePair>& lines,
00219   std::vector<std::list<LinePair> >& segments,int xSpace, int ySpace){
00220 //this algorithm runs on simulator, but seems to have bugs on the robot!
00221   OUTPUT(idText,text,"size before "<<  lines.size());
00222   if (lines.size() == 0) return;
00223   int allowedSpace = ySpace;
00224   segList.clear();
00225   //SegmentList segList;  
00226   SegmentList::iterator sit = 0;
00227   SegmentList::iterator sit2 = 0;
00228   list<LinePair>::iterator lit = lines.begin();
00229   list<LinePair>::iterator it = 0;
00230   list<LinePair>::iterator it2 = 0;
00231   LinePair compare;
00232   list<LinePair> *currentSeg;
00233     
00234   segList.push_back(list<LinePair>());
00235   segList.back().push_back(*lit);
00236   //compare = segList.front().back();
00237   //lines.pop_front();
00238   
00239   for(++lit;lit!=lines.end();++lit){
00240     
00241     LinePair& current = *lit;
00242     //lines.pop_front();
00243     
00244     list<LinePair > *lead = 0;
00245 
00246     for(sit = segList.begin();sit != segList.end();sit++){
00247       if (sit->empty()) continue;
00248       currentSeg = &(*sit);
00249       it = currentSeg->end();
00250       it--;
00251       compare = *it;
00252       //continue; 
00253       //remove segment if it's far away
00254       if (current.v1.y - compare.v1.y > allowedSpace){
00255         //if (!currentSeg->empty())
00256         segments.push_back(*currentSeg);
00257 
00258         sit2 = sit;
00259         sit++;
00260         segList.erase(sit2); //könnte Probleme machen
00261         sit--;
00262         continue;
00263       }
00264 
00265       if (linePairsFit(current,compare,xSpace)){
00266         currentSeg->push_back(current);
00267         lead = currentSeg;
00268         continue;
00269       }
00270       else if(it == currentSeg->begin()) continue;  
00271 
00272       //compare all Line Pairs of currentSeg with current
00273       //ist it != 0 sicher?
00274       do{
00275         it--;
00276         compare = *it;
00277         //compare all relevant LinePairs
00278         if(linePairsFit(current,compare,xSpace)){
00279           if(!lead){
00280           //mark lead and put the segment to it
00281             currentSeg->push_back(current);
00282             lead = currentSeg;
00283             break;
00284           }
00285           else{
00286           //merge two segments and remove the second one from segList
00287             lead->merge(*currentSeg);
00288             sit2 = sit;
00289             sit++;
00290             segList.erase(sit2); //könnte Probleme machen
00291             sit--;
00292             sit2 = 0;
00293             break;
00294           }
00295         }
00296       
00297 
00298 
00299       }while(it != currentSeg->begin() && current.v1.y - compare.v1.y <= allowedSpace);
00300 
00301 
00302     }
00303     //no segment found for current, so create new segment with current        
00304     if (!lead){
00305       segList.push_back(list<LinePair>());
00306       segList.back().push_back(current);
00307     }
00308 
00309 
00310   }
00311   
00312   //put all non-finished segments in segments-vector
00313   if (!segList.empty())
00314     for(sit = segList.begin();sit != segList.end();sit++){
00315     currentSeg = &(*sit);
00316     if (!currentSeg->empty())
00317       segments.push_back(*currentSeg);
00318   }
00319   
00320   //if no segments found return (only a bug-fix)
00321   if (segments.empty()) return;
00322 
00323   vector<list<LinePair> >::iterator iter = segments.begin();
00324   int linesAfter = 0;
00325   while(iter != segments.end()){
00326     currentSeg = &(*iter);
00327     linesAfter+= currentSeg->size();
00328     iter++;
00329   }
00330   OUTPUT(idText,text,"lines in segments "<<  linesAfter);
00331   OUTPUT(idText,text,"found segments "<<  segments.size());
00332   OUTPUT(idText,text,"size of segList "<<  segList.size());
00333 }
00334 
00335 bool RasterSpecialist::doColorSegmentation(std::vector<LinePair>& lines,
00336   std::vector<std::vector<LinePair> >& segments,int xSpace,int ySpace){
00337   
00338   if (lines.size() == 0) return false;
00339     int maxSegments=20;
00340   int segs[20];
00341   int finalSegs[20];
00342   int segCount = 0;
00343   int finalCount = 0;
00344   vector<LinePair>::iterator it;
00345   vector<LinePair>::iterator rit;
00346 
00347   for (unsigned int i=0;i<20;i++) {
00348      segs[i]=0;
00349      finalSegs[i]=0;
00350   }
00351 
00352   it=lines.begin();
00353   it ->segment = segCount;
00354   segs[segCount] = segCount;
00355   segCount++;
00356 
00357 
00358     for (it++;it!=lines.end();it++) {
00359     rit = it;
00360     int lead = -1;
00361 
00362     /*if (rit != lines.begin()) */do{
00363       rit--;
00364       if (!linesFit(*it,*rit,xSpace)) continue;
00365       if (lead == -1){
00366         //erstem Segment zuordnen
00367         lead = segs[rit->segment];
00368         it->segment = lead;
00369 
00370       }
00371       else{
00372         int newSeg = segs[rit->segment];
00373         if (newSeg == lead) continue;
00374         
00375         //weiteres segment gefunden
00376         //segmente werden vereinigt
00377         if (lead < newSeg)
00378           segs[newSeg] = lead;
00379         else segs[lead] = newSeg;
00380 
00381       }
00382     }while(((it->v1.y - rit->v1.y) <= ySpace) && (rit != lines.begin()));
00383     if (lead == -1){
00384       //neues Segment gefunden
00385       it->segment = segCount;
00386       segs[segCount] = segCount;
00387       segCount++;
00388       if (segCount>maxSegments-1)
00389         return false;
00390     }
00391     
00392   }
00393 
00394   // calculating final segment number
00395   for(int j=0;j<segCount;++j){
00396     if (j == segs[j]){ 
00397       finalSegs[j] = finalCount;
00398       segments.push_back(vector<LinePair>());
00399       finalCount++;
00400     }
00401     else finalSegs[j] = finalSegs[segs[j]];
00402   }
00403 
00404   //collecting linepairs in calculated segments
00405   for(it = lines.begin();it != lines.end();++it)
00406     segments[finalSegs[it->segment]].push_back(*it);
00407 
00408   //OUTPUT(idText,text,"lines in segments "<<  linesAfter);
00409   OUTPUT(idText,text,"found segments "<< segments.size());
00410 
00411   //segmentation finished successfully
00412   return true;
00413 }
00414 
00415 //TODO: debug calculation of convex hull
00416 void RasterSpecialist::createConvexPoly(std::list<LinePair>& input,std::vector<Vector2<int> > output){
00417   list<LinePair>::iterator it = input.begin();
00418   output.push_back(it->v1);
00419   list<LinePair>::iterator rit = 0;
00420   
00421   double t1 = 0;
00422   double t2 = 0;
00423   int row = it->v1.y;
00424   while (it != input.end() && it->v1.y == row) it++;
00425   t1 = theta(output.back(),it->v1);
00426   output.push_back(it->v1);
00427 
00428   //finding Convex-Hull along the left side top-down
00429   for(;it != input.end();++it){
00430     if (it->v1.y == row) continue;
00431     
00432     row = it->v1.y;
00433     t2 = theta(output.back(),it->v1);
00434     
00435     while (t1 >= t2){
00436       output.pop_back();
00437       if (output.size() < 2){ 
00438         t2 = theta(output.front(),it->v1);
00439         break;
00440       }
00441       t1 = theta(output[output.size()-2],output.back());
00442       t2 = theta(output.back(),it->v1);
00443     }
00444     output.push_back(it->v1);
00445     t1 = t2;
00446   }
00447   
00448   t1 = theta(output.back(),input.back().v2);
00449   output.push_back(input.back().v2);  
00450   
00451   if (it == input.begin()) return;
00452 
00453   //finding Convex-Hull along the right side bottom-up
00454   do{
00455     it--;
00456     if (it->v2.y == row) continue;
00457     
00458     row = it->v2.y;
00459     t2 = theta(output.back(),it->v2);
00460     
00461     while (t1 >= t2){
00462       output.pop_back();
00463       if (output.size() < 2){ 
00464         t2 = theta(output.front(),it->v2);
00465         break;
00466       }
00467       t1 = theta(output[output.size()-2],output.back());
00468       t2 = theta(output.back(),it->v2);
00469     }
00470 
00471     output.push_back(it->v2);
00472     t1 = t2;
00473   }while(it!=input.begin());
00474 
00475 }
00476 
00477 
00478 /*
00479 * Change log :
00480 * 
00481 * $Log: RasterSpecialist.cpp,v $
00482 * Revision 1.4  2004/09/06 12:02:26  schmidtb
00483 * commented almost all members, removed warnings in documentation
00484 
00485 * did further code clean up
00486 *
00487 * Revision 1.3  2004/09/02 07:59:29  schmidtb
00488 * Added RasterImageProcessor to repository, because we used it for the OpenChallenge.
00489 *
00490 * Revision 1.28  2004/05/25 13:27:34  schmidtb
00491 * modified version of rip for open-challenge
00492 *
00493 * Revision 1.30  2004/05/22 16:01:49  pg_besc
00494 * -modified version of rip for bridge-recognition
00495 *
00496 * Revision 1.29  2004/04/22 16:57:26  pg_besc
00497 * new version of RBallSpecialist2, now omidirectional scans for detection
00498 *
00499 * Revision 1.28  2004/04/20 07:50:27  pg_besc
00500 * new version of pre scan
00501 *
00502 * Revision 1.27  2004/03/25 15:24:55  pg_besc
00503 * made some changes
00504 *
00505 * Revision 1.26  2004/03/11 20:32:58  schmidtb
00506 * new version of rip
00507 *
00508 * Revision 1.25  2004/03/04 09:54:50  schmidtb
00509 * color correction integrated
00510 *
00511 * Revision 1.24  2004/03/03 12:53:21  schmidtb
00512 * color correction integrated
00513 *
00514 * Revision 1.23  2004/02/28 17:16:47  schmidtb
00515 * debugged and made some changes
00516 *
00517 * Revision 1.22  2004/02/26 14:31:32  schmidtb
00518 * comment some outputs
00519 *
00520 * Revision 1.21  2004/02/19 14:22:15  neubach
00521 * - debugged Code of RIP
00522 *
00523 * Revision 1.20  2004/02/18 14:56:19  neubach
00524 * new Segmentation established, code not cleared at all
00525 *
00526 * Revision 1.19  2004/02/05 11:51:18  schmidtb
00527 * now BoxSpecialist recognizes landmarks and goals
00528 *
00529 * Revision 1.18  2004/02/04 13:09:48  schmidtb
00530 * errors removed
00531 *
00532 * Revision 1.17  2004/02/04 13:00:49  schmidtb
00533 * new version of BoxSpecialist
00534 *
00535 * Revision 1.16  2004/02/02 13:42:12  schmidtb
00536 * merged sources of RIP. added som functions.
00537 *
00538 * Revision 1.15  2004/01/31 11:45:02  hyung
00539 * modified enemyValidity-calculation;
00540 * established basical enviroment for TrikotErkennung, based on Arrays and Lists, changes will take affect only #ifdef TrikotErkennung!
00541 *
00542 * Revision 1.14  2004/01/26 22:21:09  schmidtb
00543 * bugfixed BoxSpecialist
00544 *
00545 * Revision 1.13  2004/01/20 10:23:58  schmidtb
00546 * fixed bugs in RGoalSpecialist
00547 *
00548 * Revision 1.12  2004/01/19 16:18:45  schmidtb
00549 * GoalSpecialist recognizes Landmarks
00550 *
00551 * Revision 1.11  2004/01/15 23:12:34  roefer
00552 * Warnings removed
00553 *
00554 * Revision 1.10  2004/01/15 13:45:02  schmidtb
00555 * segmentation established
00556 *
00557 * Revision 1.8  2003/12/30 20:12:03  roefer
00558 * Image size is now 208 x 160. Smaller images are placed in the upper left corner
00559 *
00560 * Revision 1.7  2003/12/15 13:55:32  schmidtb
00561 * Merged and patched new version of RasterImageProcessor.
00562 *
00563 * Revision 1.6  2003/12/08 16:49:49  roefer
00564 * signed/unsigned mismatch corrected
00565 *
00566 * Revision 1.5  2003/12/08 15:02:55  schmidtb
00567 * new version of RIP
00568 *
00569 * Revision 1.4  2003/12/04 09:51:23  schmidtb
00570 * better BallSpecialist
00571 *
00572 * Revision 1.3  2003/11/28 14:50:01  dueffert
00573 * bugs and warnings fixed
00574 *
00575 * Revision 1.2  2003/11/20 10:26:56  schmidtb
00576 * Ball Detection added
00577 *
00578 * Revision 1.1  2003/11/12 13:13:20  schmidtb
00579 * new RasterImageProcessor added
00580 *
00581 *
00582 */

Generated on Thu Sep 23 19:57:30 2004 for GT2004 by doxygen 1.3.6