14 #ifndef AIMS_UTILITY_EMTHRESHOLD_H
15 #define AIMS_UTILITY_EMTHRESHOLD_H
22 template <
class T,
class U>
59 template <
class T,
class U>
inline
61 T backgd,
float foregd,
63 int levelindex,
int levelindex2,
68 _levelindex(levelindex),
69 _level2index(levelindex2),
80 std::cout <<
"Classes " <<
carto::toString(_classes) <<
" ..." << std::endl << std::flush;
81 std::cout <<
"Level index " <<
carto::toString(_levelindex) <<
" ..." << std::endl << std::flush;
82 std::cout <<
"Level 2 index " <<
carto::toString(_level2index) <<
" ..." << std::endl << std::flush;
87 template <
class T,
class U>
inline
91 std::vector<T> thresholds, thresholds_mu;
96 T maxi = image.
max(), mini = image.
min();
98 bins = int(maxi - mini);
103 binend = _binend + 1;
106 std::cout <<
"Bins " <<
carto::toString(bins) <<
" ..." << std::endl << std::flush;
121 double scl = (double) bins / (
double)( maxi - mini );
123 double points_count = 0.;
124 for (n = _binstart; n < binend; ++n)
125 points_count += his.
data()(n);
129 double class_points_count = 0.;
130 for (n = _binstart; n < binend; ++n)
132 class_points_count += his.
data()(n);
133 for (i = 0; i < _classes; ++i)
135 if ((i <= (_classes * class_points_count / points_count))
136 && (i >= (_classes * class_points_count / points_count) - 1))
145 double sum1 = 0, sum2 = 0,
sum = 0, sum_aux;
146 while ( count <= (_classes + 1) )
155 for (n = _binstart; n < binend; ++n)
157 for (i = 0; i < _classes; ++i)
161 for (j = 0; j < _classes; ++j)
163 aux(j) = th_pi(j) * (1 / sqrt(2 *
M_PI * th_sigma(j)))
164 * exp(-(1 / (2 * th_sigma(j))) * (n - th_mu(j)) * (n - th_mu(j)));
167 th_tau(n, i) = aux(i) / sum_aux;
173 th_mu = 0.; th_sigma = 0.; th_pi = 0.;
175 for (j = 0; j < _classes; ++j)
181 for (n = _binstart; n < binend; ++n) {
182 sum += th_tau(n, j) * his.
data()(n);
183 sum1 += th_tau(n, j) * n * his.
data()(n);
185 th_mu(j) = sum1 /
sum;
187 for (n = _binstart; n < binend; ++n)
188 sum2 += th_tau(n, j) * (n - th_mu(j)) * (n - th_mu(j)) * his.
data()(n);
189 th_sigma(j) = sum2 /
sum;
194 for (n = _binstart; n < binend; ++n)
196 sum1 += th_tau(n, j) * his.
data()(n);
199 th_pi(j) = sum1 /
sum;
205 for (j = 0; j < _classes; ++j)
212 else if (level > (binend - 1))
215 thresholds_mu.push_back(level);
218 std::sort(thresholds_mu.begin(), thresholds_mu.end());
221 std::cout <<
"Found class means : [";
223 for (i = 0; i < _classes; ++i)
227 if (i < (_classes - 1))
230 std::cout <<
"]" << std::endl << std::flush;
234 std::cout <<
"Found thresholds : [";
236 for (i = 0; i < (_classes - 1); ++i)
239 level = thresholds_mu[i];
240 for(
int j = thresholds_mu[i]; j <= thresholds_mu[i + 1]; j++)
242 if (his.
data()(j) < his.
data()(level))
246 thresholds.push_back(((T)(level / scl) + mini));
250 if (i < (_classes - 2))
256 std::cout <<
"]" << std::endl << std::flush;
261 template <
class T,
class U>
inline
266 std::vector<T> thresholds = processEMThresholds(image);
267 if (_levelindex < thresholds.size())
270 if (_level2index < thresholds.size())
279 template <
class T,
class U>
inline
284 std::vector<T> thresholds = processEMThresholds(image);
285 if (_levelindex < thresholds.size())
288 if (_level2index < thresholds.size())
AimsEMThreshold(threshold_t type, T backgd=0, float foregd=32767, int classes=2, int levelindex=0, int level2index=1, int bins=0, int binstart=0, int binend=0)
virtual ~AimsEMThreshold()
std::vector< T > processEMThresholds(const carto::VolumeRef< T > &image)
carto::VolumeRef< U > operator()(const carto::VolumeRef< T > &image)
Return the multi-level thresholded image.
carto::VolumeRef< U > bin(const carto::VolumeRef< T > &image)
Return the binary thresholded image.
carto::VolumeRef< U > bin(const carto::VolumeRef< T > &sqv)
carto::VolumeRef< T > operator()(const carto::VolumeRef< T > &sqv)
carto::VolumeRef< int32_t > & data()
return a reference to the data field of the histogram class.
Histogram container class, with a specified number of regular bins.
void doit(const carto::rc_ptr< carto::Volume< T > > &thing)
classical histogram computation function.
std::string toString(const T &object)
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)