Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IntegralImage.cpp
1 #include "IntegralImage.hh"
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 template<class StorageType>
8  width_ = 0;
9  height_ = 0;
10  channels_ = 0;
11  intImg_ = NULL;
12 }
13 
14 template<class StorageType>
16  width_ = 0;
17  height_ = 0;
18  channels_ = 0;
19  intImg_ = NULL;
20  GenerateFrom(im);
21 }
22 
23 template<class StorageType>
25  if (intImg_ != NULL) delete[] intImg_;
26 }
27 
28 template<class StorageType>
30 
31  // Initialize the array if necessary
32  if (intImg_ != NULL){
33  if (width_ != (int)im.GetWidth() || height_ != (int)im.GetHeight()){
34  delete[] intImg_;
35  intImg_ = new double[im.GetWidth()*im.GetHeight()];
36  }
37  } else {
38  intImg_ = new double[im.GetWidth()*im.GetHeight()];
39  }
40 
41  // Get image properties
42  width_ = im.GetWidth();
43  height_ = im.GetHeight();
44  channels_ = im.GetChannelCount();
45 
46 
47  const StorageType** idaIn = (const StorageType**) im.GetImageDataArray();
48 
49  // Start with upper left entry
50  intImg_[0] = (double)idaIn[0][0];
51 
52  // Generate the first row by adding each entry to its predecessor
53  for (unsigned int x=1; (int)x < width_; x++)
54  intImg_[x] = (double)idaIn[0][x*channels_]+intImg_[x-1];
55 
56  // Generate the first column by adding each entry to its predecessor
57  for (unsigned int y=1; (int)y < height_; y++)
58  intImg_[y*width_] = (double)idaIn[y][0]+intImg_[(y-1)*width_];
59 
60  // Fill out the rest of the image by adding the upper predecessor and
61  // the left predecessor minus the upper left predecessor
62  for (unsigned int y=1; (int)y < height_; y++)
63  for (unsigned int x=1; (int)x < width_; x++)
64  intImg_[y*width_+x] = (double)idaIn[y][x*channels_]+intImg_[(y-1)*width_+x]
65  +intImg_[y*width_+(x-1)] - intImg_[(y-1)*width_+(x-1)];
66 
67 }
68 
69 template<class StorageType>
70 double IntegralImage<StorageType>::GetDOM(int x, int y, int halfLevelSize){
71 
72  // If the blob is out of bounds, return 0
73  if (halfLevelSize <= 0 || x - halfLevelSize*2 < 0 || y - halfLevelSize*2 < 0 || x + halfLevelSize*2 +1 >= (int)width_ || y + halfLevelSize*2 +1>= (int)height_)
74  return 0.0;
75 
76  // Calculate the sum of the inner pixels via integral image:
77  double innerValueSum = GetAreaSum(x-halfLevelSize,y-halfLevelSize,x+halfLevelSize+1,y+halfLevelSize+1);
78  // Calculate the sum of pixel values of the doubled square via integral image:
79  double completeValueSum = GetAreaSum(x-(halfLevelSize*2),y-(halfLevelSize*2),x+(halfLevelSize*2)+1,y+(halfLevelSize*2)+1);\
80  // outer sum is the big sum minus the inner sum
81  double outerValueSum = completeValueSum - innerValueSum;
82  // Calculate number of pixels:
83  double numOfInnerElements = (halfLevelSize*2+1)*(halfLevelSize*2+1);
84  double numOfOuterElements = (halfLevelSize*4+1)*(halfLevelSize*4+1) - numOfInnerElements;
85  // DOM is average inner minus average outer pixel value
86  return (innerValueSum/numOfInnerElements) - (outerValueSum/numOfOuterElements);
87 }
88 
89 
90 
91 #define INSTANCE_IntImage(type)\
92 template class BIASUtils_EXPORT IntegralImage<type>;
93 
94 // create instances
95 namespace BIAS{
96 INSTANCE_IntImage(unsigned char)
97 INSTANCE_IntImage(float)
98 #ifdef BUILD_IMAGE_INT
99 INSTANCE_IntImage(int)
100 #endif
101 #ifdef BUILD_IMAGE_CHAR
102 INSTANCE_IntImage(char)
103 #endif
104 #ifdef BUILD_IMAGE_SHORT
105 INSTANCE_IntImage(short)
106 #endif
107 #if defined(BUILD_IMAGE_USHORT)
108 INSTANCE_IntImage(unsigned short)
109 #endif
110 #ifdef BUILD_IMAGE_DOUBLE
111 INSTANCE_IntImage(double)
112 #endif
113 #ifdef BUILD_IMAGE_UINT
114 INSTANCE_IntImage(unsigned int)
115 #endif
116 }
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void ** GetImageDataArray() const
Get an array of pointers to image data.
Definition: ImageBase.hh:305
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
The image template class for specific storage types.
Definition: Image.hh:78
This is the base class for images in BIAS.
Definition: ImageBase.hh:102