-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathotsu_method.cpp
73 lines (69 loc) · 1.93 KB
/
otsu_method.cpp
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
#include <math.h>
#include <simpleBMP.h>
/**
* Otsu's method is an algorithm for gray-scale image thresholding
* See https://en.wikipedia.org/wiki/Otsu%27s_method
* @param p_img[in,out] the image to be thresholded, it will be updated in place
*/
void Otsu(simpleBMP::BMPImg *p_img) {
auto &img = *p_img;
int numofpixal =
img.info_header.biWidth * img.info_header.biHeight * img.Channel();
double pi[256] = {0};
double px = 1.0 / numofpixal;
for (int row = 0; row < img.info_header.biHeight; ++row) {
auto line = img.AtLine(row);
for (int col = 0; col < img.info_header.biWidth; ++col) {
int value = line[col];
pi[value] += px;
}
}
double p1k[256] = {0};
double mk[256] = {0};
double cumsumpi = 0;
double mg = 0;
for (int i = 0; i < 255; i++) {
cumsumpi += pi[i];
p1k[i] = cumsumpi;
mg += i * pi[i];
mk[i] = mg;
}
int numofbestk = 1;
int bestk = 0;
double besttaub = 0;
double taubk[256] = {0};
for (int i = 0; i < 255; i++) {
taubk[i] = pow(mg * p1k[i] - mk[i], 2) / (p1k[i]) / (1 - p1k[i]);
if (taubk[i] > besttaub) {
bestk = i;
besttaub = taubk[i];
numofbestk = 1;
} else if (taubk[i] == besttaub) {
bestk += i;
numofbestk += 1;
}
}
bestk /= numofbestk;
for (int row = 0; row < img.info_header.biHeight; ++row) {
auto line = img.AtLine(row);
for (int col = 0; col < img.info_header.biWidth; ++col) {
int value = line[col];
line[col] = ((uint8_t)(value > bestk)) * 255;
}
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <path_to_input_image>\n", argv[0]);
return 1;
}
simpleBMP::BMPImg grayscale_img;
grayscale_img.LoadImage(argv[1]);
assert(grayscale_img.info_header.biBitCount == 8 &&
"Only support 8-bit gray-scale image");
Otsu(&grayscale_img);
std::string path = argv[1];
path += ".otsu.bmp";
grayscale_img.SaveImage(path);
return 0;
}