36 #ifndef VIGRA_INTEGRALIMAGE_HXX
37 #define VIGRA_INTEGRALIMAGE_HXX
39 #include <vigra/multi_array.hxx>
40 #include <vigra/multi_pointoperators.hxx>
42 #include <vigra/functorexpression.hxx>
46 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
48 cumulativeSum(MultiArrayView<N, T1, S1>
const & image,
49 MultiArrayView<N, T2, S2> out,
51 FUNCTOR
const & functor)
53 typedef typename MultiArrayShape<N>::type ShapeN;
54 ShapeN offset = ShapeN::unitVector(axis);
56 MultiCoordinateIterator<N> i(image.shape()),
57 end(i.getEndIterator());
62 out[*i] = functor(image[*i]);
66 out[*i] = functor(image[*i]) + out[*i - offset];
71 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
73 integralMultiArrayImpl(MultiArrayView<N, T1, S1>
const & array,
74 MultiArrayView<N, T2, S2> intarray,
77 vigra_precondition(array.shape() == intarray.shape(),
78 "integralMultiArray(): shape mismatch between input and output.");
80 cumulativeSum(array, intarray, 0, f);
82 for(
unsigned axis=1; axis < N; ++axis)
83 cumulativeSum(intarray, intarray, axis, functor::Identity());
86 template <
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
88 integralMultiArrayImpl(MultiArrayView<2, T1, S1>
const & image,
89 MultiArrayView<2, T2, S2> intimage,
90 FUNCTOR
const & functor)
92 vigra_precondition(image.shape() == intimage.shape(),
93 "integralMultiArray(): shape mismatch between input and output.");
95 int width = image.shape(0);
96 int height = image.shape(1);
99 for (
int x=0; x<width; ++x)
101 s += functor(image(x, 0));
104 for (
int y=1; y<height; ++y)
107 for (
int x=0; x<width; ++x)
109 s += functor(image(x, y));
110 intimage(x, y) = s + intimage(x, y-1);
115 template <
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
117 integralMultiArrayImpl(MultiArrayView<3, T1, S1>
const & volume,
118 MultiArrayView<3, T2, S2> intvolume,
119 FUNCTOR
const & functor)
121 vigra_precondition(volume.shape() == intvolume.shape(),
122 "integralMultiArray(): shape mismatch between input and output.");
124 int nx = volume.shape(0);
125 int ny = volume.shape(1);
126 int nz = volume.shape(2);
129 MultiArray<1, T2> s2_temp(nx);
134 for (
int iy=0; iy<ny; ++iy)
137 for (
int ix=0; ix<nx; ++ix)
139 s1 += functor(volume(ix, iy, 0));
140 s2 = s2_temp(ix) + s1;
142 intvolume(ix, iy, 0) = s2;
146 for (
int iz=1; iz<nz; ++iz)
150 for (
int iy=0; iy<ny; ++iy)
153 for (
int ix=0; ix<nx; ++ix)
155 s1 += functor(volume(ix, iy, iz));
156 s2 = s2_temp(ix) + s1;
158 intvolume(ix, iy, iz) = s2 + intvolume(ix, iy, iz-1);
164 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
166 integralMultiArray(MultiArrayView<N, T1, S1>
const & array,
167 MultiArrayView<N, T2, S2> intarray,
170 integralMultiArrayImpl(array, intarray, f);
173 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2,
class FUNCTOR>
175 integralMultiArray(MultiArrayView<N, Multiband<T1>, S1>
const & array,
176 MultiArrayView<N, Multiband<T2>, S2> intarray,
179 for(
int channel=0; channel < array.shape(N-1); ++channel)
180 integralMultiArrayImpl(array.bindOuter(channel), intarray.bindOuter(channel), f);
183 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2>
185 integralMultiArray(MultiArrayView<N, T1, S1>
const & array,
186 MultiArrayView<N, T2, S2> intarray)
188 integralMultiArray(array, intarray, functor::Identity());
191 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2>
193 integralMultiArray(MultiArrayView<N, Multiband<T1>, S1>
const & array,
194 MultiArrayView<N, Multiband<T2>, S2> intarray)
196 integralMultiArray(array, intarray, functor::Identity());
199 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2>
201 integralMultiArraySquared(MultiArrayView<N, T1, S1>
const & array,
202 MultiArrayView<N, T2, S2> intarray)
204 using namespace functor;
205 integralMultiArray(array, intarray,
sq(Arg1()));
208 template <
unsigned int N,
class T1,
class S1,
class T2,
class S2>
210 integralMultiArraySquared(MultiArrayView<N, Multiband<T1>, S1>
const & array,
211 MultiArrayView<N, Multiband<T2>, S2> intarray)
213 using namespace functor;
214 integralMultiArray(array, intarray,
sq(Arg1()));
219 #endif // VIGRA_INTEGRALIMAGE_HXX
NumericTraits< T >::Promote sq(T t)
The square function.
Definition: mathutil.hxx:382