00001
00002
00003
00004
00005
00006
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
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
00096
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
00105 it->segment = segCount;
00106 segs[segCount] = segCount;
00107 segCount++;
00108 if (segCount>maxSegments-1)
00109 return false;
00110 }
00111
00112 }
00113
00114
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
00125 for(it = lines.begin();it != lines.end();++it)
00126 segments[finalSegs[it->segment]].push_back(*it);
00127
00128
00129
00130
00131
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
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
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
00177
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
00186 it->segment = segCount;
00187 segs[segCount] = segCount;
00188 segCount++;
00189 if (segCount>maxSegments-1)
00190 return false;
00191 }
00192
00193 }
00194
00195
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
00206 for(it = lines.begin();it != lines.end();++it)
00207 segments[finalSegs[it->segment]].push_back(*it);
00208
00209
00210
00211
00212
00213 return true;
00214 }
00215
00216
00217
00218 void RasterSpecialist::createSegmentsFromLines(std::list<LinePair>& lines,
00219 std::vector<std::list<LinePair> >& segments,int xSpace, int ySpace){
00220
00221 OUTPUT(idText,text,"size before "<< lines.size());
00222 if (lines.size() == 0) return;
00223 int allowedSpace = ySpace;
00224 segList.clear();
00225
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
00237
00238
00239 for(++lit;lit!=lines.end();++lit){
00240
00241 LinePair& current = *lit;
00242
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
00253
00254 if (current.v1.y - compare.v1.y > allowedSpace){
00255
00256 segments.push_back(*currentSeg);
00257
00258 sit2 = sit;
00259 sit++;
00260 segList.erase(sit2);
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
00273
00274 do{
00275 it--;
00276 compare = *it;
00277
00278 if(linePairsFit(current,compare,xSpace)){
00279 if(!lead){
00280
00281 currentSeg->push_back(current);
00282 lead = currentSeg;
00283 break;
00284 }
00285 else{
00286
00287 lead->merge(*currentSeg);
00288 sit2 = sit;
00289 sit++;
00290 segList.erase(sit2);
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
00304 if (!lead){
00305 segList.push_back(list<LinePair>());
00306 segList.back().push_back(current);
00307 }
00308
00309
00310 }
00311
00312
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
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 do{
00363 rit--;
00364 if (!linesFit(*it,*rit,xSpace)) continue;
00365 if (lead == -1){
00366
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
00376
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
00385 it->segment = segCount;
00386 segs[segCount] = segCount;
00387 segCount++;
00388 if (segCount>maxSegments-1)
00389 return false;
00390 }
00391
00392 }
00393
00394
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
00405 for(it = lines.begin();it != lines.end();++it)
00406 segments[finalSegs[it->segment]].push_back(*it);
00407
00408
00409 OUTPUT(idText,text,"found segments "<< segments.size());
00410
00411
00412 return true;
00413 }
00414
00415
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
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
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
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
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
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582