aimsalgo 6.0.0
Neuroimaging image processing
regularBinnedHisto.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_REGULARBINNEDHISTO_H
36#define AIMS_HISTOGRAM_REGULARBINNEDHISTO_H
37
39#include <cartobase/containers/nditerator.h>
40#include <vector>
41#include <map>
42
43
44namespace aims
45{
46
49 template< typename T>
51 {
52 public:
53
54 RegularBinnedHistogram( unsigned bins = 0 );
57
60 void doit( const carto::rc_ptr<carto::Volume<T> > & thing );
61 // same but specify min/max to avoid seaching extrema
62 void doit( const carto::rc_ptr<carto::Volume<T> > & thing,
63 T mindataval, T maxdataval );
64 unsigned bins() const { return _bins; }
65 void setBins( unsigned bins );
66 T minDataValue() const { return _minvalue; }
67 T maxDataValue() const { return _maxvalue; }
68 std::vector<T> *unique( const carto::rc_ptr<carto::Volume<T> > & thing,
69 size_t abort_max = 0 ) const;
70
71 private:
72 unsigned _bins;
73 T _minvalue;
74 T _maxvalue;
75 };
76
77
78 template< typename T> inline
80 : Histogram<T>(), _bins( bins ), _minvalue( 0 ), _maxvalue( 0 )
81 {
82 }
83
84
85 template< typename T> inline
87 const RegularBinnedHistogram<T>& other )
88 : Histogram< T >( other ), _bins( other._bins ),
89 _minvalue( other._minvalue ), _maxvalue( other._maxvalue )
90 {
91 }
92
93
94 template< typename T> inline
96 {
97 _bins = bins;
99 }
100
101
102 template< typename T > inline
104 const carto::rc_ptr<carto::Volume<T> > & thing )
105 {
106 doit( thing, thing->min(), thing->max() );
107 }
108
109
110 template< typename T > inline
112 const carto::rc_ptr<carto::Volume<T> > & thing, T mini, T maxi )
113 {
114 _minvalue = mini;
115 _maxvalue = maxi;
116
117 if( _bins == 0 )
118 {
119 _bins = 256;
120 }
121
122 this->_data = carto::VolumeRef<int32_t>( _bins );
123 this->_data.fill( 0 );
124 double scl = (double) _bins / (double) ( maxi - mini );
125 double x;
126 int y;
127
128 const T *iv, *pp;
129
130 carto::const_line_NDIterator<T> it( &thing->at( 0 ), thing->getSize(),
131 thing->getStrides(), true );
132 for( ; !it.ended(); ++it )
133 {
134 iv = &*it;
135 for( pp=iv + it.line_length(); iv!=pp; it.inc_line_ptr( iv ) )
136 {
137 x = (double) ( (double) (*iv) - mini ) * scl;
138 y = (int) x;
139 if( y < 0 )
140 {}
141 else if( y >= (int) _bins )
142 {
143 if( x == (int) _bins )
144 ++this->_data( _bins-1 );
145 }
146 else
147 ++this->_data( y );
148 }
149 }
150 }
151
152
153 template< typename T > inline
154 std::vector<T> *
156 const carto::rc_ptr<carto::Volume<T> > & thing, size_t abort_max ) const
157 {
158 std::map<T, unsigned> vals;
159 // std::cout << "unique...\n";
160 // clock_t t0 = clock();
161 size_t n = 0;
162 const T *iv, *pp;
163
164 carto::const_line_NDIterator<T> it( &thing->at( 0 ), thing->getSize(),
165 thing->getStrides(), true );
166 for( ; !it.ended(); ++it )
167 {
168 iv = &*it;
169 for( pp=iv + it.line_length(); iv!=pp; it.inc_line_ptr( iv ) )
170 {
171 ++vals[*iv];
172 if( abort_max != 0 )
173 {
174 ++n;
175 if( n % 1000 == 0 && vals.size() >= abort_max )
176 throw std::runtime_error( "too many values" );
177 }
178 }
179 }
180 // std::cout << "unique map done in " << float(clock() - t0) / CLOCKS_PER_SEC << "s: " << thing->getSizeX() * thing->getSizeY() * thing->getSizeZ.dimZ() * thing->getSizeT() * CLOCKS_PER_SEC / float(clock() - t0) << " vox/s.\n";
181 std::vector<T> *res = new std::vector<T>( vals.size() );
182 typename std::map<T, unsigned>::iterator im, e = vals.end();
183 typename std::vector<T>::iterator i = res->begin();
184 for( im=vals.begin(); im!=e; ++im, ++i )
185 *i = im->first;
186 // std::cout << "unique vector done.\n";
187 return res;
188 }
189
190}
191
192#endif
carto::VolumeRef< int32_t > _data
histogram datas
Definition histogram.h:117
Histogram()
constructor.
Definition histogram.h:122
std::vector< T > * unique(const carto::rc_ptr< carto::Volume< T > > &thing, size_t abort_max=0) const
RegularBinnedHistogram(unsigned bins=0)
void doit(const carto::rc_ptr< carto::Volume< T > > &thing)
classical histogram computation function.
std::ptrdiff_t line_length() const
void inc_line_ptr(const T *&p) const