21 #ifndef _INTEGRAL_IMAGE_H
22 #define _INTEGRAL_IMAGE_H
85 this->height = height;
86 data =
new T[this->width * this->height];
89 T & operator() (
int i,
int j) {
92 const T & operator() (
int i,
int j)
const {
102 if(x<0 || y < 0 || x >=(
int)this->
width || y>=(
int)this->height)
104 std::cerr <<
"pixel_at(" << x <<
',' << y <<
")" << std::endl;
105 throw std::runtime_error(
"pixel_at called with invalid parameters");
108 if(x>0 && y>0) acc +=
data[(x-1) + (y-1) *
width];
117 double mx = x - (double) ix;
119 double my = y - (double) iy;
135 if(iy>=(
int)height-1)
141 return data[(ix+1)+(iy+1)*
width] * mx * my +
142 data[ix + iy *
width] * (1.0 - mx) * (1.0 - my) +
143 data[ix + (iy+1) *
width] * (1.0 - mx) * my +
144 data[(ix+1) + iy *
width] * mx * (1.0 - my);
156 void Export(D * buf,
int x0,
int y0,
int dst_width,
int dst_height)
const
158 for(
int j = 0; j<dst_height; ++j)
159 for(
int i = 0; i<dst_width; ++i)
162 if(val > std::numeric_limits<D>::max() )
163 val = std::numeric_limits<D>::max();
164 if(val < std::numeric_limits<D>::min() )
165 val = std::numeric_limits<D>::min();
166 buf[i + j * dst_width] = val;
175 template<
class DataType>
176 void ResampleAndExport(DataType * buf,
double x0,
double y0,
double x1,
double y1,
int dst_width,
int dst_height)
const
178 const double dx = (x1 - x0) / (
double) dst_width;
179 const double dy = (y1 - y0) / (
double) dst_height;
180 const double norm = 1.0 / (dx*dy);
186 for(
int j = 0; j<dst_height; ++j)
187 for(
int i = 0; i<dst_width; ++i)
196 buf[i + j * dst_width] = norm*(A+D-B-C);
202 void Build(
const _S *src,
unsigned int width,
unsigned int height,
long stride,
bool extend =
false)
208 this->width = width + 1;
209 this->height = height + 1;
214 this->height = height;
217 data =
new T[this->width * this->height];
225 for(
unsigned int j=0; j<width+2; j++)
230 for(
unsigned int j=0; j<
width; j++)
238 long prev_row = - (long) this->width;
241 for(
unsigned int i=1; i<height; ++i)
249 for(
unsigned int j=0; j<
width; ++j)
252 *i_data = rs + i_data[prev_row];
278 dst.
Resize(dstWidth, dstHeight);
280 double norm = 1.0 / (du*dv);
288 const double w0 = 0.5;
294 i0 = (int) ceil(-u0 / du);
301 j0 = (int) ceil(-v0 / dv);
309 for(
unsigned int j=j0; j<dstHeight; ++j)
311 int iv = (int) trunc(v);
312 double mv = v - trunc(v);
314 if(iv+1 < (
int) src.height)
316 for(
unsigned int i=i0; i<dstWidth; ++i)
319 int iu = (int) trunc(u);
320 double mu = u - trunc(u);
322 const T *ptr = &src.
data[ iu + iv * src.
width];
324 double t = norm * ( (double) ptr[0] * (1.0 - mu) * (1.0 - mv) + (
double) ptr[1] * (mu) * (1.0 - mv) +
325 (double) ptr[src.
width] * (1.0 - mu) * (mv) + (
double) ptr[src.
width + 1] * (mu) * (mv) );
327 dst.
data[i + dst.
width *j] = (T) (t + w0);
335 for(
unsigned int i=i0; i<dstWidth; ++i)
338 int iu = (int) trunc(u);
339 double mu = u - trunc(u);
341 const T *ptr = &src.
data[ iu + iv * src.
width];
343 double t = norm * ( (iu+1 < (int) src.
width) ? ( (double) ptr[0] * (1.0 - mu) + (double) ptr[1] * (mu) ) : ptr[0] );
345 dst.
data[i + dst.
width *j] = (T) (t + w0);
364 double u0 = srcRect.x0;
365 double v0 = srcRect.y0;
366 double du = (double) (srcRect.x1 - srcRect.x0) / (double) dstWidth;
367 double dv = (double) (srcRect.y1 - srcRect.y0) / (double) dstHeight;
369 Resample<T>(dst, dstWidth, dstHeight, src, u0,v0,du,dv);
T * data
Definition: IntegralImage.h:48
void Resize(unsigned int width, unsigned int height)
Resize (destructive) of buffer.
Definition: IntegralImage.h:81
Types involved in boosting.
unsigned int bpp
byte per pixel (1 for grayscale image, 2 for 16bit greyscale image, 3 for RGB/YUV/etc color image) ...
Definition: Image.h:37
void Build(const ImageHandle &src, bool extend=false)
Build the integral image using an ImageHandle object.
Definition: IntegralImage.h:261
void ResampleAndExport(DataType *buf, double x0, double y0, double x1, double y1, int dst_width, int dst_height) const
Definition: IntegralImage.h:176
Definition: IntegralImage.h:375
void Export(D *buf, int x0, int y0, int dst_width, int dst_height) const
Definition: IntegralImage.h:156
void Resample(ImageHandle &out, const ImageHandle &in, const rect &area)
Crop and Resample an Image using IntegralImage algorithm.
long stride
line stride, the delta offset, in bytes, between two different scanline
Definition: Image.h:41
Definition: IntegralImage.h:45
IntegralImageHandle(const IntegralImageHandle< T > &src)
Definition: IntegralImage.h:56
unsigned int width
image geometry
Definition: Image.h:39
unsigned int width
Definition: IntegralImage.h:50
double interpolated_value_at(double x, double y) const
recover using interpolation integral-image value in x,y
Definition: IntegralImage.h:115
a rectangle structure
Definition: Types.h:55
void Build(const _S *src, unsigned int width, unsigned int height, long stride, bool extend=false)
Build integral image from buffer.
Definition: IntegralImage.h:202
additional typedef, for portability under win32
T pixel_at(int x, int y) const
Definition: IntegralImage.h:99
void Release()
Definition: IntegralImage.h:74
unsigned char * data
initial address of the first pixel. It must be cast to correct format (uint8, uint16, rgb, etc etc)
Definition: Image.h:43