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;
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;
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)
AimsThreshold(threshold_t type, T level, T level2=0, T backgd=0, U foregd=(U)(!std::numeric_limits< U >::is_specialized||(std::numeric_limits< U >::max() >=32767) ? 32767 :std::numeric_limits< U >::max()))