File: itkTextureHistogram.h

package info (click to toggle)
ants 2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,656 kB
  • sloc: cpp: 84,137; sh: 11,419; perl: 694; xml: 115; makefile: 74; python: 48
file content (171 lines) | stat: -rw-r--r-- 4,314 bytes parent folder | download | duplicates (2)
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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
// histogram from the moving histogram operations
#ifndef __itkTextureHistogram_h
#define __itkTextureHistogram_h
#include "itkNumericTraits.h"

namespace itk
{
namespace Function
{

/*
 *
 *
 */
template< class TInputPixel, class TOutputPixel >
class TextureHistogram
{
public:

  TextureHistogram()
    {
      m_Count = 0;
    }

  // ~TextureHistogram()  {} default is ok

  void AddPixel( const TInputPixel & p )
    {
    m_Map[p]++;
    ++m_Count;
    }

  void RemovePixel(const TInputPixel & p)
  {

    // insert new item if one doesn't exist
    typename MapType::iterator it = m_Map.find( p );

    assert( it != m_Map.end() );

    if ( --(it->second) == 0 )
      {
      m_Map.erase( it );
      }
    --m_Count;

  }

  TOutputPixel GetValue(const TInputPixel &)
    {
      TOutputPixel out;
      NumericTraits<TOutputPixel>::SetLength( out, 8 );

      double sum = 0.0;
      double sum2 = 0.0;
      double sum3 = 0.0;
      double sum4 = 0.0;
      const size_t count = m_Count;
      //double median = 0.0;

      double entropy = 0.0;
      size_t curCount = 0;
      for ( typename MapType::iterator i = m_Map.begin(); i != m_Map.end(); ++i )
        {
        double t = double(i->first)*double(i->second);
        sum += t;
        sum2 += ( t *= double(i->first) );
        sum3 += ( t *= double(i->first) );
        sum4 += ( t *= double(i->first) );

        curCount += i->second;

        const double p_x = double( i->second ) / count;
        entropy += -p_x*vcl_log( p_x );

//       // this is wrong!
//       if ( curCount == count / 2 )
//         {
//         median += i->first;
//         medianIt = il
//         // we have an even number so take the average
//           if ( !(count % 2) )
//             {
//               median *= 0.5;
//             }
//         }

      }

//     curCount = 0;
//     typename MapType::iterator fmedianIt = medianIt;
//     typename MapType::iterator rmedianIt = medianIt;
//     double mad = 0.0;

//     while (curCount < count/2 )
//     {
//     if ( vcl_fabs( fmedianIt->first - median ) < vcl_fabs( rmedianIt->first - median ) )
//       {
//       curCount += fmedianIt->second;
//       ++fmedianIt;
//       }
//     else
//       {
//       curCount += rmedianIt->second;
//       --rmedianIt;
//       }
//     }


      const double icount = 1.0 / count;
      const double mean = sum * icount;

        // unbiased estimate
      const double variance = ( sum2 - ( sum * sum * icount ) ) / ( count - 1 );
      const double sigma = vcl_sqrt(variance);
      double skewness = 0.0;
      double kurtosis = 0.0;
    if(vcl_abs(variance * sigma) > itk::NumericTraits<double>::min())
      {

      skewness = ( ( sum3 - 3.0 * mean * sum2 ) * icount + 2.0 * mean * mean*mean ) / ( variance * sigma );
      }
    if(vcl_abs(variance) > itk::NumericTraits<double>::min())
      {
        kurtosis = ( sum4 * icount  + mean *( -4.0 * sum3 * icount  +  mean * ( 6.0 *sum2 * icount  - 3.0 * mean * mean ))) /
        ( variance * variance ) - 3.0;
      }

    unsigned int i = 0;
    out[i++] = mean;
    out[i++] = m_Map.begin()->first;
    out[i++] = m_Map.rbegin()->first;
    out[i++] = variance;
    out[i++] = sigma;
    out[i++] = skewness;
    out[i++] = kurtosis;
    out[i++] = entropy;
    return out;
  }

  void AddBoundary(){}

  void RemoveBoundary(){}

private:
  typedef typename std::map< TInputPixel, size_t > MapType;

  MapType       m_Map;
  size_t        m_Count;
};

} // end namespace Function
} // end namespace itk
#endif