aimsalgo 6.0.0
Neuroimaging image processing
simpleHisto.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34
35#ifndef AIMS_HISTOGRAM_SIMPLEHISTO_H
36#define AIMS_HISTOGRAM_SIMPLEHISTO_H
37
39#include <cartobase/containers/nditerator.h>
40
43template< class T>
44class SimpleHistogram : public Histogram<T>
45{
46 public:
47
50
54 : Histogram< T >( other ) { }
55
56 virtual ~SimpleHistogram() { }
58
64 void doit( const carto::rc_ptr<carto::Volume<T> > & thing );
65
68
69 void rebin( int size );
76 void rebin( int size, carto::Volume<int32_t>::iterator beg,
79};
80
81
82template< class T > inline
84{
85 carto::VolumeRef<int32_t> res( 1 << 8 * sizeof( T ) );
86
87 this->_minValid = (int)thing->min();
88 this->_maxValid = (int)thing->max();
89
90 this->_nPoints = thing->getSizeX() * thing->getSizeY() * thing->getSizeZ()
91 * thing->getSizeT();
92
93 const T *p, *pp;
94 carto::const_line_NDIterator<T> it( &thing->at( 0 ), thing->getSize(),
95 thing->getStrides(), true );
96 for( ; !it.ended(); ++it )
97 {
98 p = &*it;
99 for( pp=p + it.line_length(); p!=pp; it.inc_line_ptr( p ) )
100 res->at( (int)*p - this->_minValid )++;
101 }
102
103 this->_data = res;
104}
105
106
107template< class T > inline
109{
110 rebin( size, this->_data.begin() , this->_data.end() );
111}
112
113
114template< class T > inline
117{
118 rebin( &*end - &*beg, beg, end );
119}
120
121
122template< class T > inline
125{
126 int i, sum, length = &*end - &*beg;
127
128 ASSERT( size <= length );
129
130 int coef = (int)ceil( (double)length / (double)size );
131 carto::VolumeRef<int32_t> res( size );
132
133 this->_nPoints = 0;
134
136 for ( it2=res.begin(); it2!=res.end(); )
137 {
138 sum = 0;
139
140 for ( i = coef; i-- && it != end; )
141 sum += *it++;
142
143 *it2++ = sum;
144 this->_nPoints += sum;
145 }
146
147 int tmpMin = this->_minValid;
148
149 this->_minValid = this->_maxValid = tmpMin /coef;
150
151 for ( it2 = res.begin(), i=0; *it2 == 0 && it2 != res.end(); it2++, ++i )
152 this->_minValid = i + tmpMin / coef;
153
154 for ( ; it2 != res.end(); it2++, ++i )
155 if (*it2)
156 this->_maxValid = i + tmpMin / coef;
157
158 this->_data = res;
159}
160
161
162template <>
163inline
165 const carto::rc_ptr<carto::Volume<float> > & thing )
166{
167 int size = 65536;
168 carto::VolumeRef<int32_t> res( size );
169
170 float minh = thing->min();
171 float maxh = thing->max();
172 float div = maxh - minh;
173
174 if ( ( div < (float)size ) && ( div > 1.0f ) )
175 {
176 div = (float)size;
177 this->_minValid = (int)minh;
178 this->_maxValid = (int)maxh;
179 }
180 else
181 {
182 this->_minValid = 0;
183 this->_maxValid = size - 1;
184 }
185
186 this->_nPoints = thing->getSizeX() * thing->getSizeY() * thing->getSizeZ()
187 * thing->getSizeT();
188
189 const float *p, *pp;
190 carto::const_line_NDIterator<float> it( &thing->at( 0 ), thing->getSize(),
191 thing->getStrides(), true );
192 for( ; !it.ended(); ++it )
193 {
194 p = &*it;
195 for( pp=p + it.line_length(); p!=pp; it.inc_line_ptr( p ) )
196 {
197 int x = (int)( (float) size * ( *p - minh ) / div );
198 if( x < 0 )
199 x = 0;
200 else if( x >= size )
201 x = size - 1;
202 res.at( x )++;
203 }
204 }
205
206 this->_data = res;
207}
208
209
210template <>
211inline
213 const carto::rc_ptr<carto::Volume<double> > & thing )
214{
215 int size = 65536;
216 carto::VolumeRef<int32_t> res( size );
217
218 double minh = thing->min();
219 double maxh = thing->max();
220 double div = maxh - minh;
221
222 if ( ( div < (float)size ) && ( div > 1.0f ) )
223 {
224 div = (double)size;
225 this->_minValid = (int)minh;
226 this->_maxValid = (int)maxh;
227 }
228 else
229 {
230 this->_minValid = 0;
231 this->_maxValid = size - 1;
232 }
233
234 this->_nPoints = thing->getSizeX() * thing->getSizeY() * thing->getSizeZ()
235 * thing->getSizeT();
236
237 const double *p, *pp;
238 carto::const_line_NDIterator<double> it( &thing->at( 0 ), thing->getSize(),
239 thing->getStrides(), true );
240 for( ; !it.ended(); ++it )
241 {
242 p = &*it;
243 for( pp=p + it.line_length(); p!=pp; it.inc_line_ptr( p ) )
244 res.at( (int)( (double)size * ( *p - minh ) / div ) )++;
245 }
246
247 this->_data = res;
248}
249
250#endif
#define ASSERT(EX)
carto::VolumeRef< int32_t > _data
histogram datas
Definition histogram.h:117
int _minValid
minimum used value
Definition histogram.h:112
Histogram()
constructor.
Definition histogram.h:122
int _nPoints
total number of points
Definition histogram.h:110
int _maxValid
maximum used value
Definition histogram.h:114
SimpleHistogram()
constructor. Does nothing.
Definition simpleHisto.h:51
SimpleHistogram(const SimpleHistogram< T > &other)
copy constructor.
Definition simpleHisto.h:53
void doit(const carto::rc_ptr< carto::Volume< T > > &thing)
classical histogram computation function.
Definition simpleHisto.h:83
virtual ~SimpleHistogram()
destructor.
Definition simpleHisto.h:56
void rebin(int size)
rebinning to a specific result vector size.
iterator end()
const T & at(long x, long y=0, long z=0, long t=0) const
iterator begin()
std::ptrdiff_t line_length() const
void inc_line_ptr(const T *&p) const
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)