-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStatisticalFunctions.cpp
445 lines (327 loc) · 8.64 KB
/
StatisticalFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "StatisticalFunctions.h"
#include "sorting.h"
using namespace IITkgp_functions;
/*-----------------------------Statistical function--------------------------------*/
vector<float> IITkgp_functions::FindHist(Mat Data)
{
double maxe;
int maxposi;
FindMaxElementPosi(Data,&maxe, &maxposi);
Data.convertTo(Data,CV_64FC1);
int nele =(int) (floor(maxe) + 1);
vector<float> hist(nele,0.0);
for(int i=0;i<Data.rows;i++)
{
for(int j=0;j<Data.cols;j++)
{
int posi =(int) floor(Data.at<double>(i,j));
hist[posi]++;
}
}
for(int i=0;i<hist.size();i++)
hist[i] = hist[i]*1.0/(Data.rows*Data.cols*1.0);
return hist;
}
/**
* @function FindMedian
* @param input vector of data
* @brief Calculate Median of Given data array
* @return median(double) of the given array
*/
template <typename T>
double IITkgp_functions::FindMedian(vector<T> data)
{
vector<T> sortedarray;
for(int i=0;i<data.size();i++)
sortedarray.push_back(data[i]);
printf("Hello going to sort\n");
//MergeSort<T>(sortedarray,0,data.size()-1);
BubbleSort<T>(sortedarray);
printf("Hello sorting is done\n");
double Median;
//check even
if(sortedarray.size()%2 == 0)
{
int MedPosi = (sortedarray.size()/2);
Median = (sortedarray[MedPosi] + sortedarray[MedPosi - 1])*1.0/2;
}
else
{
int MedPosi = (sortedarray.size()/2) - 1;
Median = sortedarray[MedPosi]*1.0;
}
sortedarray.clear();
return(Median);
}
void initializemedian()
{
vector<int> data(10,0);
double med = FindMedian<int>(data);
vector<float> data1(10,0);
med = FindMedian<float>(data1);
vector<double> data2(10,0);
med = FindMedian<double>(data2);
}
/**
* @function FindMean
* @param input Single Channel Mat data
* @brief Calculate Mean of Given data array
* @return mean(double) of the given array
*/
double IITkgp_functions::FindMean(Mat data)
{
data.convertTo(data,CV_64FC1);
double mean;
double sum;
sum = 0.0;
int data_size;
data_size = data.rows*data.cols;
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
sum = sum + data.at<double>(i,j);
}
mean = sum/data_size;
return(mean);
}
/**
* @function FindVar
* @param input Single Channel Mat data
* @brief Calculate variance of Given data array
* @return Variance(double) of the given array
*/
double IITkgp_functions::FindVar(Mat data)
{
data.convertTo(data,CV_64FC1);
double var,mean;
mean = FindMean(data);
double temp;
double sum=0.0;
int data_size;
data_size = data.rows*data.cols;
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
{
temp = data.at<double>(i,j) - mean;
sum = sum + (temp * temp);
}
}
var = sum/data_size;
return(var);
}
/**
* @function FindStdDev
* @param input Single Channel Mat data
* @brief Calculate Standard Deviation of Given data array
* @return Standard Deviation(double) of the given array
*/
double IITkgp_functions::FindStdDev(Mat data)
{
data.convertTo(data,CV_64FC1);
double std_dev,var;
var = FindVar(data);
std_dev = sqrt(var);
return(std_dev);
}
/**
* @function FindSkew
* @param input Single Channel Mat data
* @brief Calculate Skewness of Given data array
* @return Skewness(double) of the given array
*/
double IITkgp_functions::FindSkew(Mat data)
{
data.convertTo(data,CV_64FC1);
double skew;
double sum = 0.0;
double mean;
double temp;
double std_dev;
int data_size;
data_size = data.rows*data.cols;
mean = FindMean(data);
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
{
temp = data.at<double>(0,i) - mean;
sum = sum + (temp * temp * temp);
}
}
sum = sum / data_size;
std_dev = FindStdDev(data);
skew = sum/(std_dev * std_dev *std_dev);
}
/**
* @function FindMinElementPosi
* @param input Single Channel Mat data and pointer to min element and its position
* @brief Calculate Min value of Given data array and its position
*
*/
void IITkgp_functions::FindMinElementPosi(Mat data, double *value, int *posi)
{
data.convertTo(data,CV_64FC1);
double min_element;
min_element = data.at<double>(0,0);
int min_posi;
int data_size;
data_size = data.rows*data.cols;
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
{
if(data.at<double>(i,j)<=min_element)
{
min_element = data.at<double>(i,j);
min_posi = i*data.cols+j;
}
}
}
*value = min_element;
*posi = min_posi;
}
/**
* @function FindMaxElement
* @param input Single Channel Mat data and pointer to max element and pointer to position
* @brief Calculate Max value of Given data array and its position
*
*/
void IITkgp_functions::FindMaxElementPosi(Mat Mdata, double *value, int *posi)
{
Mat data;
Mdata.convertTo(data,CV_64FC1);
double max_element = 0.0;
// max_element = data.at<double>(0,0);
int max_posi = 0;
// int data_size;
// data_size = data.rows*data.cols;
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
{
if(max_element <= data.at<double>(i,j))
{
max_element = data.at<double>(i,j);
max_posi = i*data.cols+j;
}
}
}
// printf("max value %lf\n",max_element);
*value = max_element;
*posi = max_posi;
}
/**
* @function FindMode
* @param input Single Channel Mat data
* @brief Calculate Mode of Given data array
* @return mean(double) of the given array
*/
double IITkgp_functions::FindMode(Mat data)
{
data.convertTo(data,CV_64FC1);
double mode;
double max_elem;
int max_posi;
FindMaxElementPosi(data,&max_elem,&max_posi);
mode = max_elem;
return(mode);
}
/**
* @function FindVarMode
* @param input Single Channel Mat data
* @brief Calculate variance of Given data array
* @return Variance(double) of the given array
*/
double IITkgp_functions::FindVarMode(Mat data)
{
data.convertTo(data,CV_64FC1);
double var,mode;
mode = FindMode(data);
double temp;
double sum=0.0;
int data_size;
data_size = data.rows*data.cols;
for(int i=0;i<data.rows;i++)
{
for(int j=0;j<data.cols;j++)
{
temp = data.at<double>(0,i) - mode;
sum = sum + (temp * temp);
}
}
var = sum/data_size;
return(var);
}
/**
* @function FindStdDevMode
* @param input Single Channel Mat data
* @brief Calculate Standard Deviation of Given data array based on Mode of the data
* @return Standard Deviation(double) of the given array
*/
double IITkgp_functions::FindStdDevMode(Mat data)
{
data.convertTo(data,CV_64FC1);
double std_dev,var;
var = FindVarMode(data);
std_dev = sqrt(var);
return(std_dev);
}
/**
* @function FindHistogram
* @param input Single Channel Mat data
* @brief Calculate Histogram of the data
* @return Histogram data of the element
*/
Mat IITkgp_functions::FindHistogram(Mat data)
{
Mat HistData;
double max_elem;
int max_posi;
FindMaxElementPosi(data,&max_elem,&max_posi);
bool uniform = true; bool accumulate = false;
int histSize = (int)max_elem;
//printf("HistSize is %d\t%lf\n",histSize,max_elem);
// int histSize = 256;
/// Set the ranges
float range[] = { 0, histSize } ;
const float* histRange = { range };
Mat ConvertedData;
data.convertTo(ConvertedData,CV_8UC1);
/// Compute the histograms:
calcHist( &ConvertedData, 1, 0, Mat(), HistData, 1, &histSize, &histRange, uniform, accumulate );
return(HistData);
}
/**
* @function DrawHistogram
* @param input Single Channel Mat data
* @brief Calculate Histogram of the data and Draw it
*
*/
void IITkgp_functions::DrawHistogram(Mat data)
{
Mat Histogram,NormalizedHistogram;
Histogram = FindHistogram(data);
double max_elem;
int max_posi;
FindMaxElementPosi(data,&max_elem,&max_posi);
int histSize = (int)max_elem;
//int histSize = 256;
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
//Histogram.convertTo(Histogram,CV_8UC1);
/// Normalize the result to [ 0, histImage.rows ]
normalize(Histogram, Histogram, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(Histogram.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(Histogram.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
}
/// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage );
}
/*-------------------------------------------------------------------------------------------------------------------------------------------*/