-
Notifications
You must be signed in to change notification settings - Fork 0
/
blur_uchar_rgb.cpp
304 lines (273 loc) · 9.67 KB
/
blur_uchar_rgb.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
// Copyright (C) 2017 Basile Fraboni
// Copyright (C) 2014 Ivan Kutskir
// All Rights Reserved
// You may use, distribute and modify this code under the
// terms of the MIT license. For further details please refer
// to : https://mit-license.org/
//
//!
//! \file blur.cpp
//! \author Basile Fraboni
//! \date 2017
//!
//! \brief The software is a C++ implementation of a fast
//! Gaussian blur algorithm by Ivan Kutskir. For further details
//! please refer to :
//! http://blog.ivank.net/fastest-gaussian-blur.html
//!
//! Unsigned char version
//!
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
#include <cmath>
#include <cstring>
#include <chrono>
typedef unsigned char uchar;
//!
//! \fn void std_to_box(float boxes[], float sigma, int n)
//!
//! \brief this function converts the standard deviation of
//! Gaussian blur into dimensions of boxes for box blur. For
//! further details please refer to :
//! https://www.peterkovesi.com/matlabfns/#integral
//! https://www.peterkovesi.com/papers/FastGaussianSmoothing.pdf
//!
//! \param[out] boxes boxes dimensions
//! \param[in] sigma Gaussian standard deviation
//! \param[in] n number of boxes
//!
void std_to_box(int boxes[], float sigma, int n)
{
// ideal filter width
float wi = std::sqrt((12*sigma*sigma/n)+1);
int wl = std::floor(wi);
if(wl%2==0) wl--;
int wu = wl+2;
float mi = (12*sigma*sigma - n*wl*wl - 4*n*wl - 3*n)/(-4*wl - 4);
int m = std::round(mi);
for(int i=0; i<n; i++)
boxes[i] = ((i < m ? wl : wu) - 1) / 2;
}
//!
//! \fn void horizontal_blur_rgb(uchar * in, uchar * out, int w, int h, int c, int r)
//!
//! \brief this function performs the horizontal blur pass for box blur.
//!
//! \param[in,out] in source channel
//! \param[in,out] out target channel
//! \param[in] w image width
//! \param[in] h image height
//! \param[in] c image channels
//! \param[in] r box dimension
//!
void horizontal_blur_rgb(uchar * in, uchar * out, int w, int h, int c, int r)
{
float iarr = 1.f / (r+r+1);
#pragma omp parallel for
for(int i=0; i<h; i++)
{
int ti = i*w;
int li = ti;
int ri = ti+r;
int fv[3] = { in[ti*c+0], in[ti*c+1], in[ti*c+2] };
int lv[3] = { in[(ti+w-1)*c+0], in[(ti+w-1)*c+1], in[(ti+w-1)*c+2] };
int val[3] = { (r+1)*fv[0], (r+1)*fv[1], (r+1)*fv[2] };
for(int j=0; j<r; j++)
{
val[0] += in[(ti+j)*c+0];
val[1] += in[(ti+j)*c+1];
val[2] += in[(ti+j)*c+2];
}
for(int j=0; j<=r; j++, ri++, ti++)
{
val[0] += in[ri*c+0] - fv[0];
val[1] += in[ri*c+1] - fv[1];
val[2] += in[ri*c+2] - fv[2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
for(int j=r+1; j<w-r; j++, ri++, ti++, li++)
{
val[0] += in[ri*c+0] - in[li*c+0];
val[1] += in[ri*c+1] - in[li*c+1];
val[2] += in[ri*c+2] - in[li*c+2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
for(int j=w-r; j<w; j++, ti++, li++)
{
val[0] += lv[0] - in[li*c+0];
val[1] += lv[1] - in[li*c+1];
val[2] += lv[2] - in[li*c+2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
}
}
//!
//! \fn void total_blur_rgb(uchar * in, uchar * out, int w, int h, int c, int r)
//!
//! \brief this function performs the total blur pass for box blur.
//!
//! \param[in,out] in source channel
//! \param[in,out] out target channel
//! \param[in] w image width
//! \param[in] h image height
//! \param[in] c image channels
//! \param[in] r box dimension
//!
void total_blur_rgb(uchar * in, uchar * out, int w, int h, int c, int r)
{
// radius range on either side of a pixel + the pixel itself
float iarr = 1.f / (r+r+1);
#pragma omp parallel for
for(int i=0; i<w; i++)
{
int ti = i;
int li = ti;
int ri = ti+r*w;
int fv[3] = {in[ti*c+0], in[ti*c+1], in[ti*c+2] };
int lv[3] = {in[(ti+w*(h-1))*c+0], in[(ti+w*(h-1))*c+1], in[(ti+w*(h-1))*c+2] };
int val[3] = {(r+1)*fv[0], (r+1)*fv[1], (r+1)*fv[2] };
for(int j=0; j<r; j++)
{
val[0] += in[(ti+j*w)*c+0];
val[1] += in[(ti+j*w)*c+1];
val[2] += in[(ti+j*w)*c+2];
}
for(int j=0; j<=r; j++, ri+=w, ti+=w)
{
val[0] += in[ri*c+0] - fv[0];
val[1] += in[ri*c+1] - fv[1];
val[2] += in[ri*c+2] - fv[2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
for(int j=r+1; j<h-r; j++, ri+=w, ti+=w, li+=w)
{
val[0] += in[ri*c+0] - in[li*c+0];
val[1] += in[ri*c+1] - in[li*c+1];
val[2] += in[ri*c+2] - in[li*c+2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
for(int j=h-r; j<h; j++, ti+=w, li+=w)
{
val[0] += lv[0] - in[li*c+0];
val[1] += lv[1] - in[li*c+1];
val[2] += lv[2] - in[li*c+2];
out[ti*c+0] = std::round(val[0]*iarr);
out[ti*c+1] = std::round(val[1]*iarr);
out[ti*c+2] = std::round(val[2]*iarr);
}
}
}
//!
//! \fn void box_blur_rgb(uchar * in, uchar * out, int w, int h, int c, int r)
//!
//! \brief this function performs a box blur pass.
//!
//! \param[in,out] in source channel
//! \param[in,out] out target channel
//! \param[in] w image width
//! \param[in] h image height
//! \param[in] c image channels
//! \param[in] r box dimension
//!
void box_blur_rgb(uchar *& in, uchar *& out, int w, int h, int c, int r)
{
std::swap(in, out);
horizontal_blur_rgb(out, in, w, h, c, r);
total_blur_rgb(in, out, w, h, c, r);
// Note to myself :
// here we could go anisotropic with different radiis rx,ry in HBlur and TBlur
}
//!
//! \fn void fast_gaussian_blur_rgb(uchar * in, uchar * out, int w, int h, int c, float sigma)
//!
//! \brief this function performs a fast Gaussian blur. Applying several
//! times box blur tends towards a true Gaussian blur. Three passes are sufficient
//! for good results. For further details please refer to :
//! http://blog.ivank.net/fastest-gaussian-blur.html
//!
//! \param[in,out] in source channel
//! \param[in,out] out target channel
//! \param[in] w image width
//! \param[in] h image height
//! \param[in] c image channels
//! \param[in] sigma gaussian std dev
//!
void fast_gaussian_blur_rgb(uchar *& in, uchar *& out, int w, int h, int c, float sigma)
{
// sigma conversion to box dimensions
int boxes[3];
std_to_box(boxes, sigma, 3);
box_blur_rgb(in, out, w, h, c, boxes[0]);
box_blur_rgb(out, in, w, h, c, boxes[1]);
box_blur_rgb(in, out, w, h, c, boxes[2]);
}
//! \code{.cpp}
int main(int argc, char * argv[])
{
if( argc < 2 ) exit(1);
const char * image_file = argv[1];
const float sigma = argc > 2 ? std::atof(argv[2]) : 3.;
const char * output_file = argc > 3 ? argv[3] : "blur.png";
// image loading
int width, height, channels;
uchar * image_data = stbi_load(argv[1], &width, &height, &channels, 0);
std::cout << "Source image: " << width<<"x" << height << " ("<<channels<<")" << std::endl;
if(channels < 3)
{
std::cout<< "Input images must be RGB images."<<std::endl;
exit(1);
}
// copy data
int size = width * height * channels;
// output channels r,g,b
uchar * new_image = new uchar[size];
uchar * old_image = new uchar[size];
// channels copy r,g,b
for(int i = 0; i < size; ++i)
old_image[i] = image_data[i];
// per channel filter
auto start = std::chrono::system_clock::now();
fast_gaussian_blur_rgb(old_image, new_image, width, height, channels, sigma);
auto end = std::chrono::system_clock::now();
// stats
float elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
std::cout << "time " << elapsed << "ms" << std::endl;
// channels copy r,g,b
for(int i = 0; i < size; ++i)
image_data[i] = (uchar) std::min((uchar)255, std::max((uchar)0, new_image[i]));
// save
std::string file(output_file);
std::string ext = file.substr(file.size()-3);
if( ext == "bmp" )
stbi_write_bmp(output_file, width, height, channels, image_data);
else if( ext == "jpg" )
stbi_write_jpg(output_file, width, height, channels, image_data, 90);
else
{
if( ext != "png" )
{
std::cout << "format '" << ext << "' not supported writing default .png" << std::endl;
file = file.substr(0, file.size()-4) + std::string(".png");
}
stbi_write_png(file.c_str(), width, height, channels, image_data, channels*width);
}
stbi_image_free(image_data);
// clean memory
delete[] new_image;
delete[] old_image;
return 0;
}
//! \endcode