Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
JointHistogram.cpp
1 #include "JointHistogram.hh"
2 #include <Base/Math/Random.hh>
3 #include <Filter/Gauss.hh>
4 
5 using namespace BIAS;
6 using namespace std;
7 
8 template<class StorageType>
10 
11 
12 }
13 template<class StorageType>
15 
16 
17 }
18 
19 template<class StorageType> int JointHistogram<StorageType>::
22  unsigned int nrOfSamples)
23 {
24 
25  dNumberOfSamples_ = nrOfSamples;
26 
27  BIAS::Gauss<StorageType,float> gaussFilter_;
28  BIAS::Random randomizer;
29 
30  dWidth_ = image1.GetWidth();
31  dHeight_ = image1.GetHeight();
32  dChannelCount_= image1.GetChannelCount();
33 
34  if(dWidth_ != image2.GetWidth() || dHeight_!= image2.GetHeight()
35  || dChannelCount_ !=image2.GetChannelCount() ){
36  BIASERR("Give Images of equal dimension!");
37  return -1;
38  }
39 
40  //gaussfilter the input image
41  gaussFilter_.Filter7x7Grey(image1, gaussImage1_);
42  gaussFilter_.Filter7x7Grey(image2, gaussImage2_);
43 
44  // init the histogram image
45  if(!histImage_.IsEmpty() || !nrImage_.IsEmpty() ){
46  histImage_.Release();
47  nrImage_.Release();
48  }
49 
50  float min1,max1,min2,max2;
51  gaussImage1_.GetMinMaxPixelValue(min1,max1);
52  gaussImage2_.GetMinMaxPixelValue(min2,max2);
53 
54  if(min2>min1) min1=min2;
55  if(max2>max1) max1=max2;
56 
57  histImage_.Init((unsigned int)(rint(max1+1)),(unsigned int)(rint(max1+1)),1);
58  nrImage_.Init((unsigned int)(rint(max1+1)),(unsigned int)(rint(max1+1)),1);
59 
60  float ** ida1 = NULL;
61  float ** ida2 = NULL;
62  float ** idaH = NULL;
63  float ** idaNr = NULL;
64 
65  ida1 = gaussImage1_.GetImageDataArray(); //take gaussfiltered image
66  ida2 = gaussImage2_.GetImageDataArray(); //take gaussfiltered image
67  idaNr = nrImage_.GetImageDataArray();
68  idaH = histImage_.GetImageDataArray();
69 
70  Vector2<unsigned int> coordsTmp;
71 
72  for(unsigned int i=0;i<dNumberOfSamples_;i++){
73  coordsTmp[0] = randomizer.GetUniformDistributedInt(0,(int)dWidth_-1);
74  coordsTmp[1] = randomizer.GetUniformDistributedInt(0,(int)dHeight_-1);
75 
76  float pixel1=0.0,pixel2=0.0;
77  pixel1 = ida1[coordsTmp[1]][coordsTmp[0]]; // get value at y,x
78  pixel2 = ida2[coordsTmp[1]][coordsTmp[0]]; // get value at y,x
79 
80  idaH[(unsigned int)(pixel1)][(unsigned int)(pixel2)]+=1.0;
81  idaNr[(unsigned int)(pixel1)][(unsigned int)(pixel2)] +=1.0;
82  }
83 
84  NormalizeParzen_();
85 
86  return 0;
87 
88 }
89 
90 
91 
92 template<class StorageType> int JointHistogram<StorageType>::
95 {
96  dWidth_ = image1.GetWidth();
97  dHeight_ = image1.GetHeight();
98  dChannelCount_= image1.GetChannelCount();
99 
100  if(dWidth_ != image2.GetWidth() || dHeight_!= image2.GetHeight()
101  || dChannelCount_ !=image2.GetChannelCount() ){
102  BIASERR("Give Images of equal dimension!");
103  return -1;
104  }
105 
106  if(!histImage_.IsEmpty())
107  histImage_.Release();
108 
109  StorageType min1,max1,min2,max2;
110  image1.GetMinMaxPixelValue(min1,max1);
111  image2.GetMinMaxPixelValue(min2,max2);
112 
113  if(min2>min1) min1=min2;
114  if(max2>max1) max1=max2;
115 
116  if(!histImage_.IsEmpty())
117  histImage_.Release();
118  histImage_.Init((unsigned int)(max1+1),(unsigned int)(max1+1),1);
119 
120  float ** idaHist = histImage_.GetImageDataArray();
121  StorageType ** ida1 = image1.GetImageDataArray();
122  StorageType ** ida2 = image2.GetImageDataArray();
123 
124  //count total number of pixels
125  pixelCount_=0;
126 
127  for(unsigned int x=0;x< dWidth_;x++){
128  for(unsigned int y=0;y< dHeight_;y++){
129 
130  idaHist[(unsigned int)(ida1[y][x])][(unsigned int)(ida2[y][x])]+=1.0;
131  pixelCount_++;
132  }
133  }
134  drawImage_ = histImage_;
135  //normalize image to enable Shannon entropy
136  Normalize_();
137 
138  return 0;
139 }
140 
141 template<class StorageType> void JointHistogram<StorageType>::
143  image = drawImage_;
144 }
145 
146 template<class StorageType> float JointHistogram<StorageType>::
148  float entropy=0.0;
149  unsigned int width = histImage_.GetWidth();
150  unsigned int height = histImage_.GetHeight();
151 
152  float ** idaHist = histImage_.GetImageDataArray();
153 
154  for(unsigned int x=0;x< width;x++){
155  for(unsigned int y=0;y< height;y++){
156 
157  if(idaHist[y][x] != 0.0){
158  entropy += (idaHist[y][x]*log(idaHist[y][x]));
159 
160  }
161  }
162  }
163  return (-1)*entropy;
164 }
165 
166 template<class StorageType> float JointHistogram<StorageType>::
167 CalcRenyiEntropy(double alpha){
168  float entropy=0.0;
169  unsigned int width = histImage_.GetWidth();
170  unsigned int height = histImage_.GetHeight();
171 
172  float ** idaHist = histImage_.GetImageDataArray();
173 
174  for(unsigned int x=0;x< width;x++){
175  for(unsigned int y=0;y< height;y++){
176 
177  if(idaHist[y][x] != 0.0){
178  entropy += pow((float)idaHist[y][x],(float)alpha);
179 
180  }
181  }
182  }
183  return (float)( (1/(1-alpha))*log(entropy) );
184 }
185 
186 
187 template<class StorageType> void JointHistogram<StorageType>::
189  unsigned int width = histImage_.GetWidth();
190  unsigned int height = histImage_.GetHeight();
191 
192  float ** idaHist = histImage_.GetImageDataArray();
193  float ** idaNr = nrImage_.GetImageDataArray();
194  for(unsigned int x=0;x< width;x++){
195  for(unsigned int y=0;y< height;y++){
196  if(idaNr[(int)y][(int)x] != 0.0)
197  idaHist[y][x] /= idaNr[(int)y][(int)x]*dNumberOfSamples_;
198  else if(idaHist[y][x] != 0.0)
199  idaHist[y][x] /=dNumberOfSamples_;
200 
201  }
202  }
203 }
204 
205 template<class StorageType> void JointHistogram<StorageType>::
207  unsigned int width = histImage_.GetWidth();
208  unsigned int height = histImage_.GetHeight();
209 
210  float ** idaHist = histImage_.GetImageDataArray();
211 
212  for(unsigned int x=0;x< width;x++){
213  for(unsigned int y=0;y< height;y++){
214  idaHist[y][x] /= (float)pixelCount_;
215  }
216  }
217 }
218 
219 #define INST(type) template class JointHistogram<type>;
220 namespace BIAS{
221 INST(float)
222 INST(unsigned char)
223 }
virtual int Filter7x7Grey(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
fast, approximate, direct implementation, ignoring wrap-arounds, roi is updated but ignored ...
Definition: Gauss.cpp:516
void GetMinMaxPixelValue(StorageType &min, StorageType &max, unsigned short int channel=0, unsigned int *mincoo=NULL, unsigned int *maxcoo=NULL) const
returns the minimal and maximal pixel value in channel only Finds minimum and maximum pixel value in ...
Definition: Image.cpp:802
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
smoothing with gaussian kernel
Definition: Gauss.hh:51
void Draw(BIAS::Image< float > &image)
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int Compute(BIAS::Image< StorageType > &image1, BIAS::Image< StorageType > &image2)
unsigned int GetHeight() const
Definition: ImageBase.hh:319
INST(unsigned char)
The image template class for specific storage types.
Definition: Image.hh:78
float CalcRenyiEntropy(double alpha)
int ComputeParzenWindow(BIAS::Image< StorageType > &image1, BIAS::Image< StorageType > &image2, unsigned int nrOfSamples)
class for producing random numbers from different distributions
Definition: Random.hh:51
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153