14 #ifndef AIMS_UTILITY_EMTHRESHOLD_H 15 #define AIMS_UTILITY_EMTHRESHOLD_H 27 template <
class T,
class U>
64 template <
class T,
class U>
inline 66 T backgd,
float foregd,
68 int levelindex,
int levelindex2,
73 _levelindex(levelindex),
74 _level2index(levelindex2),
85 std::cout <<
"Classes " <<
carto::toString(_classes) <<
" ..." << std::endl << std::flush;
86 std::cout <<
"Level index " <<
carto::toString(_levelindex) <<
" ..." << std::endl << std::flush;
87 std::cout <<
"Level 2 index " <<
carto::toString(_level2index) <<
" ..." << std::endl << std::flush;
92 template <
class T,
class U>
inline 95 std::vector<T> thresholds, thresholds_mu;
102 bins = int(maxi - mini);
107 binend = _binend + 1;
110 std::cout <<
"Bins " <<
carto::toString(bins) <<
" ..." << std::endl << std::flush;
125 double scl = (double) bins / (
double)( maxi - mini );
127 double points_count = 0.;
128 for (n = _binstart; n < binend; ++n)
129 points_count += his.
data()(n);
133 double class_points_count = 0.;
134 for (n = _binstart; n < binend; ++n)
136 class_points_count += his.
data()(n);
137 for (i = 0; i < _classes; ++i)
139 if ((i <= (_classes * class_points_count / points_count))
140 && (i >= (_classes * class_points_count / points_count) - 1))
149 double sum1 = 0, sum2 = 0,
sum = 0, sum_aux;
150 while ( count <= (_classes + 1) )
159 for (n = _binstart; n < binend; ++n)
161 for (i = 0; i < _classes; ++i)
165 for (j = 0; j < _classes; ++j)
167 aux(j) = th_pi(j) * (1 / sqrt(2 *
M_PI * th_sigma(j)))
168 * exp(-(1 / (2 * th_sigma(j))) * (n - th_mu(j)) * (n - th_mu(j)));
171 th_tau(n, i) = aux(i) / sum_aux;
177 th_mu = 0.; th_sigma = 0.; th_pi = 0.;
179 for (j = 0; j < _classes; ++j)
185 for (n = _binstart; n < binend; ++n) {
186 sum += th_tau(n, j) * his.
data()(n);
187 sum1 += th_tau(n, j) * n * his.
data()(n);
189 th_mu(j) = sum1 /
sum;
191 for (n = _binstart; n < binend; ++n)
192 sum2 += th_tau(n, j) * (n - th_mu(j)) * (n - th_mu(j)) * his.
data()(n);
193 th_sigma(j) = sum2 /
sum;
198 for (n = _binstart; n < binend; ++n)
200 sum1 += th_tau(n, j) * his.
data()(n);
203 th_pi(j) = sum1 /
sum;
209 for (j = 0; j < _classes; ++j)
216 else if (level > (binend - 1))
219 thresholds_mu.push_back(level);
222 std::sort(thresholds_mu.begin(), thresholds_mu.end());
225 std::cout <<
"Found class means : [";
227 for (i = 0; i < _classes; ++i)
231 if (i < (_classes - 1))
234 std::cout <<
"]" << std::endl << std::flush;
238 std::cout <<
"Found thresholds : [";
240 for (i = 0; i < (_classes - 1); ++i)
243 level = thresholds_mu[i];
244 for(
int j = thresholds_mu[i]; j <= thresholds_mu[i + 1]; j++)
246 if (his.
data()(j) < his.
data()(level))
250 thresholds.push_back(((T)(level / scl) + mini));
254 if (i < (_classes - 2))
260 std::cout <<
"]" << std::endl << std::flush;
265 template <
class T,
class U>
inline 270 if (_levelindex < thresholds.size())
273 if (_level2index < thresholds.size())
282 template <
class T,
class U>
inline 287 if (_levelindex < thresholds.size())
290 if (_level2index < thresholds.size())
AimsData< U > bin(const AimsData< T > &image)
Return the binary thresholded image.
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)
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
Histogram container class, with a specified number of regular bins.
AimsData< int32_t > & data()
return a reference to the data field of the histogram class.
std::vector< T > processEMThresholds(const AimsData< T > &image)
AimsData< T > operator()(const AimsData< T > &image)
Return the multi-level thresholded image.
AimsData< U > bin(const AimsData< T > &sqv)
void doit(const AimsData< T > &thing)
classical histogram computation function.
virtual ~AimsEMThreshold()
AimsData< T > operator()(const AimsData< T > &sqv)
std::string toString(const T &object)