-
Notifications
You must be signed in to change notification settings - Fork 1
/
blur.cpp
92 lines (75 loc) · 3.17 KB
/
blur.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
// Download a Halide distribution from halide-lang.org and untar it in
// the current directory. Then you should be able to compile this
// file with:
//
// c++ -g blur.cpp -std=c++11 -L halide/bin/ -lHalide `libpng-config --cflags --ldflags` -lopencv_core -lopencv_imgproc -O3
//
// You'll also need a multi-megapixel png image to run this on. Name
// it input.png and put it in this directory.
// Include the Halide language
#include "halide/include/Halide.h"
using namespace Halide;
#include <iostream>
// Some support code for timing and loading/saving images
#include "halide/tools/halide_image_io.h"
#include "halide/tutorial/clock.h"
// Include OpenCV for timing comparison
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(int argc, char **argv) {
Image<float> in = Tools::load_image("input.png");
// Define a 7x7 Gaussian Blur with a repeat-edge boundary condition.
float sigma = 1.5f;
Var x, y, c;
Func kernel;
kernel(x) = exp(-x*x/(2*sigma*sigma)) / (sqrtf(2*M_PI)*sigma);
Func in_bounded = BoundaryConditions::repeat_edge(in);
Func blur_y;
blur_y(x, y, c) = (kernel(0) * in_bounded(x, y, c) +
kernel(1) * (in_bounded(x, y-1, c) +
in_bounded(x, y+1, c)) +
kernel(2) * (in_bounded(x, y-2, c) +
in_bounded(x, y+2, c)) +
kernel(3) * (in_bounded(x, y-3, c) +
in_bounded(x, y+3, c)));
Func blur_x;
blur_x(x, y, c) = (kernel(0) * blur_y(x, y, c) +
kernel(1) * (blur_y(x-1, y, c) +
blur_y(x+1, y, c)) +
kernel(2) * (blur_y(x-2, y, c) +
blur_y(x+2, y, c)) +
kernel(3) * (blur_y(x-3, y, c) +
blur_y(x+3, y, c)));
// Schedule it.
kernel.compute_root();
blur_x.compute_root().vectorize(x, 8).parallel(y);
blur_y.compute_at(blur_x, y).vectorize(x, 8);
// Print out pseudocode for the pipeline.
blur_x.compile_to_lowered_stmt("blur.html", {in}, HTML);
// Benchmark the pipeline.
Image<float> output(in.width(),
in.height(),
in.channels());
for (int i = 0; i < 10; i++) {
double t1 = current_time();
blur_x.realize(output);
double t2 = current_time();
std::cout << "Time: " << (t2 - t1) << '\n';
}
Tools::save_image(output, "output.png");
// Time OpenCV doing the same thing.
{
cv::Mat input_image = cv::Mat::zeros(in.width(), in.height(), CV_32FC3);
cv::Mat output_image = cv::Mat::zeros(in.width(), in.height(), CV_32FC3);
double best = 1e10;
for (int i = 0; i < 10; i++) {
double t1 = current_time();
GaussianBlur(input_image, output_image, cv::Size(7, 7),
1.5f, 1.5f, cv::BORDER_REPLICATE);
double t2 = current_time();
best = std::min(best, t2 - t1);
}
std::cout << "OpenCV time: " << best << "\n";
}
return 0;
}