This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathimpl_gray.h
134 lines (116 loc) · 3.49 KB
/
impl_gray.h
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
#ifndef IMPL_GRAY_H
#define IMPL_GRAY_H
#include "prefix.h"
#include "math.h"
inline basic_image sh_gy_general(basic_image const& old_img)
{
if (old_img.is_empty()) return basic_image();
// make gray image for showing
basic_image new_img(old_img.width(), old_img.height());
int px, py;
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
int gray = old_img.at(px, py).gray();
new_img.at(px, py).red(gray);
new_img.at(px, py).green(gray);
new_img.at(px, py).blue(gray);
}
}
return new_img;
}
inline basic_image sh_gy_stretch(basic_image const& old_img)
{
if (old_img.is_empty()) return basic_image();
int px, py;
// gets the lower and upper bounds of the old image
int old_lower = 0;
int old_upper = 0;
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
old_lower = old_img.at(px, py).gray() < old_lower? old_img.at(px, py).gray() : old_lower;
old_upper = old_img.at(px, py).gray() > old_upper? old_img.at(px, py).gray() : old_upper;
}
}
// adjusts gray
int new_lower = 0;
int new_upper = 255;
basic_image new_img(old_img.width(), old_img.height());
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
int new_gray = (new_upper - new_lower) * old_img.at(px, py).gray() / (old_upper - old_lower) + new_lower;
if (new_gray < new_lower) new_gray = new_lower;
if (new_gray > new_upper) new_gray = new_upper;
new_img.at(px, py).gray(new_gray);
}
}
return new_img;
}
inline basic_image sh_gy_histogram(basic_image const& old_img)
{
if (old_img.is_empty()) return basic_image();
int px, py;
// stats gray
int gray_h[256] = {0}; // gray histogram
int max_gray = old_img.at(0, 0).gray();
int min_gray = old_img.at(0, 0).gray();
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
max_gray = old_img.at(px, py).gray() > max_gray? old_img.at(px, py).gray() : max_gray;
min_gray = old_img.at(px, py).gray() < min_gray? old_img.at(px, py).gray() : min_gray;
EXTL_ASSERT(old_img.at(px, py).gray() < 256);
++gray_h[old_img.at(px, py).gray()];
}
}
// cumulative probability
double c[256];
int pixel_n = old_img.width() * old_img.height();
double cum = 0;
for (int i = 0; i < 256; ++i)
{
cum += double(gray_h[i]) / pixel_n;
c[i] = cum;
}
// transform
basic_image new_img(old_img.width(), old_img.height());
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
int new_gray = int(c[old_img.at(px, py).gray()] * (max_gray - min_gray) + min_gray);
if (new_gray < 0) new_gray = 0;
if (new_gray > 255) new_gray = 255;
new_img.at(px, py).gray(new_gray);
}
}
return new_img;
}
inline basic_image sh_gy_adaptive(basic_image const& old_img)
{
if (old_img.is_empty()) return basic_image();
// pre-handle
basic_image tmp_img = sh_gy_stretch(old_img);
tmp_img = sh_gy_histogram(tmp_img);
// make gray image for showing
basic_image new_img(tmp_img.width(), tmp_img.height());
int px, py;
for (px = 0; px < (int)tmp_img.width(); ++px)
{
for (py = 0; py < (int)tmp_img.height(); ++py)
{
int gray = tmp_img.at(px, py).gray();
new_img.at(px, py).red(gray);
new_img.at(px, py).green(gray);
new_img.at(px, py).blue(gray);
}
}
return new_img;
}
#endif // IMPL_GRAY_H