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

# Modules/ImageProcessor/VLCImageProcessor/VLCClustering.cpp

Go to the documentation of this file.

    00001 #include "VLCClustering.h"
    00002
    00003 VLCClustering::VLCClustering()
    00004 {
    00005   cl = VLCClusterliste();
    00006
    00007   vhshort = 8;
    00008   vhmiddle = 8;
    00009   vhlong = 16;
    00010
    00011   yshort = 0;
    00012   ymiddle = 80;
    00013   ylong = 110;
    00014
    00015   hv = 20;
    00016
    00017   vh = 4;
    00018
    00019   lImage = 208;
    00020   hImage = 160;
    00021
    00022 }
    00023
    00024 //a method to add a new scan-line. if the scan-line is positioned next to existing clusters, the scan-line and these
    00025 //clusters will be merged to one cluster.
    00026 //s is the start-point of the scan-line and e the end-point. l is the length of the run measured in pixel.
    00027 void VLCClustering::addLine(Vector2<int> startInt, Vector2<int> endInt, int length )
    00028 {
    00029   Vector2<double> start(startInt.x,startInt.y);
    00030   Vector2<double> end(endInt.x,endInt.y);
    00031
    00032   //check the direction of the run(vertical or horizontal)
    00033   //fabs() is the the method abs() vor double
    00034   if(fabs(start.x - end.x) < fabs(start.y - end.y))
    00035     vertical = true;
    00036   else
    00037     vertical = false;
    00038
    00039   //determine xmin and xmax, ymin and ymax, which are the borders of the bounding box of the
    00040   //lighty diagonal scan-line
    00041   if(start.x <= end.x)
    00042   {
    00043     xmin = start.x;
    00044     xmax = end.x;
    00045   }
    00046   else
    00047   {
    00048     xmin = end.x;
    00049     xmax = start.x;
    00050   }
    00051   if(start.y <= end.y)
    00052   {
    00053     ymin = start.y;
    00054     ymax = end.y;
    00055   }
    00056   else
    00057   {
    00058     ymin = end.y;
    00059     ymax = start.y;
    00060   }
    00061
    00062
    00063   //clusterlist empty?
    00064   if(cl.getEmpty())
    00065   {
    00066     VLCCluster firstCluster = VLCCluster(xmin,xmax,ymin,ymax);
    00067     vhIter = cl.getVNaviBegin();
    00068     cl.addCluster(firstCluster, vhIter, true);
    00069   }
    00070   else //clusterlist is not empty
    00071   //integrates the scan-line into the clusterlist
    00072   Cluster1(vertical);
    00073
    00074 }
    00075
    00076
    00077
    00078 void VLCClustering::Cluster1(bool v)
    00079 {
    00080   //vertical or horizontal scan-line?
    00081   if(v)
    00082   {
    00083     prepV(xmin); //prepare for vertical scanning
    00084
    00085     //case1 or case2 ?
    00086     //case1: there are no other clusters that are positioned between xmin and xmax
    00087     //    only horizontal neighbours will be checked
    00088     //case2: there are clusters that are positioned between xmin and xmax
    00089     //    horizontal and vertical neighbours will be checked
    00090
    00091     //case2:
    00092     if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getXMin() > xmax))))
    00093       markV(xmin, xmax, ymin, ymax, vh);  //check vertical neighbours
    00094     else
    00095     {
    00096       VLCCluster scanline = VLCCluster(xmin, xmax, ymin, ymax);
    00097       cIter = *vhIter;
    00098       if((vhIter != vhIter2) && (cIter->getXMin() <= xmax) && (xmin > cIter->getXMin()))
    00099         ++vhIter;
    00100       cl.addCluster(scanline, vhIter, true);
    00101     }
    00102
    00103     //case1 and second part of case2: checking for horizontal neighbours:
    00104     //the whole scan-line is positioned in the area of the long scan-lines, so vhlong is used
    00105     //for the modified/bounding box
    00106     if(ymin >= ylong)
    00107     {
    00108       //modification of the bounding box (vhlong)
    00109       prepH(ymin);
    00110       if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00111         markH(xmin, xmax, ymin, ymax, vhlong);
    00112
    00113     }
    00114     //case the the beginning of the scan-line is between ymiddle and ylong (itīs not in between ylong and
    00115     //end of scan-line, because that case is already catched in the case before this one: ymin >= ylong)
    00116     else  if((ymin >= ymiddle) && (ymin < ylong))
    00117     {
    00118       //check, if the end-point of the scan-line is in the area of the long scan-lines or the middle scan-lines
    00119       //case ymax is between ymiddle and ylong,too:
    00120       if(ymax < ylong)
    00121       {
    00122         //modification of the bounding box
    00123         prepH(ymin);
    00124         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00125           markH(xmin, xmax, ymin, ymax, vhmiddle);
    00126       }
    00127       else //ymax is between ylong and end of the image
    00128       {
    00129         //in this case, there will be two bounding boxes of different sizes
    00130         //modified bounding box1
    00131         prepH(ymin);
    00132         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ylong - 1))))
    00133           markH(xmin, xmax, ymin, ylong - 1, vhmiddle);
    00134
    00135         //modified bounding box2
    00136         prepH(ylong - 1);
    00137         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00138           markH(xmin, xmax, ylong - 1, ymax, vhlong);
    00139       }//end of else (if(ymax < ylong))
    00140     }//end of else  if(ymin >= ymiddle)
    00141
    00142     else if((ymin >= yshort) && (ymin < ymiddle))
    00143     {
    00144       //ymin is positioned between yshort and ymiddle
    00145       //check, in which area ymax is at:
    00146       if(ymax < ymiddle) //one bounding box
    00147       {
    00148         //modified bounding box
    00149         prepH(ymin);
    00150         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00151           markH(xmin, xmax, ymin, ymax, vhshort);
    00152       }
    00153       else if((ylong > ymax) && (ymax >= ymiddle)) //two bounding boxes
    00154       {
    00155         //first modified bounding box
    00156         prepH(ymin);
    00157         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymiddle-1))))
    00158           markH(xmin, xmax, ymin, ymiddle - 1, vhshort);
    00159
    00160         //second bounding box
    00161         prepH(ymiddle -1);
    00162         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00163           markH(xmin, xmax, ymiddle - 1, ymax, vhmiddle);
    00164       }
    00165       else if(ymax >= ylong) //ymax >= ylong, three bounding boxes
    00166       {
    00167         //first bounding box
    00168         prepH(ymin);
    00169         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymiddle - 1))))
    00170           markH(xmin, xmax, ymin, ymiddle - 1, vhshort);
    00171
    00172         //second bounding box
    00173         prepH(ymiddle - 1);
    00174         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ylong - 1))))
    00175           markH(xmin, xmax, ymiddle - 1, ylong - 1, vhmiddle);
    00176
    00177         //third bounding box
    00178         prepH(ylong - 1);
    00179         if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00180           markH(xmin, xmax, ylong - 1, ymax, vhlong);
    00181       }//end of else if(ymax >= ylong) //ymax >= ylong, three bounding boxes
    00182
    00183     }//end of else ymin >= yshort
    00184   }//end of if(v)
    00185
    00186   else //scan horizontal
    00187   {
    00188     prepH(ymin);
    00189     if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
    00190       markH(xmin,xmax,ymin,ymax,vh);
    00191     else
    00192       {
    00193         VLCCluster scanline = VLCCluster(xmin, xmax, ymin, ymax);
    00194         cIter = *vhIter;
    00195         if((vhIter != vhIter2) && (cIter->getYMin() <= ymax) && (ymin > cIter->getYMin()))
    00196           ++vhIter;
    00197         cl.addCluster(scanline, vhIter, false);
    00198       }
    00199     //case 1 and second part of case 2
    00200     //checking vertical neighbours
    00201
    00202     //modify bounding box, to check the vertical neighbours.
    00203     prepV(xmin);
    00204     if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getXMin() > xmax))))
    00205       markV(xmin, xmax, ymin, ymax,hv );
    00206   }//end of else (if(v))
    00207
    00208 }
    00209
    00210
    00211 //prepare for vertical scanning: choose vNavi, set the iterators and skip not interessting clusters
    00212 //o means: use the bounding box of the scan-line: xmin,xmax,ymin,ymax
    00213 //otherwise use the modified bounding box, xn,xx,yn,yx
    00214
    00215 void VLCClustering::prepV(double xminimum)
    00216 {
    00217     //choose the list vNavi, which stores the navigation-information for vertical scanning
    00218     vhIter = cl.getVNaviBegin(); //begin of the navigation-information list
    00219     vhIter2 = cl.getVNaviEnd();
    00220     cIter = *vhIter; //pointer to a cluster in clist
    00221
    00222     //skip clusters that are not interessting for the clustering in vertical-direction.
    00223     while((vhIter != vhIter2) && (cIter->getXMax() < xminimum))
    00224     {
    00225       ++vhIter;
    00226       if(vhIter != vhIter2)
    00227         cIter = *vhIter;
    00228     }
    00229
    00230 }
    00231
    00232 //prepare for horizontal scanning: choose hNavi, set the iterators and skip not interessting clusters
    00233 //o means: use the bounding box of the scan-line: xmin,xmax,ymin,ymax
    00234 //otherwise use the modified bounding box, xn,xx,yn,yx
    00235 void VLCClustering::prepH(double yminimum)
    00236 {
    00237   //choose the list hNavi, which stores the navigation-information for horizontal scanning
    00238   vhIter = cl.getHNaviBegin(); //begin of the navigation-information list
    00239   vhIter2 = cl.getHNaviEnd(); //end of the navigation-information list
    00240   cIter = *vhIter; //pointer to a cluster in clist
    00241
    00242   //skip clusters that are not interessting for the clustering in horizontal-direction.
    00243   while((vhIter != vhIter2) && (cIter->getYMax() < yminimum))
    00244   {
    00245     ++vhIter;
    00246     if(vhIter != vhIter2)
    00247       cIter = *vhIter;
    00248   }
    00249 }
    00250
    00251
    00252
    00253
    00254 void VLCClustering::markV(double xminimum, double xmaximum, double yminimum, double ymaximum, int expansion )
    00255 {
    00256   //initialize the iterator to mark the range of candidate at the beginning of every marking and clustering
    00257   //process
    00258   cfirst = NULL;
    00259   clast = NULL;
    00260   //in case that there are no other clusters, which will be taken into account, too,
    00261   cinput = vhIter;
    00262
    00263
    00264   cIter = *vhIter;
    00265   //if this constrain is not satisfied, the cluster already skipped the range xn-xx/xmin-xmax
    00266   while((vhIter != vhIter2) && (cIter->getXMin() <= xmaximum))
    00267   {
    00268     //if this constrain is not satisfied, the cluster is positioned before the range xn-xx
    00269     if(cIter->getXMax() >= xminimum)
    00270     {
    00271       //candidates
    00272       //vd is the distance between clusters in vertical scanning
    00273       if((cIter->getYMax() >= (yminimum - expansion)) && (cIter->getYMin() <= (ymaximum + expansion)))
    00274       {
    00275         if(cfirst == NULL)
    00276           cfirst = vhIter;
    00277         clast = vhIter;
    00278         cIter->setCandidate(true);
    00279       }
    00280     }// end of if(cIter->getXMax() >= xminimum)
    00281     ++vhIter;
    00282     if(vhIter != vhIter2)
    00283       cIter = *vhIter;
    00284   }//end of while((vhIter != vhIter2) && (cIter->getXMin() <= xmaximum))
    00285
    00286   if(cfirst != NULL)
    00287   {
    00288     clusterFinal(true, xminimum,xmaximum,yminimum,ymaximum);
    00289   }
    00290   else
    00291   {
    00292     VLCCluster scanline = VLCCluster(xminimum, xmaximum, yminimum, ymaximum);
    00293     cIter = *cinput;
    00294     if((cinput != vhIter2) && (cIter->getXMin() <= xmaximum) && (xminimum > cIter->getXMin()))
    00295       ++cinput;
    00296     cl.addCluster(scanline, cinput, true);
    00297   }
    00298 }
    00299
    00300
    00301
    00302 void VLCClustering::markH(double xminimum, double xmaximum, double yminimum, double ymaximum, int expansion)
    00303 {
    00304   //initialize the iterator to mark the range of candidate at the beginning of every marking and clustering
    00305   //process
    00306   cfirst = NULL;
    00307   clast = NULL;
    00308   cinput = vhIter;
    00309
    00310   cIter = *vhIter;
    00311   //if this constrain is not satisfied, the cluster already skipped the range yn-yx
    00312   while((vhIter != vhIter2) && (cIter->getYMin() <= ymaximum))
    00313   {
    00314     //if this constrain is not satisfied, the cluster is positioned before the range yn-yx
    00315     if(cIter->getYMax() >= yminimum)
    00316     {
    00317       if((cIter->getXMin() <= (xmaximum + expansion)) && (cIter->getXMax() >= (xminimum - expansion)))
    00318       {
    00319         if(cfirst == NULL)
    00320           cfirst = vhIter;
    00321         clast = vhIter;
    00322         cIter->setCandidate(true);
    00323       }//end of if((cIter->getXMin() <= xmaximum) && (cIter->getXMax() >= xminimum))
    00324
    00325     }//end of if(cIter->getYMax() >= yminimum)
    00326     ++vhIter;
    00327     if(vhIter != vhIter2)
    00328       cIter = *vhIter;
    00329   }//end of while((vhIter != vhIter2) && (cIter->getYMin() <= ymaximum))
    00330
    00331   if(cfirst != NULL)
    00332   {
    00333     clusterFinal(false, xminimum,xmaximum,yminimum,ymaximum);
    00334   }
    00335   else
    00336   {
    00337     VLCCluster scanline = VLCCluster(xminimum, xmaximum, yminimum, ymaximum);
    00338     cIter = *cinput;
    00339     if((cinput != vhIter2) && (cIter->getYMin() <= ymaximum) && (yminimum > cIter->getYMin()))
    00340       ++cinput;
    00341     cl.addCluster(scanline, cinput, false);
    00342   }
    00343
    00344 }
    00345
    00346 void VLCClustering::clusterFinal(bool v, double xminimum, double xmaximum, double yminimum, double ymaximum)
    00347 {
    00348   //borders of the new cluster
    00349   double newXMIN;
    00350   double newXMAX;
    00351   double newYMIN;
    00352   double newYMAX;
    00353
    00354   cIter = *cfirst;
    00355   if(xminimum < cIter->getXMin())
    00356     newXMIN = xminimum;
    00357   else
    00358     newXMIN = cIter->getXMin();
    00359   if(xmaximum > cIter->getXMax())
    00360     newXMAX = xmaximum;
    00361   else
    00362     newXMAX = cIter->getXMax();
    00363   if(yminimum < cIter->getYMin())
    00364     newYMIN = yminimum;
    00365   else
    00366     newYMIN = cIter->getYMin();
    00367   if(ymaximum > cIter->getYMax())
    00368     newYMAX = ymaximum;
    00369   else
    00370     newYMAX = cIter->getYMax();
    00371
    00372
    00373   //define position for the new cluster:
    00374   if(v)
    00375   {
    00376     if(cfirst == cl.getVNaviBegin())
    00377       cinput = NULL;
    00378     else
    00379     {
    00380       cinput = --cfirst;
    00381       ++cfirst;
    00382     }
    00383   }//end of if(v)
    00384   else
    00385   {
    00386     if(cfirst == cl.getHNaviBegin())
    00387       cinput = NULL;
    00388     else
    00389     {
    00390       cinput = --cfirst;
    00391       ++cfirst;
    00392     }
    00393   }
    00394
    00395   ++cfirst; // cfirst already was taken into account
    00396   ++clast;//in order to include clast
    00397   for(vhIter = cfirst; vhIter != clast; ++vhIter)
    00398   {
    00399     cl.removeCluster(cIter);
    00400     cIter = *vhIter;
    00401     if(cIter->getCandidate() == true)
    00402     {
    00403       if(cIter->getXMin() < newXMIN)
    00404         newXMIN = cIter->getXMin();
    00405       if(cIter->getXMax() > newXMAX)
    00406         newXMAX = cIter->getXMax();
    00407       if(cIter->getYMin() < newYMIN)
    00408         newYMIN = cIter->getYMin();
    00409       if(cIter->getYMax() > newYMAX)
    00410         newYMAX = cIter->getYMax();
    00411     }
    00412   }
    00413   cl.removeCluster(cIter);
    00414
    00415   //build cluster and add it to the clusterlist:
    00416   VLCCluster newCluster = VLCCluster(newXMIN, newXMAX, newYMIN, newYMAX);
    00417   if(cinput == NULL)
    00418   {
    00419     if(v)
    00420       cinput = cl.getVNaviBegin();
    00421     else
    00422       cinput = cl.getHNaviBegin();
    00423   }
    00424   else
    00425     ++cinput;
    00426   cl.addCluster(newCluster, cinput, v);
    00427
    00428 }
    00429
    00430 int VLCClustering::getSize()
    00431 {
    00432   hlist.clear();
    00433   hlist = cl.getMidpoints();
    00434   return(hlist.size());
    00435 }
    00436
    00437
    00438 Vector2<double> VLCClustering::getMidpoint(int e)
    00439 {
    00440   hlist.clear();
    00441   hlist = cl.getMidpoints();
    00442
    00443   hiterator = hlist.begin();
    00444   for(int i = 0; i < e; ++i)
    00445   {
    00446     ++hiterator;
    00447   }
    00448   return (*hiterator);
    00449 }
    00450
    00451 Vector2<double> VLCClustering::getMidpointX(int e)
    00452 {
    00453   vhIter = cl.getVNaviBegin();
    00454   for(int i = 0; i < e; ++i)
    00455   {
    00456     ++vhIter;
    00457   }
    00458   cIter = *vhIter;
    00459   return(cIter->getMidpoint());
    00460 }
    00461
    00462 Vector2<double> VLCClustering::getMidpointY(int e)
    00463 {
    00464   vhIter = cl.getHNaviBegin();
    00465   for(int i = 0; i < e; ++i)
    00466   {
    00467     ++vhIter;
    00468   }
    00469   cIter = *vhIter;
    00470   return(cIter->getMidpoint());
    00471 }
    00472
    00473 VLCCluster VLCClustering::getClusterX(int e)
    00474 {
    00475   vhIter = cl.getVNaviBegin();
    00476   for(int i = 0; i < e; ++i)
    00477   {
    00478     ++vhIter;
    00479   }
    00480   cIter = *vhIter;
    00481   return(*cIter);
    00482 }
    00483
    00484 VLCCluster VLCClustering::getClusterY(int e)
    00485 {
    00486   vhIter = cl.getHNaviBegin();
    00487   for(int i = 0; i < e; ++i)
    00488   {
    00489     ++vhIter;
    00490   }
    00491   cIter = *vhIter;
    00492   return(*cIter);
    00493 }
    00494
    00495 void VLCClustering::reset()
    00496 {
    00497   cl.deleteAll();
    00498 }
    00499
    00500