Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Median1D.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 "Median1D.hh"
26 #include <Base/Debug/LogFacility.hh>
27 #include <algorithm>
28 
29 using namespace BIAS;
30 
31 template<class DataType>
32 void Median1D<DataType>::Compute(const std::vector<DataType> &vec)
33 {
34  Values_ = vec;
35  if (Values_.empty()) {
36  BIASERR("Failed to compute median for empty vector!");
37  Median_ = DataType(); // undefined
38  } else {
39  std::sort(Values_.begin(), Values_.end());
40  const unsigned int i = (Values_.size() - 1) / 2;
41  Median_ = Values_[i];
42  }
43 }
44 
45 template<class DataType>
46 DataType Median1D<DataType>::GetQuantile(double p) const
47 {
48  if (Values_.empty()) {
49  BIASERR("Failed to compute quantile for empty vector!");
50  return DataType(); // undefined
51  }
52  const double d = std::min(1.0, std::max(0.0, p)) * (Values_.size() - 1);
53  const unsigned int i = (unsigned int)d;
54  return Values_[i];
55 }
56 
57 template<class DataType>
58 DataType Median1D<DataType>::GetX84(const std::vector<DataType> &vec,
59  DataType median) const
60 {
61  if (vec.empty()) {
62  BIASERR("Failed to compute X84 for empty vector!");
63  return DataType(); // undefined
64  }
65  const unsigned int n = vec.size();
66  std::vector<DataType> diff(n);
67  for (unsigned int i = 0; i < n; i++)
68  diff[i] = (vec[i] > median) ? (vec[i] - median) : (median - vec[i]);
69  std::sort(diff.begin(), diff.end());
70  return diff[(n - 1) / 2];
71 }
72 
73 // instance definitions
74 template class BIASMathAlgo_EXPORT BIAS::Median1D<float>;
75 template class BIASMathAlgo_EXPORT BIAS::Median1D<double>;
76 template class BIASMathAlgo_EXPORT BIAS::Median1D<int>;
77 template class BIASMathAlgo_EXPORT BIAS::Median1D<long>;
78 template class BIASMathAlgo_EXPORT BIAS::Median1D<char>;
DataType GetX84() const
Return median of absolute differences to median (X84 rule).
Definition: Median1D.hh:62
Computes the median and p-quantile of a vector.
Definition: Median1D.hh:41
DataType GetQuantile(double p) const
Compute and return p-quantile of vector.
Definition: Median1D.cpp:46
void Compute(const std::vector< DataType > &vec)
Compute median and store sorted vector internally.
Definition: Median1D.cpp:32