-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistogram.cpp
More file actions
73 lines (61 loc) · 2.01 KB
/
Histogram.cpp
File metadata and controls
73 lines (61 loc) · 2.01 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
#include "Histogram.h"
#include "SpecialImage.h"
void Histogram::renderHistogram(char const *fileName) {
int height = 0;
int width = histogram.size();
for (int i = 0; i < width; i++)
height = max(height, histogram[i] / 10);
height++;
SpecialImage graph(width, height, Color(255, 255, 255));
for (int i = 0; i < width; i++)
for (int j = 0; j < histogram[i] / 10; j++)
graph.set_color(j, i, Color(0, 0, 0));
std::vector<int> v = findThresholds();
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < height; j++) {
graph.set_color(j, v[i], Color(255, 0, 0));
}
}
graph.reflectVertical().write(fileName);
}
Histogram::Histogram(Image const &I1, int smooth_factor,
int considerationFactor)
: histogram(I1.resolution() + 1, 0),
considerationFactor(considerationFactor) {
resolution = I1.resolution();
vector<int> raw_histogram(I1.resolution() + 1, 0);
for (int i = 0; i < I1._height; i++)
for (int j = 0; j < I1._width; j++) {
raw_histogram[I1.color(i, j).brightness()]++;
}
for (int i = 0; i < I1.resolution(); i++) {
histogram[i] = raw_histogram[i];
}
for (int i = smooth_factor; i < I1.resolution() - smooth_factor; i++) {
int avg = 0;
for (int j = i - smooth_factor; j < i + smooth_factor + 1; j++) {
avg += raw_histogram[j];
}
histogram[i] = (avg) / (2 * smooth_factor + 1);
}
}
vector<int> Histogram::getDistribution() { return histogram; }
vector<int> Histogram::findThresholds() {
vector<int> thresholds;
for (int i = considerationFactor; i < resolution - considerationFactor; i++) {
int flag = 0;
for (int j = i - considerationFactor; j < i && !flag; j++) {
if (histogram[j] < histogram[i])
flag = 1;
}
for (int j = i + 1; j < i + considerationFactor + 1 && !flag; j++) {
if (histogram[j] < histogram[i])
flag = 1;
}
if (!flag && histogram[i]) {
thresholds.push_back(i);
i += 2 * considerationFactor + 2;
}
}
return thresholds;
}