Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PyramidImage.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 #ifndef __PyramidImage_hh__
22 #define __PyramidImage_hh__
23 
24 #include <Base/Common/BIASpragmaStart.hh>
25 
26 #include <Base/Common/SharedPtr.hh>
27 #include <Base/Image/Image.hh>
28 #include <Filter/Rescale.hh>
29 #include <Image/PyramidImageInterface.hh>
30 
31 #include <float.h>
32 #include <vector>
33 
34 // for GetSingleImage
35 // border between pyramid level and fill color
36 #define PYRAMID_BORDER 2
37 #define PYRAMID_BACKGROUND 0
38 
39 #define D_PYRAMID_CREATE 0x00000001
40 #define D_PYRAMID_SINGLE_IM 0x00000002
41 
42 namespace BIAS
43 {
44  /** @class PyramidImage
45  @ingroup g_image
46  @brief Class for holding multiple downsampled images
47 
48  Represent a pyramid image, i.e. multiple downsampled images.
49  The image with index 0 has the original size, while each (i+1)-th image
50  has half the size of the i-th image.
51 
52  @note Pointers to the images are held in this class.
53  @author woelk 09/2004 */
54  template <class StorageType>
55  class BIASImage_EXPORT PyramidImage
56  : public Debug, public PyramidImageInterface<StorageType>
57  {
58  public:
59  PyramidImage();
60 
61  PyramidImage(const PyramidImage<StorageType>& pim);
62 
63  PyramidImage(const double factor,
64  const std::vector<SharedPtr<Image<StorageType> > > &imgs);
65 
66  ~PyramidImage();
67 
68  /** @brief copy image into level 0 and create other levels according to
69  parameters set so far (pyramidsize, filter,...)
70  @param py_size 0=auto, 1=only original image
71  @author woelk 09/2004 */
72  void Init(const Image<StorageType>& image, const unsigned py_size=0);
73 
74  /** @brief same as Init(Image) but can use different ST, e.g. construct
75  float pyramid from uchar image, uses ImageConvert then.
76  @param py_size 0=auto, 1=only original image
77  @author koeser */
78  void InitFromImageBase(const ImageBase& image, const unsigned py_size=0);
79 
80  /** @author woelk 09/2004 */
81  void Init(const unsigned int width, const unsigned int height,
82  const unsigned int channelcount, const unsigned py_size);
83 
84  /// initializes with empty images
85  void Init(const unsigned pyramid_size);
86 
87  void Clear();
88  inline void clear() { Clear(); }
89 
90  /// sets this as shallow copy of pim
92  ShallowCopy(const PyramidImage<StorageType>& pim);
93 
94  /// create a shallow clone
95  virtual PyramidImageInterface<StorageType> *ShallowClone() const;
96 
97  /** deep copy assignement
98  @author woelk 09/2004 */
100  operator=(const PyramidImage<StorageType>& pim);
101 
102  /** element access */
103  SharedPtr<Image<StorageType> >& operator[](const unsigned index)
104  { BIASASSERT(index<=_Images.size()); return _Images[index]; }
105 
107  operator[](const unsigned index) const
108  { BIASASSERT(index<_Images.size());
109  BIASASSERT(Get(_Images[index])!=NULL);
110  return _Images[index]; }
111 
112  /** @brief computes (Gaussian) expectation value across a region,
113  used e.g. in anisotropic anti-aliasing */
114  int GetAnisotropicImageValue(const double& xsource,
115  const double& ysource,
116  const Matrix2x2<double>& Cov,
117  double &T,
118  unsigned int channel = 0) const;
119 
120 
121 
122  /** @brief trilinear value from scale space: (x,y) is position in
123  pyramid[0], 0<=scale<=size()-1 is pyramid level
124 
125  Slow, but correct implementation, with boundary checking,
126  takes care of PositionOffset
127 
128  @author koeser 10/2007 */
129  int GetTrilinearImageValue(const double& x,
130  const double& y,
131  const double& scale,
132  double& T,
133  int channel=0) const;
134 
135 
136  /** @brief bilinear value from scale space: (x,y) is position in
137  pyramid[0], 0<=scale<=size()-1 is pyramid level
138 
139  Slow, but correct implementation, with boundary checking,
140  takes care of PositionOffset
141 
142  @author koeser 1/2008 */
143  double GetImageValue(const double& x, const double& y,
144  unsigned int scale, int channel=0) const;
145 
146  /** downsamples from (*this)[0]
147  assumes that all pointers e.g. (*this)[1] are valid
148  and point to an image of correct size
149  returns 0 on success, anything else on failure
150  @author woelk */
151  int Downsample();
152 
153  /** @brief resizes this and fills correctly with smaller images
154  *
155  * Make sure that you do not change the filter type between initial
156  * pyramid creation and the call to this function, otherwise the
157  * global scalar _PositionOffset becomes inconsistent
158  *
159  * No images smaller than minImageWidth will be created. I case such
160  * an image is rejected, return value is set to +1.
161  * @return =0:ok, <0:error, >0 pyramid size limited by min image width
162  * @author koeser 08/2007 */
163  int CreateAdditionalLayer(unsigned int numnewLayers = 1,
164  unsigned int minImageWidth = 32);
165 
166  /** returns a single image containing all pyramid images
167  @author woelk 09/2004 */
168  void GetSingleImage(Image<StorageType>& im) const;
169 
170  void SetUID(BIAS::UUID uid);
171 
172  /** set the ROI for all images */
173  int SetROI(unsigned minx, unsigned miny, unsigned maxx, unsigned maxy);
174 
175  /** set the ROI for all images from original ROI from Img */
176  int SetROI(const ROI &roi);
177 
178  inline bool IsEmpty() const { return _Images.empty(); }
179  inline unsigned Size() const { return (unsigned)_Images.size(); }
180 
181  /** @brief sets all pixels in all images to zero
182  @author woelk 10/2004 */
183  void SetZero();
184 
185  /** @brief writes a file in mip format per pyramid level using prefix
186  @author woelk 10/2004 */
187  int WriteImages(const std::string& prefix) const;
188 
189  /////////////////////////////////////////////////////////////////////////
190  // get/set functions
191  /////////////////////////////////////////////////////////////////////////
192 
193  inline void SetRescaleFactor(const double factor)
194  { _RescaleFactor=factor; _rescale.SetFactor(factor); }
195  inline double GetRescaleFactor() const { return _RescaleFactor; }
196 
198  { _rescale.SetLowPassFilter(filter); }
199  inline void SetLowPassType(int lpt)
200  { _rescale.SetLowPassType(lpt); }
201 
202 
203  inline const std::vector<double>& GetFactors() const { return _vFactor; };
204 
205  inline std::vector<double> GetFactors() { return _vFactor; };
206 
207 
208  /// @author woelk, see _PostionOffset for details
209  inline double GetPositionOffset() const { return _PositionOffset; };
210 
211  // keeps images
212  virtual void resize(const unsigned size);
213 
214  void Dump(std::ostream& os = std::cout) const;
215 
216  bool IsInROI(double x, double y, int layer) const;
217 
218 
219  protected:
220  std::vector<SharedPtr<Image<StorageType> > > _Images;
221 
222  // rescale filter for downsampling
224 
225  // the param of the rescale filter
227 
228  // _vFactor[i] is the dowsampling factor from (*this)[0] -> (*this)[i]
229  std::vector<double> _vFactor;
230 
231  /** Offset which needs to be used when transferring point locations
232  between pyramid levels:
233 
234  (2*xs+offset, 2*ys+offset) = (xb, yb) or
235  (xs, ys) = ((xb-offset)/2.0, (yb-offset)/2.0)
236 
237  where (xs, ys) is the pixel position in pixel coo. the smaller
238  destination image and (xb, yb) is the position in pixel coo. in the
239  bigger source image.
240 
241  see Rescale::DowsampleBy2 for details
242  @author woelk 05/2005 */
244 
245  /** creates the levels by downsampling from (*this)[0] */
246  void _CreateLevels();
247 
249  { _Images.push_back(img); }
250 
251 
252  };
253 
254 } // namespace BIAS
255 
256 
257 #include <Base/Common/BIASpragmaEnd.hh>
258 
259 #endif
std::vector< SharedPtr< Image< StorageType > > > _Images
class for handling different region of interest (ROI) representations...
Definition: ROI.hh:118
T * Get(SharedPtr< T > &t)
void SetLowPassType(int lpt)
Rescale< StorageType, StorageType > _rescale
SharedPtr< Image< StorageType > > & operator[](const unsigned index)
element access
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
std::vector< double > _vFactor
double GetPositionOffset() const
std::vector< double > GetFactors()
bool IsEmpty() const
void SetLowPassFilter(const FilterNToN< StorageType, StorageType > &filter)
SharedPtr< const Image< StorageType > > operator[](const unsigned index) const
double _PositionOffset
Offset which needs to be used when transferring point locations between pyramid levels: ...
The image template class for specific storage types.
Definition: Image.hh:78
interface definition of all pyramid images
unsigned Size() const
const std::vector< double > & GetFactors() const
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
virtual void push_back(SharedPtr< Image< StorageType > > img)
void SetRescaleFactor(const double factor)
double GetRescaleFactor() const