-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotsuFuncs.cpp
More file actions
138 lines (120 loc) · 4.48 KB
/
otsuFuncs.cpp
File metadata and controls
138 lines (120 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "otsuFuncs.h"
double* calculateProbabilities(const PnmImage& image, const bool ompEnabled)
{
auto* probability = new double[INTENSITY_LAYER_COUNT];
memset(probability, 0.0, INTENSITY_LAYER_COUNT * sizeof(double));
#pragma omp parallel if (ompEnabled) default(none) shared(probability, image)
{
auto* localProbability = new double[INTENSITY_LAYER_COUNT];
memset(localProbability, 0.0, INTENSITY_LAYER_COUNT * sizeof(double));
#pragma omp for schedule(static)
for (int y = 0; y < image.getYSize(); ++y)
{
for (int x = 0; x < image.getXSize(); ++x)
{
++localProbability[image.getPixel(x, y)];
}
}
#pragma omp critical
{
for (int i = 0; i < INTENSITY_LAYER_COUNT; ++i)
{
probability[i] += localProbability[i];
}
}
delete[] localProbability;
}
const int totalPixelCount = image.getXSize() * image.getYSize();
for (int i = 0; i < INTENSITY_LAYER_COUNT; ++i)
{
probability[i] /= totalPixelCount;
}
return probability;
}
double* calculatePrefOmegas(const double* probability)
{
auto* omega = new double[INTENSITY_LAYER_COUNT];
omega[0] = probability[0];
for (int i = 1; i < INTENSITY_LAYER_COUNT; ++i)
{
omega[i] = omega[i - 1] + probability[i];
}
return omega;
}
double* calculatePrefMus(const double* probability)
{
auto* mu = new double[INTENSITY_LAYER_COUNT];
mu[0] = 0.0;
for (int i = 1; i < INTENSITY_LAYER_COUNT; ++i)
{
mu[i] = mu[i - 1] + i * probability[i];
}
return mu;
}
double getPrefOmegaRange(const double* omega, const int left, const int right)
{
return omega[right] - (left >= 0 ? omega[left] : 0.0);
}
double getPrefMuRange(const double* mu, const int left, const int right)
{
return mu[right] - (left >= 0 ? mu[left] : 0.0);
}
double calculateSigmaForClass(const double* prefOmega, const double* prefMu, const int left, const int right)
{
const double omegaRange = getPrefOmegaRange(prefOmega, left, right);
const double muRange = getPrefMuRange(prefMu, left, right);
return muRange * muRange / omegaRange;
}
std::vector<int> calculateOtsuThresholds(const PnmImage& image, const bool ompEnabled)
{
const auto* probability = calculateProbabilities(image, ompEnabled);
const auto* prefOmega = calculatePrefOmegas(probability);
const auto* prefMu = calculatePrefMus(probability);
std::vector<std::pair<double, std::vector<int>>> results;
#pragma omp parallel if (ompEnabled) default(none) shared(prefOmega, prefMu, results)
{
double localBestSigma = 0.0;
std::vector<int> localBestThresholds(3);
#pragma omp for schedule(dynamic)
for (int i = 1; i < INTENSITY_LAYER_COUNT - 3; ++i)
{
const double firstClassSigma = calculateSigmaForClass(prefOmega, prefMu, -1, i);
for (int j = i + 1; j < INTENSITY_LAYER_COUNT - 2; ++j)
{
const double secondClassSigma = calculateSigmaForClass(prefOmega, prefMu, i, j);
for (int k = j + 1; k < INTENSITY_LAYER_COUNT - 1; ++k)
{
const double thirdClassSigma = calculateSigmaForClass(prefOmega, prefMu, j, k);
const double fourthClassSigma = calculateSigmaForClass(
prefOmega, prefMu, k, INTENSITY_LAYER_COUNT - 1);
const double curSigma = firstClassSigma + secondClassSigma + thirdClassSigma + fourthClassSigma;
if (curSigma > localBestSigma)
{
localBestSigma = curSigma;
localBestThresholds[0] = i;
localBestThresholds[1] = j;
localBestThresholds[2] = k;
}
}
}
}
#pragma omp critical
{
results.emplace_back(localBestSigma, localBestThresholds);
}
}
double overallBestSigma = 0.0;
int overallBestIndex = 0;
for (int i = 0; i < results.size(); ++i)
{
if (results[i].first > overallBestSigma)
{
overallBestSigma = results[i].first;
overallBestIndex = i;
}
}
delete[] probability;
delete[] prefOmega;
delete[] prefMu;
return results[overallBestIndex].second;
}