Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfTVL1Flow.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 /*
26 @inproceedings{Zach:2007:DBA:1771530.1771554,
27  author = {Zach, C. and Pock, T. and Bischof, H.},
28  title = {A duality based approach for realtime TV-L1 optical flow},
29  booktitle = {Proceedings of the 29th DAGM conference on Pattern recognition},
30  year = {2007},
31  isbn = {978-3-540-74933-2},
32  location = {Heidelberg, Germany},
33  pages = {214--223},
34  numpages = {10},
35  url = {http://dl.acm.org/citation.cfm?id=1771530.1771554},
36  acmid = {1771554},
37  publisher = {Springer-Verlag},
38  address = {Berlin, Heidelberg},
39 }
40  */
41 
42 #ifndef CLFTVL1FLOW_HH_
43 #define CLFTVL1FLOW_HH_
44 
45 #include <Base/Common/BIASpragmaStart.hh>
46 #include <OpenCLFramework/clfContext.hh>
47 #include <OpenCLFramework/clfProgram.hh>
48 #include <OpenCLFramework/clfImage2D.hh>
49 #include <OpenCLFramework/Filter/clfFilter.hh>
50 #include <OpenCLFramework/Filter/clfSimpleFilter.hh>
51 #include <OpenCLFramework/Filter/clfColorConversion.hh>
52 
53 namespace BIAS {
54 
55  /**
56  * @brief TVL1 optical flow opencl implementation
57  *
58  * "A duality based approach for realtime TV-L1 optical flow" by Zach et.al. (see file header for bibtex)
59  * Implementation in OpenCL
60  * @author fkellner 08/11
61  */
62  class BIASOpenCLFramework_EXPORT clfTVL1Flow {
63  public:
64  /** @brief constructor, requires valid gpu context
65  * @todo create constructor with parameters for algorithm (defaults are good though)
66  */
67  clfTVL1Flow(clfContext *ctx);
68  virtual ~clfTVL1Flow();
69 
70  void SetNumberOfIterations(unsigned int iternumber);
71  void SetSmoothness(const float& smoothness);
72 
73  /**
74  * @brief call this for every new image
75  * every image must be same size, width and height multiples of 16.
76  * image pyramid will go down until width or height are no multiples of 16,
77  * meaning that a 1024x768 image will be downsampled 4 times (64x48)
78  */
79  void AddImage(BIAS::Image<unsigned char> image);
80  /**
81  * @brief compute optical flow between images
82  * compute optical flow between the last two added images,
83  * does nothing if only one or no images have been added.
84  */
85  void Compute();
86  /**
87  * @brief get flow vectors (direction u,v)
88  * stores flow vectors with direction (u,v) in images first 2 color channels.
89  * note that the resulting image has 4 channels!
90  */
91  void GetUVW(BIAS::Image<float> &image);
92  /**
93  * @brief get flow vectors in color coding
94  * converts the flow vectors to colors, where direction represents a color
95  * in a hsv hue circle. length of the vectors scales saturation and value.
96  * note that the resulting image has 4 channels!
97  */
98  void GetColorCoded(BIAS::Image<float> &image);
99 
100  protected:
101  // creates image pyramid by downscaling by 2 vector.size times
102  void CreatePyramid_(std::vector<clfImage2D *> &pyramid);
103  // upsample by 2, scaling the pixel values by parameter scale
104  void Upsample_(clfImage2D *src, clfImage2D *dst, float scale=1.0f);
105  // shorthand for creating a new image and clear to zero
106  clfImage2D *newImage_(unsigned int w, unsigned int h, bool clear = false);
107  // warps the second image according to already computed (u,v)s
108  // will also fill the gradient images
109  void Warp_(unsigned int level);
110  // computation of flow on a pyramid level
111  void ComputeFlow_(unsigned int level);
112  private:
113  clfContext *context_;
114  clfProgram *programCL_;
115 
116  // color conversion, filters and scalers
117  clfColorConversion *colorConvCL_;
118  clfSimpleFilter *downsampleCL_;
119  clfSimpleFilter *upsampleCL_;
120  clfFilter<float, float> *clearFilter_;
121  // input images
122  clfImage2D *image1_, *image2_;
123  // only needed for creating debug images
124  clfImage2D *debug_;
125 
126  // reusable pyramids of all the different intermediate results
127  std::vector<clfImage2D *> uvw_, uvwX_, uvwY_, uvw0_, uvwOld_;
128  std::vector<clfImage2D *> image2warpedt_, imageDiv_, P1_, P2_, imageCopy_, imageCopy2_;
129  std::vector<clfImage2D *> Ixyg_;
130  std::vector<clfImage2D *> pyramid1_;
131  std::vector<clfImage2D *> pyramid2_;
132 
133  // parameters for algorithm
134  int maxComputeUnits_;
135  int numIm_;
136  float sigma_, tau_, gamma_, lambda_;
137  unsigned int warps_;
138  unsigned int maxIterations_;
139  unsigned int levels_;
140  };
141 
142 } /* namespace BIAS */
143 #include <Base/Common/BIASpragmaEnd.hh>
144 #endif /* CLFTVL1FLOW_HH_ */
TVL1 optical flow opencl implementation.
Definition: clfTVL1Flow.hh:62
OpenCL Program wrapper.
Definition: clfProgram.hh:53
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
OpenCL Context wrapper.
Definition: clfContext.hh:49