Repository navigation
Expand file tree
/
Copy pathOtsuThreshold.cpp
More file actions
239 lines (197 loc) · 6.42 KB
/
Copy pathOtsuThreshold.cpp
File metadata and controls
239 lines (197 loc) · 6.42 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "OtsuThreshold.h"
void OtsuThreshold::greyscaleImage(ImageData* inputData, ImageData* out)
{
out->size = inputData->size;
out->width = inputData->width;
out->height = inputData->height;
out->channels = inputData->channels;
unsigned char luma = 0;
for (int i = 0; i < inputData->size; i++)
{
luma = 0.299 * inputData->R[i] + 0.587 * inputData->G[i] + 0.114 * inputData->B[i];
//assign luma to output
out->R.push_back(luma);
out->G.push_back(luma);
out->B.push_back(luma);
if (out->A.size() > 0)
out->A.push_back(inputData->A[i]);
}
}
//parallize
void OtsuThreshold::generateHistogram(ImageData* inputData, std::map<int, int>* histogram)
{
for (int i = 0; i < 256; i++) {
histogram->insert({ i,0 });
}
//if grayscale, rgb values of a pixel are identical
//chose r as sample
for (int i = 0; i < inputData->size; i++) {
double value = inputData->R[i];
if (histogram->count(value) > 0) //contains key
{
(*histogram)[value]++;
}
else {
histogram->insert({ value, 1 });
}
}
return;
}
double OtsuThreshold::otsuThreshold(std::map<int, int> hist, int imageSize) {
//pre-calculations
double totalMean = hist[0] * 0;
std::map<int, double> valueProbability;
valueProbability.insert({ 0, (double)hist[0] / (double)imageSize });
for (int i = 1; i < 256; i++) {
valueProbability.insert({ i, (double)hist[i] / (double)imageSize });
totalMean += valueProbability[i] * i;
}
//--------------------------------------------------
int threshold = 0;
double w0 = 0, w1 = 0;
double mu0 = 0, mu1 = 0, mu = 0;
double interclassVariance = 0;
double highestVariance = 0;
double highestThreshold = 0;
//#pragma omp parallel
for (int i = 0; i < 256; i++)
{
threshold = i;
if (threshold == 0)
continue;
w0 += valueProbability[threshold];
w1 = 1 - w0;
mu += threshold * valueProbability[threshold];
mu0 = mu / w0;
mu1 = (totalMean - mu) / w1;
interclassVariance = w0 * w1 * pow(mu0 - mu1, 2);
if (interclassVariance > highestVariance)
{
highestVariance = interclassVariance;
highestThreshold = threshold;
}
}
return highestThreshold;
}
//assumes image is greyscale
void OtsuThreshold::applyThresholding(ImageData* inputData, ImageData* outputData, double thresholdValue, std::vector<int> low, std::vector<int> high)
{
for (int i = 0; i < inputData->size; i++)
{
//check pixel brightness, assume grayscale, use R channel as sample
//if higher than threshold
if (inputData->R[i] > thresholdValue)
{
outputData->R[i] = (high[0]);
outputData->G[i] = (high[1]);
outputData->B[i] = (high[2]);
}
else if (inputData->R[i] <= thresholdValue)
{
outputData->R[i] = (low[0]);
outputData->G[i] = (low[1]);
outputData->B[i] = (low[2]);
}
}
}
void OtsuThreshold_v2::greyscaleImage(ImageData* inputData, ImageData* out)
{
out->size = inputData->size;
out->width = inputData->width;
out->height = inputData->height;
out->channels = inputData->channels;
unsigned char luma = 0;
for (int i = 0; i < inputData->size; i++)
{
luma = 0.299 * inputData->R[i] + 0.587 * inputData->G[i] + 0.114 * inputData->B[i];
//assign luma to output
out->R.push_back(luma);
out->G.push_back(luma);
out->B.push_back(luma);
if (out->A.size() > 0)
out->A.push_back(inputData->A[i]);
}
}
//parallize
void OtsuThreshold_v2::generateHistogram(ImageData* inputData, std::map<int, int>* histogram)
{
for (int i = 0; i < 256; i++) {
histogram->insert({ i,0 });
}
//if grayscale, rgb values of a pixel are identical
//chose r as sample
#pragma omp parallel
{
// Each thread gets its own local histogram
std::map<double, int> local_hist;
for (int i = 0; i < inputData->size; i++) {
double value = inputData->R[i];
local_hist[value]++;
}
// Merge local histograms into global one
#pragma omp critical
{
for (const auto& key : local_hist) {
(*histogram)[key.first] += key.second;
}
}
}
return;
}
double OtsuThreshold_v2::otsuThreshold(std::map<int, int> hist, int imageSize) {
//pre-calculations
double totalMean = hist[0] * 0;
std::map<int, double> valueProbability;
valueProbability.insert({ 0, (double)hist[0] / (double)imageSize });
for (int i = 1; i < 256; i++) {
valueProbability.insert({ i, (double)hist[i] / (double)imageSize });
totalMean += valueProbability[i] * i;
}
//--------------------------------------------------
int threshold = 0;
double w0 = 0, w1 = 0;
double mu0 = 0, mu1 = 0, mu = 0;
double interclassVariance = 0;
double highestVariance = 0;
double highestThreshold = 0;
//#pragma omp parallel
for (int i = 0; i < 256; i++)
{
threshold = i;
if (threshold == 0)
continue;
w0 += valueProbability[threshold];
w1 = 1 - w0;
mu += threshold * valueProbability[threshold];
mu0 = mu / w0;
mu1 = (totalMean - mu) / w1;
interclassVariance = w0 * w1 * pow(mu0 - mu1, 2);
if (interclassVariance > highestVariance)
{
highestVariance = interclassVariance;
highestThreshold = threshold;
}
}
return highestThreshold;
}
//assumes image is greyscale
void OtsuThreshold_v2::applyThresholding(ImageData* inputData, ImageData* outputData, double thresholdValue, std::vector<int> low, std::vector<int> high)
{
for (int i = 0; i < inputData->size; i++)
{
//check pixel brightness, assume grayscale, use R channel as sample
//if higher than threshold
if (inputData->R[i] > thresholdValue)
{
outputData->R[i] = (high[0]);
outputData->G[i] = (high[1]);
outputData->B[i] = (high[2]);
}
else if (inputData->R[i] <= thresholdValue)
{
outputData->R[i] = (low[0]);
outputData->G[i] = (low[1]);
outputData->B[i] = (low[2]);
}
}
}