-
Notifications
You must be signed in to change notification settings - Fork 1
/
unsharp_mask.cpp
123 lines (97 loc) · 3.93 KB
/
unsharp_mask.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
// 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 unsharp_mask.cpp -std=c++11 -L halide/bin/ -lHalide `libpng-config --cflags --ldflags` -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -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 gray;
gray(x, y) = max(in_bounded(x, y, 0),
max(in_bounded(x, y, 1),
in_bounded(x, y, 2)));
Func blur_y;
blur_y(x, y) = (kernel(0) * gray(x, y) +
kernel(1) * (gray(x, y-1) +
gray(x, y+1)) +
kernel(2) * (gray(x, y-2) +
gray(x, y+2)) +
kernel(3) * (gray(x, y-3) +
gray(x, y+3)));
Func blur_x;
blur_x(x, y) = (kernel(0) * blur_y(x, y) +
kernel(1) * (blur_y(x-1, y) +
blur_y(x+1, y)) +
kernel(2) * (blur_y(x-2, y) +
blur_y(x+2, y)) +
kernel(3) * (blur_y(x-3, y) +
blur_y(x+3, y)));
Func sharpen;
sharpen(x, y) = 2 * gray(x, y) - blur_x(x, y);
Func ratio;
ratio(x, y) = sharpen(x, y) / gray(x, y);
Func result;
result(x, y, c) = ratio(x, y) * in(x, y, c);
// Schedule it.
blur_y.compute_at(result, y).vectorize(x, 8);
ratio.compute_at(result, y).vectorize(x, 8);
result.vectorize(x, 8).parallel(y).reorder(x, c, y);
// Benchmark the pipeline.
Image<float> output(in.width(),
in.height(),
in.channels());
for (int i = 0; i < 10; i++) {
double t1 = current_time();
result.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::imread("input.png");
input_image.convertTo(input_image, CV_32FC3);
cv::Mat output_image;
double best = 1e10;
for (int i = 0; i < 10; i++) {
double t1 = current_time();
cv::Mat channels[3];
cv::split(input_image, channels);
cv::Mat gray = cv::max(channels[0], cv::max(channels[1], channels[2]));
cv::Mat blurry(gray.size(), CV_32FC1);
GaussianBlur(gray, blurry, cv::Size(7, 7),
1.5f, 1.5f, cv::BORDER_REPLICATE);
cv::Mat sharp = 2*gray - blurry;
cv::Mat out_channels[3];
cv::Mat ratio = sharp/gray;
for (int c = 0; c < 3; c++) {
out_channels[c] = channels[c].mul(ratio);
}
cv::merge(out_channels, 3, output_image);
double t2 = current_time();
best = std::min(best, t2 - t1);
}
std::cout << "OpenCV time: " << best << "\n";
output_image.convertTo(output_image, CV_8UC3);
cv::imwrite("opencv_output.png", output_image);
}
return 0;
}