-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cpp
More file actions
114 lines (96 loc) · 2.79 KB
/
kernel.cpp
File metadata and controls
114 lines (96 loc) · 2.79 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
#include "kernel.h"
#include "image.h"
void scale_functions::sobel_scale(Image &src) {
for (int i = 0; i < src.height(); i++) {
for (int j = 0; j < src.width(); j++) {
src.at(i, j) = (int)((src.at(i, j) + 255 * 4) / 8);
}
}
}
void scale_functions::identity_scale(Image &src) {}
void scale_functions::mean_blur_scale(Image &src) {
for (int i = 0; i < src.height(); i++) {
for (int j = 0; j < src.width(); j++) {
src.at(i, j) = (int)((src.at(i, j) / 9));
}
}
}
void scale_functions::gausian_blur_scale(Image &src) {
for (int i = 0; i < src.height(); i++) {
for (int j = 0; j < src.width(); j++) {
src.at(i, j) = (int)((src.at(i, j) / 16));
}
}
}
Kernel::Kernel() {
this->w = 3;
this->h = 3;
kern_type = Identity;
for (unsigned int i = 0; i < h; i++) {
for (unsigned int j = 0; j < w; j++) {
kernel[i][j] = 0;
if (i == j) {
kernel[i][j] = 1;
}
}
}
}
void InitKernels::identity_kern(Kernel &src) {
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++) {
src.kernel[i][j] = 0;
if (i == j) {
src.kernel[i][j] = 1;
}
}
}
}
void InitKernels::mean_blur_kern(Kernel &src) {
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++) {
src.kernel[i][j] = 1;
}
}
}
void InitKernels::gausian_kern(Kernel &src) {
double gaus_kern[3][3] = {{1, 2, 1}, {2, 4, 2}, {1, 2, 1}};
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++) {
src.kernel[i][j] = gaus_kern[i][j];
}
}
}
void InitKernels::horizontal_sobel_kern(Kernel &src) {
double h_sobel_kern[3][3] = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++) {
src.kernel[i][j] = h_sobel_kern[i][j];
}
}
}
void InitKernels::vertical_sobel_kern(Kernel &src) {
double v_sobel_kern[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
for (unsigned int i = 0; i < 3; i++) {
for (unsigned int j = 0; j < 3; j++) {
src.kernel[i][j] = v_sobel_kern[i][j];
}
}
}
void Kernel::set_kernel(Kernel_Type kern_t) {
if (kern_t == Identity) {
InitKernels::identity_kern(*this);
scale_function = scale_functions::identity_scale;
} else if (kern_t == Mean_Blur) {
InitKernels::mean_blur_kern(*this);
scale_function = scale_functions::mean_blur_scale;
} else if (kern_t == Gausian_Blur) {
InitKernels::gausian_kern(*this);
scale_function = scale_functions::gausian_blur_scale;
} else if (kern_t == Vertical_Sobel) {
InitKernels::vertical_sobel_kern(*this);
scale_function = scale_functions::sobel_scale;
} else if (kern_t == Horizontal_Sobel) {
InitKernels::horizontal_sobel_kern(*this);
scale_function = scale_functions::sobel_scale;
}
}