| 1 |
|
| 2 |
#ifndef CVD_INC_INTEGRAL_IMAGE_H |
| 3 |
#define CVD_INC_INTEGRAL_IMAGE_H |
| 4 |
|
| 5 |
#include <cvd/image.h> |
| 6 |
#include <cvd/internal/pixel_operations.h> |
| 7 |
#include <cvd/vision.h> |
| 8 |
|
| 9 |
namespace CVD |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
template <class S, class D> |
| 21 |
void integral_image(const BasicImage<S>& in, BasicImage<D>& out) |
| 22 |
{ |
| 23 |
if(in.size() != out.size()) |
| 24 |
throw Exceptions::Vision::IncompatibleImageSizes("integral_image"); |
| 25 |
|
| 26 |
Pixel::operations<D>::assign(out[0][0], in[0][0]); |
| 27 |
|
| 28 |
for(int x = 1; x < in.size().x; x++) |
| 29 |
out[0][x] = out[0][x - 1] + in[0][x]; |
| 30 |
|
| 31 |
|
| 32 |
for(int y = 1; y < in.size().y; y++) |
| 33 |
out[y][0] = out[y - 1][0] + in[y][0]; |
| 34 |
|
| 35 |
|
| 36 |
for(int y = 1; y < in.size().y; y++) |
| 37 |
{ |
| 38 |
D sum; |
| 39 |
Pixel::operations<D>::assign(sum, in[y][0]); |
| 40 |
|
| 41 |
for(int x = 1; x < in.size().x; x++) |
| 42 |
{ |
| 43 |
sum += in[y][x]; |
| 44 |
Pixel::operations<D>::assign(out[y][x], sum + out[y - 1][x]); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
#ifndef DOXYGEN_IGNORE_INTERNAL |
| 49 |
namespace Internal |
| 50 |
{ |
| 51 |
template <class C> |
| 52 |
class IntegralImage |
| 53 |
{ |
| 54 |
}; |
| 55 |
|
| 56 |
template <class C> |
| 57 |
struct ImagePromise<IntegralImage<C>> |
| 58 |
{ |
| 59 |
ImagePromise(const BasicImage<C>& im) |
| 60 |
: i(im) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
const BasicImage<C>& i; |
| 65 |
template <class D> |
| 66 |
void execute(Image<D>& j) |
| 67 |
{ |
| 68 |
j.resize(i.size()); |
| 69 |
integral_image<C, D>(i, j); |
| 70 |
} |
| 71 |
}; |
| 72 |
}; |
| 73 |
|
| 74 |
template <class C> |
| 75 |
Internal::ImagePromise<Internal::IntegralImage<C>> integral_image(const BasicImage<C>& c) |
| 76 |
{ |
| 77 |
return Internal::ImagePromise<Internal::IntegralImage<C>>(c); |
| 78 |
} |
| 79 |
#else |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
template <class S, class D> |
| 98 |
Image<D> integral_image(const BasicImage<S>& from); |
| 99 |
|
| 100 |
#endif |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
#endif |