Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

OpticalFlow.h

Go to the documentation of this file.
00001 #ifndef OPTICAL_FLOW_H
00002 #define OPTICAL_FLOW_H
00003 
00004 #include "Shared/fmat.h"
00005 #include "DualCoding/ShapeLine.h"
00006 
00007 #include "RawImagePyramid.h"
00008 
00009 using namespace std;
00010 
00011 class ScorePoint {
00012 public:
00013   ScorePoint() : position(fmat::pack(0,0)), score(0.0f) {}
00014   
00015   fmat::Column<2> position;
00016   float score;
00017 };
00018 
00019 class FlowVector {
00020 
00021 public:
00022   FlowVector() : position(fmat::pack(0,0)), flow(fmat::pack(0,0)), relevanceScore(0) {}
00023 
00024   fmat::Column<2> position;
00025   fmat::Column<2> flow;
00026   
00027   float relevanceScore;
00028 };
00029 
00030 class OpticalFlow {
00031 
00032 public:
00033 
00034   static const unsigned int SCORE_LAYER = 0; //!< Image layer from which we select features to track
00035 
00036   static const unsigned int NUM_FLOW_VECTORS = 50; //!< Number of flow vectors to calculate
00037 
00038   //! Size of width/2, height/2 of the feature when we calculate flow.
00039   /*! The larger this is, the better the tracking.  Large values will severely decrease framerate, however.
00040    */
00041   static const unsigned int FLOW_WINDOW = 2;
00042 
00043   //! Size of width/2, height/2 of the feature when we select them for flow calculation.
00044   /*! It is typically safe to keep this low to save computation time.
00045    */
00046   static const unsigned int SCORE_WINDOW = 1;
00047 
00048   //! Number of frames we remember about the history of the robot's motion.  Used for removing outliers.
00049   static const unsigned int NUM_FRAMES = 10;
00050 
00051   //! Number of pixels per grid square when calculating minimum eigenvalues.
00052   static const unsigned int CANDIDATE_FEATURE_RESOLUTION = 30;
00053 
00054   OpticalFlow();
00055   OpticalFlow(OpticalFlow &other);
00056 
00057   virtual ~OpticalFlow() {};
00058 
00059   //! Comparison function to sort the flow vectors by horizontal translation.
00060   static bool myFlowComp(const FlowVector &v1, const FlowVector &v2);
00061 
00062   //! Comparison function in order to sort the flow vectors by relevance score.
00063   static bool myFlowComp2(const FlowVector &v1, const FlowVector &v2);
00064 
00065   static bool scoreComp(const ScorePoint &p1, const ScorePoint &p2) {
00066     return p2.score < p1.score;
00067   }
00068 
00069   //! Update the current flow vectors and increment the integrated angle.
00070   void updateFlow();
00071 
00072   //! Draw the optical flow vectors in the camera shape space.
00073   void drawFlow();
00074 
00075   void swapPyramids();
00076 
00077   //! Select the features that we are going to track.
00078   /*! The features are scored by the magnitude of their minimum eigenvalue.
00079       Then, the features wpith the largest score are selected.
00080    */
00081   void initializePositions();
00082 
00083   float getIntegratedFlow() { return integratedFlow; }
00084 
00085   //! Compute scores for vectors to determine outliers.
00086   /*! The lower the score, the better.  Uses the idea that the robot's motion is continuous.
00087       Gives high score to vectors that are far away from the robot's history of motion.
00088    */
00089   void computeRelevanceScores();
00090 
00091   //! Lucas-Kanade optic flow algorithm
00092   static fmat::Column<2> 
00093   iterativeLucasKanade(fmat::Column<2> center, fmat::Column<2> trans,
00094            RawImage& img1, RawImage& img2, int window);
00095 
00096 protected:
00097   float integratedFlow;
00098 
00099 private:
00100   std::vector<FlowVector> flowVectors;
00101   std::vector<DualCoding::Shape<DualCoding::LineData> > flowVectorVisuals;
00102   std::vector<float> pastTranslations;
00103 
00104   OpticalFlow &  operator=(const OpticalFlow &other);
00105 
00106   RawImagePyramid pyramid1;
00107   RawImagePyramid pyramid2;
00108   RawImagePyramid *currPyramid;
00109   RawImagePyramid *prevPyramid;
00110 };
00111 
00112 /**class OpticalFlowOdometry : public OpticalFlow {
00113 public:
00114   OpticalFlowOdometry() : scalingFactor(ANGULAR_RESOLUTION), lastAngle(0.0f), OpticalFlow() {}
00115 
00116   void update();
00117   float getAngle() { return lastAngle; }
00118   float getIntegratedAngle() { return integratedFlow / scalingFactor; }
00119 private:
00120   float scalingFactor;
00121   float lastAngle;
00122   };*/
00123 
00124 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:45 2016 by Doxygen 1.6.3