-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
190 lines (152 loc) · 4.94 KB
/
log.cpp
File metadata and controls
190 lines (152 loc) · 4.94 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
#include <iostream>
#include <chrono>
#include <cmath>
#include <cstring>
#define PI 3.141516f
struct sImage {
int width, height;
int margin;
float *data;
sImage(const int i_margin) {
margin = i_margin;
data = NULL;
};
~sImage() {
if (data != NULL) {
delete data;
}
};
/**
* (Main Function!)
* Copies a secuence of 1-D raw image data,
* and adds the margins border
*/
void copy_into(const float* i_data, const int data_width, const int data_heigth) {
width = data_width, height = data_heigth;
// Data cleanup
if (data != NULL) {
delete data;
}
data = new float[(width + margin) * (height + margin)];
memset(data, 0.f, sizeof(float) * (width + margin) * (height + margin));
#pragma omp parallel for
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
data[(x * (width + margin)) + y] = i_data[(x * width) + y];
}
}
};
/**
* Wrapper for cleaning the accesing the image, taking into
* accout the margins
*/
inline float get_with_margins(const int x, const int y) const {
return data[(x * (width + margin)) + y];
};
/**
* Wrapper for cleaning the accesing the image
*/
inline float get(const int x, const int y) const {
return data[(x * width) + y];
};
};
struct sLoG {
float *kernel;
int kernel_size;
float std_desv;
sLoG(const int kernel_side_size, const float desv) {
kernel_size = kernel_side_size;
std_desv = desv;
kernel = new float[kernel_size * kernel_size];
generate_kernel();
}
~sLoG() {
delete kernel;
}
/**
* Generate the Laplacian of Gaussian discrete kernel
* for convolution, according to the formula:
*
* See pic on readme
*
*/
void generate_kernel() {
// Compute parts of the equation that are not dependant
// on the x, y factors
float p1 = -1.f / (PI * pow(std_desv, 4.0f));
float p2 = 2.f * pow(std_desv, 2.0f);
for (int x = 0; x < kernel_size; x++) {
float x_2 = pow(x, 2.0f);//x * x;
for (int y = 0; y < kernel_size; y++) {
float y_2 = pow(y, 2.0f);
float p3 = - ((x_2 + y_2) / p2);
float tmp = p1;
tmp *= 1.f + p3;
tmp *= exp(p3);
kernel[(x * kernel_size) + y] = tmp;
}
}
}
/**
* Wrapper for cleaning up the acces to the filter
*/
inline float get(const int x, const int y) const {
return kernel[(x * kernel_size) + y];
};
/**
* (Main Function!)
* Computes the average Laplacian of Gaussian score
* for a given image, usigin the previusly calculated
* convolutional filter
*/
float compute_avg_LoG(const sImage *img) {
float result = 0.0f;
int img_width = img->width, img_heigth = img->height;
int kernel_half = kernel_size / 2.0f;
#pragma omp parallel for collapse(2) reduction(+:result)
for (int i_x = 0; i_x < img_width; i_x++) {
for (int i_y = 0; i_y < img_heigth; i_y++) {
float tmp = 0.0f;
// Convolutional operation optimized with SIMD
#pragma omp simd collapse(2) reduction(+:tmp)
for (int f_x = 0; f_x < kernel_size; f_x++) {
for (int f_y = 0; f_y < kernel_size; f_y++) {
int i_f_x = f_x + i_x - kernel_half; // Image's filter indexess
int i_f_y = f_y + i_y - kernel_half;
tmp += get(f_x, f_y) * img->get_with_margins(i_f_x, i_f_y);
}
}
result += tmp;
}
}
return result;
}
};
int main() {
sLoG log_op = sLoG(10, 1.4f);
sImage img = sImage(10);
// Iterative test multiple batch sizes
for (int j = 0; j < 3; j++) {
int size = pow(100.0f, j);
float res;
// Prepare test data
float* ex_data = new float[size * size];
for (int i = 0; i < size * size; i++) {
ex_data[i] = i;
}
float avg_result = 0.0f;
for (int i = 0; i <= 100; i++) {
// Calculate and measure time
auto start_timer = std::chrono::steady_clock::now();
img.copy_into(ex_data, 100, 100);
res = log_op.compute_avg_LoG(&img);
auto end_timer = std::chrono::steady_clock::now();
avg_result = std::chrono::duration_cast<std::chrono::microseconds>(end_timer - start_timer).count();
}
avg_result /= 100;
// My god, what is this monster
std::cout << "LoG Size: " << size << " Result: " << res << " Time: "<< avg_result << std::endl;
delete ex_data;
}
return 0;
};