Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestMedian.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Common/CompareFloatingPoint.hh>
26 #include <Base/Debug/Error.hh>
27 #include <Base/Math/Random.hh>
28 #include <MathAlgo/Median1D.hh>
29 #include <iostream>
30 #include <vector>
31 
32 using namespace BIAS;
33 using namespace std;
34 
35 /**
36  @file TestMedian.cpp
37  @brief Test median computation for vectors.
38  @relates Median1D
39  @ingroup g_tests
40  @author MIP
41 */
42 
43 int main(int argc, char *argv[])
44 {
45  Median1D<double> medDouble;
46  Median1D<float> medFloat;
47  Median1D<int> medInt;
48 
49  vector<double> vecDouble;
50  vector<float> vecFloat;
51  vector<int> vecInt;
52 
53  const unsigned int numRuns = 1000;
54  const double minVal = -100.0;
55  const double maxVal = 100.0;
56  const unsigned int minSize = 3;
57  const unsigned int maxSize = 150;
58 
59  Random randomizer;
60  unsigned int size, medIdx, countLT, countGT, numLT, numGT;
61  double median;
62  bool addLT;
63  int result = 0;
64 
65  for (unsigned int run = 1; run <= numRuns; run++)
66  {
67  // create random vectors
68  size = randomizer.GetUniformDistributedInt(minSize, maxSize);
69  median = randomizer.GetUniformDistributed(minVal, maxVal);
70  medIdx = randomizer.GetUniformDistributedInt(0, (int)size-1);
71  vecDouble.resize(size);
72  vecFloat.resize(size);
73  vecInt.resize(size);
74  vecDouble[medIdx] = median;
75  countLT = countGT = 0;
76  numGT = size / 2;
77  numLT = size - 1 - numGT;
78  for (unsigned int i = 0; i < size; i++) {
79  if (i != medIdx) {
80  if (countLT == numLT)
81  addLT = false;
82  else if (countGT == numGT)
83  addLT = true;
84  else
85  addLT = randomizer.GetUniformDistributedInt(0, 100) < 50;
86  if (addLT) {
87  vecDouble[i] = randomizer.GetUniformDistributed(minVal, median);
88  countLT++;
89  } else {
90  vecDouble[i] = randomizer.GetUniformDistributed(median, maxVal);
91  countGT++;
92  }
93  }
94  vecFloat[i] = (float)vecDouble[i];
95  vecInt[i] = (int)vecDouble[i];
96  }
97  BIASASSERT(countLT + countGT + 1 == size);
98  BIASASSERT(vecDouble.size() == size && vecFloat.size() == size && vecInt.size() == size);
99  if (countLT != numLT || countGT != numGT) {
100  cout << "Error in run " << run << "/" << numRuns << " : " << endl
101  << " found " << countLT << " values below median, expected "
102  << numLT << endl << " found " << countGT << " values above median, expected "
103  << numGT << endl << ", vector size is " << size << endl << flush;
104  result = -1;
105  }
106 
107  // compute and compare median
108  medDouble.Compute(vecDouble);
109  medFloat.Compute(vecFloat);
110  medInt.Compute(vecInt);
111  if (!Equal(medDouble.GetMedian(), median, 1e-4) ||
112  !Equal((double)medFloat.GetMedian(), median, 1e-4) ||
113  medInt.GetMedian() != (int)median) {
114  cout << "Error in run " << run << "/" << numRuns << " : " << endl
115  << " [double] computed " << medDouble.GetMedian()
116  << ",\t expected " << median << endl
117  << " [float] computed " << medFloat.GetMedian()
118  << ",\t expected " << (float)median << endl
119  << " [int] computed " << medInt.GetMedian()
120  << ",\t expected " << (int)median << endl
121  << " vector size is " << size << endl << flush;
122  result = -1;
123  }
124  }
125 
126  return result;
127 }
128 
DataType GetMedian() const
Return computed median.
Definition: Median1D.hh:56
Computes the median and p-quantile of a vector.
Definition: Median1D.hh:41
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
void Compute(const std::vector< DataType > &vec)
Compute median and store sorted vector internally.
Definition: Median1D.cpp:32
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
class for producing random numbers from different distributions
Definition: Random.hh:51