Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Pyramid.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
24 #ifndef VIGRA_EXT_PYRAMID_H
25 #define VIGRA_EXT_PYRAMID_H
26 
27 #include <vigra/separableconvolution.hxx>
28 
29 #include <hugin_utils/utils.h>
30 
31 
32 // TODO: use fast version from enblend 3.0
33 #include <vigra_ext/pyramid2.h>
34 
35 
36 namespace vigra_ext {
37 
38 template <class ImageIn, class Image>
39 void reduceNTimes(ImageIn & in, Image & out, int n)
40 {
41  typedef typename Image::value_type vt;
42  typedef typename vigra::NumericTraits<vt>::RealPromote SKIPSMType;
43  if (n <= 0) {
44  out = in;
45  return;
46  }
47 
48  size_t w = in.width();
49  size_t h = in.height();
50  // Size of next level
51  w = (w + 1) >> 1;
52  h = (h + 1) >> 1;
53 
54  Image temp;
55  Image * curr = &temp;
56  Image * next = &out;
57  if( (n % 2) == 1)
58  {
59  // even number of reduce operations, first reduce into output image
60  curr = &out;
61  next = &temp;
62  }
63  curr->resize(w,h);
64  enblend::reduce<SKIPSMType>(false, srcImageRange(in), destImageRange(*curr));
65  n--;
66  w = (w + 1) >> 1;
67  h = (h + 1) >> 1;
68  for( ; n > 0 ; --n) {
69  next->resize(w,h);
70  enblend::reduce<SKIPSMType>(false, srcImageRange(*curr), destImageRange(*next));
71  w = (w + 1) >> 1;
72  h = (h + 1) >> 1;
73  Image * t = curr;
74  curr = next;
75  next = t;
76  }
77 }
78 
79 template <class Image, class ImageMask>
80 void reduceNTimes(Image & in, ImageMask & inMask, Image & out, ImageMask & outMask, int n)
81 {
82  typedef typename Image::value_type vt;
83  typedef typename vigra::NumericTraits<vt>::RealPromote SKIPSMType;
84  typedef double SKIPSMAlphaType;
85 
86  if (n <= 0) {
87  out = in;
88  outMask = inMask;
89  return;
90  }
91 
92  size_t w = in.width();
93  size_t h = in.height();
94  // Size of next level
95  w = (w + 1) >> 1;
96  h = (h + 1) >> 1;
97 
98  Image temp;
99  ImageMask tempMask;
100  Image * curr = &temp;
101  ImageMask * currMask = &tempMask;
102  Image * next = &out;
103  ImageMask * nextMask = &outMask;
104  if( (n % 2) == 1)
105  {
106  // even number of reduce operations, first reduce into output image
107  curr = &out;
108  currMask = &outMask;
109  next = &temp;
110  nextMask = &tempMask;
111  }
112  curr->resize(w,h);
113  currMask->resize(w,h);
114  enblend::reduce<SKIPSMType, SKIPSMAlphaType>(false, srcImageRange(in), srcImage(inMask),
115  destImageRange(*curr), destImageRange(*currMask));
116  n--;
117  w = (w + 1) >> 1;
118  h = (h + 1) >> 1;
119  for( ; n > 0 ; --n) {
120  next->resize(w,h);
121  nextMask->resize(w,h);
122  enblend::reduce<SKIPSMType, SKIPSMAlphaType>(false, srcImageRange(*curr), srcImage(*currMask),
123  destImageRange(*next), destImageRange(*nextMask));
124  w = (w + 1) >> 1;
125  h = (h + 1) >> 1;
126  Image * t = curr;
127  ImageMask * tm = currMask;
128  curr = next;
129  currMask = nextMask;
130  next = t;
131  nextMask = tm;
132  }
133 }
134 
135 template <class ImageIn, class ImageOut>
136 void reduceToNextLevel(ImageIn & in, ImageOut & out)
137 {
138  typedef typename ImageOut::value_type vt;
139  typedef typename vigra::NumericTraits<vt>::RealPromote SKIPSMType;
140 
141  size_t w = in.width();
142  size_t h = in.height();
143  // Size of next level
144  w = (w + 1) >> 1;
145  h = (h + 1) >> 1;
146  out.resize(w,h);
147  enblend::reduce<SKIPSMType>(false, srcImageRange(in), destImageRange(out));
148 }
149 
150 template <class ImageIn, class ImageInMask, class ImageOut, class ImageOutMask>
151 void reduceToNextLevel(ImageIn & in, ImageInMask & inMask, ImageOut & out, ImageOutMask & outMask)
152 {
153  typedef typename ImageOut::value_type vt;
154  typedef typename vigra::NumericTraits<vt>::RealPromote SKIPSMType;
155  //typedef typename vigra::NumericTraits<typename ImageOutMask::value_type>::Promote SKIPSMAlphaType;
156  typedef double SKIPSMAlphaType;
157 
158  size_t w = in.width();
159  size_t h = in.height();
160  // Size of next level
161  w = (w + 1) >> 1;
162  h = (h + 1) >> 1;
163  out.resize(w,h);
164  outMask.resize(w, h);
165  enblend::reduce<SKIPSMType, SKIPSMAlphaType>(false, srcImageRange(in), srcImage(inMask),
166  destImageRange(out), destImageRange(outMask));
167 }
168 
169 
170 static const double AA = 0.4;
171 static const double W[] = {0.25 - AA / 2.0, 0.25, AA, 0.25, 0.25 - AA / 2.0};
172 
177 template <class Image>
178 void reduceToNextLevelOld(Image & in, Image & out)
179 {
180  DEBUG_TRACE("");
181  // image size at current level
182  int width = in.width();
183  int height = in.height();
184 
185  // image size at next smaller level
186  int newwidth = (width + 1) / 2;
187  int newheight = (height + 1) / 2;
188 
189  // resize result image to appropriate size
190  out.resize(newwidth, newheight);
191 
192  // define a Gaussian kernel (size 5x1)
193  // with sigma = 1
194  vigra::Kernel1D<double> filter;
195  filter.initExplicitly(-2, 2) = W[0], W[1], W[2], W[3], W[4];
196 
197  vigra::BasicImage<typename Image::value_type> tmpimage1(width, height);
198  vigra::BasicImage<typename Image::value_type> tmpimage2(width, height);
199 
200  // smooth (band limit) input image
201  separableConvolveX(srcImageRange(in),
202  destImage(tmpimage1), kernel1d(filter));
203  separableConvolveY(srcImageRange(tmpimage1),
204  destImage(tmpimage2), kernel1d(filter));
205 
206  // downsample smoothed image
207  resizeImageNoInterpolation(srcImageRange(tmpimage2), destImageRange(out));
208 
209 }
210 
211 } // namespace
212 
213 
214 #endif // VIGRA_EXT_PYRAMID_H
void reduceToNextLevel(ImageIn &in, ImageOut &out)
Definition: Pyramid.h:136
void reduceToNextLevelOld(Image &in, Image &out)
Gaussian reduction to next pyramid level.
Definition: Pyramid.h:178
#define DEBUG_TRACE(msg)
Definition: utils.h:67
vigra::pair< typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::ImageConstAccessor > srcImage(const ROIImage< Image, Mask > &img)
Definition: ROIImage.h:300
static const double AA
Definition: Pyramid.h:170
IMPEX double h[25][1024]
Definition: emor.cpp:169
vigra::pair< typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::ImageAccessor > destImage(ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:324
vigra::triple< typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::ImageConstAccessor > srcImageRange(const ROIImage< Image, Mask > &img)
helper function for ROIImages
Definition: ROIImage.h:287
static const double W[]
Definition: Pyramid.h:171
vigra::triple< typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::ImageAccessor > destImageRange(ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:312
void reduceNTimes(ImageIn &in, Image &out, int n)
Definition: Pyramid.h:39