-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRecursiveBilateralFilter.cpp
469 lines (388 loc) · 12.9 KB
/
RecursiveBilateralFilter.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Purpose of this file is to run a series of tests on several images using different implementations of the
// Recursive Bilaterial Filter, and to show rough time estimate for each run
#include "stdafx.h"
#include "stb_image.h"
#include "stb_image_write.h"
#include "rbf.hpp"
#include <time.h>
#include <iostream>
#include <time.h>
#include "RBFilter_SSE2.h"
#include "RBFilter_AVX2.h"
#include <iomanip>
using namespace std;
// main filter strength controls
const float sigma_spatial = 0.12f;
const float sigma_range = 0.09f;
// number of test runs per image, for better average time measurement
// if running debug mode, use small number so it's faster
#ifdef _DEBUG
const int test_runs = 1;
#else
const int test_runs = 100;
#endif
// path where files are located, you may need to change this
const char images_folder_path[] = "./images/";
// test images:
const char file_name_testGirl[] = "testGirl.jpg"; // size: 448 x 626
const char file_name_house[] = "Thefarmhouse.jpg"; // size: 1440 x 1080
const char file_name_testpattern[] = "testpatern5.png"; // size: 1920 x 1080
// timer uses 'test_runs' as divisor
class TestRunTimer
{
clock_t begTime;
public:
void start() { begTime = clock(); }
float elapsedTimeMS() { return float(clock() - begTime) / (float)test_runs; }
};
// utility for setting output file name
template <size_t _Size>
char* modifyFilePath(char (&file_path)[_Size], const char* suffix)
{
size_t l = strlen(file_path);
// get rid of old extension
for (size_t i = l - 1; i > 0; i--)
{
if (file_path[i] == '.')
{
file_path[i] = 0;
break;
}
}
// add current sigma values just for clarity
char extra_text[64];
sprintf_s(extra_text, "%0.3f_%0.3f", sigma_spatial, sigma_range);
// add suffix
strcat_s(file_path, "_");
strcat_s(file_path, suffix);
strcat_s(file_path, "_");
strcat_s(file_path, extra_text);
strcat_s(file_path, ".png"); // force PNG format
return file_path;
}
// using original implementation, source code from
// https://github.com/ufoym/RecursiveBF
void testRunRecursiveBF_Original(const char* image_name)
{
cout << "\nImage: " << image_name;
char file_path[256];
strcpy_s(file_path, images_folder_path);
strcat_s(file_path, image_name);
int width, height, channel;
unsigned char * img = stbi_load(file_path, &width, &height, &channel, 3);
if (!img)
{
cout << "\nFailed to load image path: " << file_path;
return;
}
cout << ", size: " << width << " x " << height;
channel = 3; // require 3 channel for this test
unsigned char * img_out = nullptr;
TestRunTimer timer;
// memory reserve for filter algorithm before timer start
float * buffer = new float[(width * height* channel + width * height + width * channel + width) * 2];
timer.start();
for (int i = 0; i < test_runs; ++i)
recursive_bf(img, img_out, sigma_spatial, sigma_range, width, height, channel, buffer);
cout << ", time ms: " << timer.elapsedTimeMS();
delete[] buffer;
modifyFilePath(file_path, "RBF");
stbi_write_png(file_path, width, height, channel, img_out, width * 3);
delete[] img;
delete[] img_out;
}
// using optimized SSE2 with optional multithreading, single stage (non-pipelined)
void testRunRecursiveBF_SSE2_mt(const char* image_name, int thread_count)
{
cout << "\nImage: " << image_name;
char file_path[256];
strcpy_s(file_path, images_folder_path);
strcat_s(file_path, image_name);
int width, height, channel;
unsigned char * img = stbi_load(file_path, &width, &height, &channel, 4);
if (!img)
{
cout << "\nFailed to load image path: " << file_path;
return;
}
cout << ", size: " << width << " x " << height;
channel = 4; // require 4 channel for this test
CRBFilterSSE2 rbf_object;
bool success = rbf_object.initialize(width, height, thread_count, false);
if (!success)
{
cout << "\nCRBFilterSSE2 failed to initialize for some reason";
delete[] img;
return;
}
rbf_object.setSigma(sigma_spatial, sigma_range);
unsigned char * img_out = new unsigned char[width * height * 4];
TestRunTimer timer;
timer.start();
for (int i = 0; i < test_runs; ++i)
success = rbf_object.filter(img_out, img, width, height, width * 4);
if (success)
{
cout << ", time ms: " << timer.elapsedTimeMS();
}
else // fail
{
cout << "\nCRBFilterSSE2::filter failed for some reason";
}
char suffix[64];
sprintf_s(suffix, "SSE2_%dt", thread_count);
modifyFilePath(file_path, suffix);
stbi_write_png(file_path, width, height, channel, img_out, width * 4);
delete[] img;
delete[] img_out;
}
// using optimized SSE2 with optional multithreading, pipelined 2 stages
void testRunRecursiveBF_SSE2_Pipelined(const char* image_name, int thread_count)
{
cout << "\nImage: " << image_name;
char file_path[256];
strcpy_s(file_path, images_folder_path);
strcat_s(file_path, image_name);
int width, height, channel;
unsigned char * img = stbi_load(file_path, &width, &height, &channel, 4);
if (!img)
{
cout << "\nFailed to load image path: " << file_path;
return;
}
cout << ", size: " << width << " x " << height;
channel = 4; // require 4 channel for this test
CRBFilterSSE2 rbf_object;
bool success = rbf_object.initialize(width, height, thread_count, true);
if (!success)
{
cout << "\nCRBFilterSSE2 failed to initialize for some reason";
delete[] img;
return;
}
rbf_object.setSigma(sigma_spatial, sigma_range);
// need 2 output buffers, one for each stage
unsigned char * img_out[2];
img_out[0] = new unsigned char[width * height * 4];
img_out[1] = new unsigned char[width * height * 4];
TestRunTimer timer;
timer.start();
for (int i = 0; i < test_runs; ++i)
success = rbf_object.filterPipePush(img_out[i&1], img, width, height, width * 4);
rbf_object.filterPipeFlush();
if (success)
{
cout << ", time ms: " << timer.elapsedTimeMS();
}
else // fail
{
cout << "\nCRBFilterSSE2::filterPipePush failed for some reason";
}
char suffix[64];
sprintf_s(suffix, "SSE2_Pipe_%dt", thread_count);
modifyFilePath(file_path, suffix);
stbi_write_png(file_path, width, height, channel, img_out[0], width * 4);
delete[] img;
delete[] img_out[0];
delete[] img_out[1];
}
// using optimized AVX2 with optional multithreading, single stage (non-pipelined)
void testRunRecursiveBF_AVX2_mt(const char* image_name, int thread_count)
{
cout << "\nImage: " << image_name;
char file_path[256];
strcpy_s(file_path, images_folder_path);
strcat_s(file_path, image_name);
int width, height, channel;
unsigned char * img = stbi_load(file_path, &width, &height, &channel, 4);
if (!img)
{
cout << "\nFailed to load image path: " << file_path;
return;
}
cout << ", size: " << width << " x " << height;
channel = 4; // require 4 channel for this test
CRBFilterAVX2 rbf_object;
bool success = rbf_object.initialize(width, height, thread_count, false);
if (!success)
{
cout << "\nCRBFilterAVX2 failed to initialize for some reason";
delete[] img;
return;
}
rbf_object.setSigma(sigma_spatial, sigma_range);
int pitch = rbf_object.getOptimalPitch(width);
unsigned char * img_out;
// setup 32 byte aligned memory buffers for input and output, using optimal pitch
{
img_out = (unsigned char*)_aligned_malloc(pitch * height, 32);
// move source image to aligned memory
unsigned char* buffer = (unsigned char*)_aligned_malloc(pitch * height, 32);
for (int y = 0; y < height; y++)
{
memcpy(buffer + y * pitch, img + y * width * 4, width * 4);
}
delete[] img;
img = buffer;
}
TestRunTimer timer;
timer.start();
for (int i = 0; i < test_runs; ++i)
success = rbf_object.filter(img_out, img, width, height, pitch);
if (success)
{
cout << ", time ms: " << timer.elapsedTimeMS();
}
else // fail
{
cout << "\nCRBFilterAVX2::filter failed for some reason";
}
char suffix[64];
sprintf_s(suffix, "AVX2_%dt", thread_count);
modifyFilePath(file_path, suffix);
stbi_write_png(file_path, width, height, channel, img_out, pitch);
_aligned_free(img);
_aligned_free(img_out);
}
// using optimized AVX2 with optional multithreading, pipelined 2 stages, memory aligned
void testRunRecursiveBF_AVX2_Pipelined(const char* image_name, int thread_count)
{
cout << "\nImage: " << image_name;
char file_path[256];
strcpy_s(file_path, images_folder_path);
strcat_s(file_path, image_name);
int width, height, channel;
unsigned char * img = stbi_load(file_path, &width, &height, &channel, 4);
if (!img)
{
cout << "\nFailed to load image path: " << file_path;
return;
}
cout << ", size: " << width << " x " << height;
channel = 4; // require 4 channel for this test
CRBFilterAVX2 rbf_object;
bool success = rbf_object.initialize(width, height, thread_count, true);
if (!success)
{
cout << "\nCRBFilterAVX2 failed to initialize for some reason";
delete[] img;
return;
}
rbf_object.setSigma(sigma_spatial, sigma_range);
int pitch = rbf_object.getOptimalPitch(width);
unsigned char* img_out[2];
// setup 32 byte aligned memory buffers for input and output, using optimal pitch
{
img_out[0] = (unsigned char*)_aligned_malloc(pitch * height, 32);
img_out[1] = (unsigned char*)_aligned_malloc(pitch * height, 32);
// move source image to aligned memory
unsigned char* buffer = (unsigned char*)_aligned_malloc(pitch * height, 32);
for (int y = 0; y < height; y++)
{
memcpy(buffer + y * pitch, img + y * width * 4, width * 4);
}
delete[] img;
img = buffer;
}
TestRunTimer timer;
timer.start();
for (int i = 0; i < test_runs; ++i)
success = rbf_object.filterPipePush(img_out[i & 1], img, width, height, width * 4);
rbf_object.filterPipeFlush();
if (success)
{
cout << ", time ms: " << timer.elapsedTimeMS();
}
else // fail
{
cout << "\nCRBFilterAVX2::filterPipePush failed for some reason";
}
char suffix[64];
sprintf_s(suffix, "AVX2_Pipe_%dt", thread_count);
modifyFilePath(file_path, suffix);
stbi_write_png(file_path, width, height, channel, img_out[0], pitch);
_aligned_free(img);
_aligned_free(img_out[0]);
_aligned_free(img_out[1]);
}
/////////////////////////////////////////////////////////////////////////////
int main()
{
cout << "test run \n";
cout << fixed << setprecision(1);
////////////////////////
cout << "\nOriginal Recursive Bilateral Filter implementation";
// image: testpattern
testRunRecursiveBF_Original(file_name_testpattern);
// image: house
testRunRecursiveBF_Original(file_name_house);
// image: testGirl
testRunRecursiveBF_Original(file_name_testGirl);
////////////////////////
cout << "\n\nOptimized SSE2 single threaded, single stage (non-pipelined)";
// image: testpattern
testRunRecursiveBF_SSE2_mt(file_name_testpattern, 1);
// image: house
testRunRecursiveBF_SSE2_mt(file_name_house, 1);
// image: testGirl
testRunRecursiveBF_SSE2_mt(file_name_testGirl, 1);
////////////////////////
cout << "\n\nOptimized SSE2 2x multithreading, single stage (non-pipelined)";
// image: testpattern
testRunRecursiveBF_SSE2_mt(file_name_testpattern, 2);
// image: house
testRunRecursiveBF_SSE2_mt(file_name_house, 2);
// image: testGirl
testRunRecursiveBF_SSE2_mt(file_name_testGirl, 2);
////////////////////////
cout << "\n\nOptimized SSE2 4x multithreading, single stage (non-pipelined)";
// image: testpattern
testRunRecursiveBF_SSE2_mt(file_name_testpattern, 4);
// image: house
testRunRecursiveBF_SSE2_mt(file_name_house, 4);
// image: testGirl
testRunRecursiveBF_SSE2_mt(file_name_testGirl, 4);
////////////////////////
cout << "\n\nOptimized SSE2 4x2 thread pipelined 2 stages";
// image: testpattern
testRunRecursiveBF_SSE2_Pipelined(file_name_testpattern, 4);
// image: house
testRunRecursiveBF_SSE2_Pipelined(file_name_house, 4);
// image: testGirl
testRunRecursiveBF_SSE2_Pipelined(file_name_testGirl, 4);
////////////////////////
cout << "\n\nOptimized AVX2 single threaded, single stage (non-pipelined), memory aligned";
// image: testpattern
testRunRecursiveBF_AVX2_mt(file_name_testpattern, 1);
// image: house
testRunRecursiveBF_AVX2_mt(file_name_house, 1);
// image: testGirl
testRunRecursiveBF_AVX2_mt(file_name_testGirl, 1);
////////////////////////
cout << "\n\nOptimized AVX2 2x multithreading, single stage (non-pipelined), memory aligned";
// image: testpattern
testRunRecursiveBF_AVX2_mt(file_name_testpattern, 2);
// image: house
testRunRecursiveBF_AVX2_mt(file_name_house, 2);
// image: testGirl
testRunRecursiveBF_AVX2_mt(file_name_testGirl, 2);
////////////////////////
cout << "\n\nOptimized AVX2 4x multithreading, single stage (non-pipelined), memory aligned";
// image: testpattern
testRunRecursiveBF_AVX2_mt(file_name_testpattern, 4);
// image: house
testRunRecursiveBF_AVX2_mt(file_name_house, 4);
// image: testGirl
testRunRecursiveBF_AVX2_mt(file_name_testGirl, 4);
////////////////////////
cout << "\n\nOptimized AVX2 4x2 thread pipelined 2 stages, memory aligned";
// image: testpattern
testRunRecursiveBF_AVX2_Pipelined(file_name_testpattern, 4);
// image: house
testRunRecursiveBF_AVX2_Pipelined(file_name_house, 4);
// image: testGirl
testRunRecursiveBF_AVX2_Pipelined(file_name_testGirl, 4);
cout << "\nFinish";
cin.get();
return 0;
}