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

Modules/ImageProcessor/GT2005ImageProcessor/GT2005BallSpecialist.h

Go to the documentation of this file.
00001 /**
00002 * @file GT2005BallSpecialist.h
00003 * 
00004 * This file contains a class for Image Processing.
00005 * @author <A href=mailto:juengel@informatik.hu-berlin.de>Matthias Juengel</A>
00006 * @author Max Risler
00007 * @author <A href=mailto:brunn@sim.tu-darmstadt.de>Ronnie Brunn</A>
00008 * @author <A href=mailto:mkunz@sim.tu-darmstadt.de>Michael Kunz</A>
00009 * @author <a href="mailto:walter.nistico@uni-dortmund.de">Walter Nistico</a>
00010 */
00011 
00012 #ifndef __GT2005BallSpecialist_h_
00013 #define __GT2005BallSpecialist_h_
00014 
00015 #include "Tools/Math/Geometry.h"
00016 #include "Tools/Debugging/DebugImages.h"
00017 #include "Representations/Perception/ColorTable.h"
00018 #include "Representations/Perception/Image.h"
00019 #include "Representations/Perception/CameraMatrix.h"
00020 #include "Representations/Perception/BallPercept.h"
00021 #include "Representations/Perception/MultipleBallPerceptElement.h"
00022 #include "Modules/ImageProcessor/ImageProcessorTools/ColorCorrector.h"
00023 #include "Modules/ImageProcessor/ImageProcessorTools/ColorModelConversions.h"
00024 #include "Modules/ImageProcessor/ImageProcessorTools/BresenhamLineScan.h"
00025 #include "Tools/Math/Common.h"
00026 #include "Tools/List.h"
00027 
00028 
00029 /**
00030 The BallSpecialist finds a single ball in an image.
00031 */
00032 class GT2005BallSpecialist  
00033 {
00034 public:
00035   GT2005BallSpecialist(const ColorCorrector& colorCorrector);
00036   
00037   /** 
00038   * Searches for the ball in the image, starting from the secified point. 
00039   */
00040   void searchBall
00041   (
00042     const Image& image,
00043     const ColorTable& colorTable,
00044     const CameraMatrix& cameraMatrix,
00045     const CameraMatrix& prevCameraMatrix,
00046     int x, int y,
00047     BallPercept& ballPercept
00048   );
00049   
00050   /**
00051   * Returns the Similarity of the given color to orange.
00052   */
00053   unsigned char getSimilarityToOrange (unsigned char y, unsigned char u, unsigned char v)
00054   {
00055 /*      double r = y + 1.140 * (u - 128);
00056       double g = y - 0.394 * (v - 128) - 0.581 * (u - 128);
00057       double b = y + 2.028 * (v - 128);
00058       if(r < 0) r = 0; if(r > 255) r = 255;
00059       if(g < 0) g = 0; if(g > 255) g = 255;
00060       if(b < 0) b = 0; if(b > 255) b = 255;*/
00061     unsigned char r, g, b;
00062     ColorModelConversions::fromYCbCrToRGB(y, u, v, r, g, b);
00063 
00064 //    return (unsigned char) min(max(r-2*b-abs(2*g-r),0),255);
00065     return (unsigned char) min(max(r - 2*b - 3*max((g-r/2),0) - (3*max((r/2-g),0))/2 ,0),255);
00066   }
00067 
00068   void resetMultiplePerceptsList(BallPercept& ballPercept);  
00069   //List that contains the multiple percepts ordered by reliability
00070     List<MultipleBallPerceptElement> multiplePercepts;
00071     void forwardPercept(BallPercept& ballPercept);
00072 
00073 
00074 private:
00075   /**
00076   * The class represents a ball point.
00077   */
00078   class BallPoint : public Vector2<int>
00079   {
00080   public:
00081     BallPoint() : atBorder(false), greenIsClose(true), yellowIsClose(false), hardEdge(true){};
00082     bool greenIsClose;
00083     bool yellowIsClose;
00084     bool hardEdge;
00085     bool atBorder;
00086 
00087     BallPoint& operator=(const Vector2<int>& other)
00088     {
00089       x = other.x;
00090       y = other.y;
00091 
00092       return *this;
00093     }
00094   };
00095 
00096   class BallPointList
00097   {
00098   public:
00099     BallPointList() : number(0) {};
00100 
00101     enum {maxNumberOfPoints = 400}; /**< The maximum number of ball points. */
00102     BallPoint ballPoints[maxNumberOfPoints]; /**< The ball points. */
00103     int number; /**< The actual number of ball points. */
00104 
00105     void add(const BallPoint& ballPoint);
00106 
00107     BallPoint& operator[] (int i) { return ballPoints[i]; }
00108     const BallPoint& operator[] (int i) const { return ballPoints[i]; }
00109   };
00110 
00111   /** Scan for the ball starting at a given trigger point */
00112   void scanForBallPoints
00113   (
00114    const Image& image,
00115    const CameraInfo& bwCameraInfo,
00116    const ColorTable& colorTable,
00117    int x, int y,
00118    BallPointList& ballPoints,
00119    int& countAmbiguous,
00120    int& countOrange,
00121    int& maxOrangePerLine,
00122    int& countPixel
00123   );
00124 
00125   /** Finds the end of the ball */
00126   bool findEndOfBall(
00127     const Image& image,
00128     const CameraInfo& bwCameraInfo,
00129     const ColorTable& colorTable,
00130     const BallPoint& start,
00131     const Vector2<int>& step,
00132     BallPoint& destination,
00133     int& countAmbiguous,
00134     int& countOrange,
00135     int& maxOrangePerLine,
00136     int& countPixel
00137     );
00138 
00139   /**
00140   * The function tries to calculate the ball percept by using the Levenberg-Marquardt
00141   * algorithm. The function fails if less than 3 points are available.
00142   * @return true if ball percept was created
00143   */
00144   bool createBallPerceptLevenbergMarquardt
00145   (
00146     const BallPointList& ballPoints,
00147     Vector2<int>& center,
00148     double& radius
00149   );
00150 
00151   /*
00152   * Check if a list of points is inside a given circle
00153   */
00154   bool checkIfPointsAreInsideBall(const BallPointList& ballPoints, Vector2<int>& center, double radius);
00155 
00156   /*
00157   * Calculate the deviation of the points from the circle. (Only needed if checkIfPointsAreInsideBall(...) wasn't called first
00158   */
00159   void calculateDeviationOfBallPoints(const BallPointList& ballPoints, Vector2<int>& center, double radius);
00160 
00161   /**
00162   * The function checks whether a ball percept is plausible and will add it if so.
00163   * @param image The image the ballpercept comes from.
00164   * @param bwCameraInfo Object containing camera parameters.
00165   * @param colorTable The colortable to be used
00166   * @param cameraMatrix The matrix of the camera of the image.
00167   * @param prevCameraMatrix The matrix of the camera of a previous image.
00168   * @param center The center of the ball.
00169   * @param radius The radius of the ball.
00170   * @param ballPercept The object the ball is added to.
00171   */
00172   void addBallPercept
00173   (
00174     const Image& image,
00175     const CameraInfo& bwCameraInfo,
00176     const ColorTable& colorTable,
00177     const CameraMatrix& cameraMatrix,
00178     const CameraMatrix& prevCameraMatrix,
00179     const Vector2<int>& center,
00180     double radius,
00181     BallPercept& ballPercept
00182   );  
00183 
00184     /**
00185     * The function calculates the factor durabilityOfBallPoints that will be needed later to calculate a reliability for the percept.
00186   */
00187     void considerBallPoints(const BallPointList &ballPoints);
00188 
00189   /**
00190     * The function calculates the reliability of the ball percept.
00191   */
00192   double calculateReliability(double percentOfOrange, double radius,  Vector2<double> anglesToCenter, const CameraInfo bwCameraInfo,
00193                 const CameraMatrix& cameraMatrix, const CameraMatrix& prevCameraMatrix);
00194 
00195   const ColorCorrector& colorCorrector;
00196   
00197   /* variables needed to calculate the reliability of a ball percept*/
00198     double deviationOfBallPoints; // the average deviation of non-border ballpoints from the calculated circle
00199   double durabilityOfBallPoints; // 0..1; factor considering the relation of nearGreen points, nearYellow points and hardEdge points to the number of non-border points
00200 
00201     
00202     // adds a new element to the list
00203     void addMultiplePercept(const MultipleBallPerceptElement& newPercept);
00204 
00205     
00206 };
00207 
00208 #endif// __BallSpecialist_h_

Generated on Mon Mar 20 21:59:45 2006 for GT2005 by doxygen 1.3.6