11 #ifndef PRIMATOLOGIST_PROBA_LABELS_H 12 #define PRIMATOLOGIST_PROBA_LABELS_H 15 #include <cartodata/volume/volume.h> 26 template <
typename ProbaType>
27 void prepare( carto::VolumeRef<ProbaType> & proba,
28 carto::VolumeRef<ProbaType> & nbprior,
29 std::vector<int> & labels,
30 std::map<int,int> & indices,
31 const std::vector<int> & ignore,
36 new_size = proba.getSizeT();
41 int prev_size = labels.size();
42 labels.resize(new_size);
43 for(
int t = prev_size; t < new_size; ++t )
47 std::multimap<int,int> pre_indices;
48 for(
int t = 0; t < labels.size(); ++t )
49 pre_indices.insert( std::pair<int,int>(labels[t], t) );
52 std::vector<int>::const_iterator ig;
53 for( ig = ignore.begin(); ig != ignore.end(); ++ig )
55 std::multimap<int,int>::const_iterator i;
56 std::pair<std::multimap<int,int>::const_iterator, std::multimap<int,int>::const_iterator> ii;
57 ii = pre_indices.equal_range(*ig);
58 for( i = ii.first; i != ii.second; ++i )
65 new_size = labels.size();
67 for(
int t = 0; t < labels.size(); ++t )
73 if( seen.count(labels[t]) )
76 seen.insert(labels[t]);
80 if( new_size == labels.size() )
86 carto::VolumeRef<ProbaType> new_proba( proba.getSizeX(), proba.getSizeY(),
87 proba.getSizeZ(), new_size );
88 new_proba->copyHeaderFrom( proba.header() );
91 std::vector<int> old_labels = labels;
94 for(
int t = 0; t < old_labels.size(); ++t )
98 if( ! indices.count(old_labels[t]) )
100 indices[old_labels[t]] = labels.size();
101 labels.push_back( old_labels[t] );
103 pre_indices.insert( std::pair<int,int>(indices[old_labels[t]], t) );
104 for(
int z = 0; z < proba.getSizeZ(); ++z )
105 for(
int y = 0; y < proba.getSizeY(); ++y )
106 for(
int x = 0; x < proba.getSizeX(); ++x )
107 new_proba(x, y, z, indices[old_labels[t]]) += proba(x, y, z, t);
116 std::multimap<int,int>::const_iterator i, j;
117 std::pair<std::multimap<int,int>::const_iterator, std::multimap<int,int>::const_iterator> ii, jj;
118 carto::VolumeRef<ProbaType> new_nbprior(new_size, new_size);
119 new_nbprior->fill(0.);
120 for(
int t = 0; t < new_size; ++t ) {
121 ii = pre_indices.equal_range(t);
122 for( i = ii.first; i != ii.second; ++i ) {
123 for(
int u = 0; u < new_size; ++u ) {
124 jj = pre_indices.equal_range(u);
125 for( j = jj.first; j != jj.second; ++j )
127 new_nbprior(t, u) += nbprior(i->second, j->second);
134 for(
int t = 0; t < new_size; ++t ) {
136 for(
int u = 0; u < new_size; ++u )
137 sum += new_nbprior(t, u);
138 for(
int u = 0; u < new_size; ++u )
139 new_nbprior(t, u) /=
sum;
142 nbprior = new_nbprior;
147 template <
typename ProbaType>
149 carto::VolumeRef<ProbaType> & nbprior,
150 double probacatch = 1. )
152 if( probacatch >= 1. )
157 int nclass_old = proba.getSizeT();
158 int nclass_new = nclass_old + 1;
160 proba->reallocate( proba.getSizeX(), proba.getSizeY(), proba.getSizeZ(),
161 proba.getSizeT() + 1, true );
164 for(
int z = 0; z < proba.getSizeZ(); ++z )
165 for(
int y = 0; y < proba.getSizeY(); ++y )
166 for(
int x = 0; x < proba.getSizeX(); ++x )
168 proba(x, y, z, proba.getSizeT() - 1) = probacatch;
173 if( nbprior.get() ) {
175 int nclass_old = proba.getSizeT();
176 int nclass_new = nclass_old + 1;
179 for(
int j = 0; j < nbprior.getSizeY(); ++j )
182 for(
int i = 0; i < nbprior.getSizeX(); ++i )
183 sum += nbprior(i, j);
184 for(
int i = 0; i < nbprior.getSizeX(); ++i ) {
186 nbprior(i, j) /=
sum;
191 carto::VolumeRef<ProbaType> old_nbprior(
new carto::Volume<ProbaType>( *nbprior ) );
193 nbprior->reallocate( nclass_new, nclass_new );
194 for(
int j = 0; j < nclass_old; ++j )
195 for(
int i = 0; i < nclass_old; ++i )
196 nbprior(i, j) = old_nbprior(i, j);
198 for(
int i = 0; i < nclass_old; ++i ) {
199 nbprior( nclass_old, i ) = 0;
200 nbprior( i, nclass_old ) = .25 / nclass_old;
202 nbprior( nclass_old, nclass_old ) = 0.75;
210 #endif // PRIMATOLOGIST_PROBA_LABELS_H
void addProbaCatch(carto::VolumeRef< ProbaType > &proba, carto::VolumeRef< ProbaType > &nbprior, double probacatch=1.)
carto::DataTypeTraits< T >::LongType sum(const carto::VolumeRef< T > &in, const carto::VolumeRef< M > &mask)
void prepare(carto::VolumeRef< ProbaType > &proba, carto::VolumeRef< ProbaType > &nbprior, std::vector< int > &labels, std::map< int, int > &indices, const std::vector< int > &ignore, int nclass=0)
carto::VolumeRef< P > & normalize(carto::VolumeRef< P > &proba, const carto::VolumeRef< M > &mask)