primatologist-gpl 6.0.4
labels.h
Go to the documentation of this file.
1/* Copyright (C) 2000-2013 CEA
2 *
3 * This software and supporting documentation were developed by
4 * bioPICSEL
5 * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6 * 18, route du Panorama
7 * 92265 Fontenay-aux-Roses
8 * France
9 */
10
11#ifndef PRIMATOLOGIST_PROBA_LABELS_H
12#define PRIMATOLOGIST_PROBA_LABELS_H
13
15#include <cartodata/volume/volume.h>
16#include <map>
17#include <vector>
18
19namespace aims {
20namespace proba {
21
22 namespace internal {
23 const int TO_REMOVE = -3000;
24 }
25
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,
32 int nclass = 0 )
33 {
34 int new_size;
35 if( proba.get() )
36 new_size = proba.getSizeT();
37 else
38 new_size = nclass;
39
40 // initialize labels
41 int prev_size = labels.size();
42 labels.resize(new_size);
43 for( int t = prev_size; t < new_size; ++t )
44 labels[t] = t;
45
46 // initialize indices
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) );
50
51 // manage ignored labels
52 std::vector<int>::const_iterator ig;
53 for( ig = ignore.begin(); ig != ignore.end(); ++ig )
54 {
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 )
59 {
60 labels[i->second] = internal::TO_REMOVE;
61 }
62 }
63
64 // compute new size
65 new_size = labels.size();
66 std::set<int> seen;
67 for( int t = 0; t < labels.size(); ++t )
68 {
69 if( labels[t] == internal::TO_REMOVE )
70 --new_size;
71 else
72 {
73 if( seen.count(labels[t]) )
74 --new_size;
75 else
76 seen.insert(labels[t]);
77 }
78 }
79
80 if( new_size == labels.size() )
81 return;
82
83 // compute new proba
84 if( proba.get() )
85 {
86 carto::VolumeRef<ProbaType> new_proba( proba.getSizeX(), proba.getSizeY(),
87 proba.getSizeZ(), new_size );
88 new_proba->copyHeaderFrom( proba.header() );
89 new_proba->fill(0.);
90
91 std::vector<int> old_labels = labels;
92 labels.resize(0);
93 pre_indices.clear();
94 for( int t = 0; t < old_labels.size(); ++t )
95 {
96 if( old_labels[t] != internal::TO_REMOVE )
97 {
98 if( ! indices.count(old_labels[t]) )
99 {
100 indices[old_labels[t]] = labels.size();
101 labels.push_back( old_labels[t] );
102 }
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);
108 }
109 }
110
111 proba = new_proba;
112 }
113
114 if( nbprior.get() )
115 {
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 )
126 {
127 new_nbprior(t, u) += nbprior(i->second, j->second);
128 }
129 }
130 }
131 }
132
133 double sum;
134 for( int t = 0; t < new_size; ++t ) {
135 sum = 0.;
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;
140 }
141
142 nbprior = new_nbprior;
143 }
144 }
145
146
147 template <typename ProbaType>
148 void addProbaCatch( carto::VolumeRef<ProbaType> & proba,
149 carto::VolumeRef<ProbaType> & nbprior,
150 double probacatch = 1. )
151 {
152 if( probacatch >= 1. )
153 probacatch = 0.01;
154
155 if( proba.get() ) {
156
157 int nclass_old = proba.getSizeT();
158 int nclass_new = nclass_old + 1;
159
160 proba->reallocate( proba.getSizeX(), proba.getSizeY(), proba.getSizeZ(),
161 proba.getSizeT() + 1, true );
163
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 )
167 {
168 proba(x, y, z, proba.getSizeT() - 1) = probacatch;
169 }
170
171 }
172
173 if( nbprior.get() ) {
174
175 int nclass_old = proba.getSizeT();
176 int nclass_new = nclass_old + 1;
177
178 // pre normalize
179 for( int j = 0; j < nbprior.getSizeY(); ++j )
180 {
181 double sum = 0.;
182 for( int i = 0; i < nbprior.getSizeX(); ++i )
183 sum += nbprior(i, j);
184 for( int i = 0; i < nbprior.getSizeX(); ++i ) {
185 if( sum > 0. )
186 nbprior(i, j) /= sum; // / (1. - probacatch);
187 }
188 }
189
190 // save data
191 carto::VolumeRef<ProbaType> old_nbprior( new carto::Volume<ProbaType>( *nbprior ) );
192 // reallocate
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);
197
198 for( int i = 0; i < nclass_old; ++i ) {
199 nbprior( nclass_old, i ) = 0;
200 nbprior( i, nclass_old ) = .25 / nclass_old;
201 }
202 nbprior( nclass_old, nclass_old ) = 0.75;
203
204 }
205 }
206
207} // namespace proba
208} // namespace aims
209
210#endif // PRIMATOLOGIST_PROBA_LABELS_H
const int TO_REMOVE
Definition labels.h:23
carto::VolumeRef< P > & normalize(carto::VolumeRef< P > &proba, const carto::VolumeRef< M > &mask)
Definition proba_d.h:151
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)
Definition labels.h:27
void addProbaCatch(carto::VolumeRef< ProbaType > &proba, carto::VolumeRef< ProbaType > &nbprior, double probacatch=1.)
Definition labels.h:148