Java -> C++
Code
int Otsu(float* histo, int n_length)
{
// Otsu's threshold algorithm
// M. Emre Celebi 6.15.2007, Fourier Library https://sourceforge.net/projects/fourier-ipal/
// ported to ImageJ plugin by G.Landini
int ih;
int threshold = -1;
int num_pixels = 0;
double total_mean; /* mean gray-level for the whole image */
double bcv, term; /* between-class variance, scaling term */
double max_bcv; /* max BCV */
double* cnh = nullptr;
double* mean = nullptr;
double* normal_histo = nullptr;
cnh = new double[n_length]; /* cumulative normalized histogram */
mean = new double[n_length]; /* mean gray-level */
normal_histo = new double[n_length]; /* normalized histogram */
/* Calculate total numbre of pixels */
for (ih = 0; ih < n_length; ih++)
num_pixels = num_pixels + histo[ih];
term = 1.0 / (double)num_pixels;
/* Calculate the normalized histogram */
for (ih = 0; ih < n_length; ih++) {
normal_histo[ih] = term * histo[ih];
}
/* Calculate the cumulative normalized histogram */
cnh[0] = normal_histo[0];
for (ih = 1; ih < n_length; ih++) {
cnh[ih] = cnh[ih - 1] + normal_histo[ih];
}
mean[0] = 0.0;
for (ih = 0 + 1; ih < n_length; ih++) {
mean[ih] = mean[ih - 1] + ih * normal_histo[ih];
}
total_mean = mean[n_length - 1];
// Calculate the BCV at each gray-level and find the threshold that maximizes it
threshold = -DBL_MAX;
max_bcv = 0.0;
for (ih = 0; ih < n_length; ih++) {
bcv = total_mean * cnh[ih] - mean[ih];
bcv *= bcv / (cnh[ih] * (1.0 - cnh[ih]));
if (max_bcv < bcv) {
max_bcv = bcv;
threshold = ih;
}
}
if (cnh)
delete[] cnh;
if (mean)
delete[] mean;
if (normal_histo)
delete[] normal_histo;
return threshold;
}
imageJ 관련 문서 : https://imagej.net/plugins/auto-threshold
github link : https://github.com/fiji/Auto_Threshold/blob/master/src/main/java/fiji/threshold/Auto_Threshold.java
'영상처리' 카테고리의 다른 글
AutoThreshold (RenyiEntropy) C++ (2) | 2024.12.11 |
---|---|
AutoThreshold (Percentile) C++ (0) | 2024.12.11 |
AutoThreshold (Moments) C++ (0) | 2024.12.11 |
AutoThreshold (Minimum) C++ (1) | 2024.12.11 |
AutoThreshold (MinErrorI) C++ (0) | 2024.12.11 |