/
convolve.hpp
431 lines (392 loc) · 15.1 KB
/
convolve.hpp
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
//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_NUMERIC_CONVOLVE_HPP
#define BOOST_GIL_EXTENSION_NUMERIC_CONVOLVE_HPP
#include <boost/gil/extension/numeric/algorithm.hpp>
#include <boost/gil/extension/numeric/kernel.hpp>
#include <boost/gil/extension/numeric/pixel_numeric_operations.hpp>
#include <boost/gil/algorithm.hpp>
#include <boost/gil/image_view_factory.hpp>
#include <boost/gil/metafunctions.hpp>
#include <boost/assert.hpp>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <type_traits>
#include <vector>
namespace boost { namespace gil {
// 2D spatial seperable convolutions and cross-correlations
/// \ingroup ImageAlgorithms
/// \brief Boundary options for 1D cross-correlation and convolution
/// Use it as `enum class` with mutually exclusive constants, not as bitfields.
/// \todo TODO: Change to enum class?
enum /*class*/ convolve_boundary_option
{
convolve_option_output_ignore, /// do nothing to the output
convolve_option_output_zero, /// set the output to zero
convolve_option_extend_padded, /// assume the source boundaries to be padded already
convolve_option_extend_zero, /// assume the source boundaries to be zero
convolve_option_extend_constant /// assume the source boundaries to be the boundary value
};
namespace detail {
/// \brief Compute the cross-correlation of 1D kernel with the rows of an image
/// \tparam PixelAccum - TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel - TODO
/// \tparam DstView Models MutableImageViewConcept
/// \tparam Correlator - TODO
/// \param src_view
/// \param kernel - TODO
/// \param dst_view Destination where new computed values of pixels are assigned to
/// \param option - TODO
/// \param correlator - TODO
template
<
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView,
typename Correlator
>
void correlate_rows_impl(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option,
Correlator correlator)
{
BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions());
BOOST_ASSERT(kernel.size() != 0);
if(kernel.size() == 1)
{
// Reduces to a multiplication
view_multiplies_scalar<PixelAccum>(src_view, *kernel.begin(), dst_view);
return;
}
using src_pixel_ref_t = typename pixel_proxy<typename SrcView::value_type>::type;
using dst_pixel_ref_t = typename pixel_proxy<typename DstView::value_type>::type;
using x_coord_t = typename SrcView::x_coord_t;
using y_coord_t = typename SrcView::y_coord_t;
x_coord_t const width = src_view.width();
y_coord_t const height = src_view.height();
if (width == 0)
return;
PixelAccum acc_zero;
pixel_zeros_t<PixelAccum>()(acc_zero);
if (option == convolve_option_output_ignore || option == convolve_option_output_zero)
{
typename DstView::value_type dst_zero;
pixel_assigns_t<PixelAccum, dst_pixel_ref_t>()(acc_zero, dst_zero);
if (width < static_cast<x_coord_t>(kernel.size()))
{
if (option == convolve_option_output_zero)
fill_pixels(dst_view, dst_zero);
}
else
{
std::vector<PixelAccum> buffer(width);
for (y_coord_t y = 0; y < height; ++y)
{
assign_pixels(src_view.row_begin(y), src_view.row_end(y), &buffer.front());
typename DstView::x_iterator it_dst = dst_view.row_begin(y);
if (option == convolve_option_output_zero)
std::fill_n(it_dst, kernel.left_size(), dst_zero);
it_dst += kernel.left_size();
correlator(&buffer.front(), &buffer.front() + width + 1 - kernel.size(),
kernel.begin(), it_dst);
it_dst += width + 1 - kernel.size();
if (option == convolve_option_output_zero)
std::fill_n(it_dst, kernel.right_size(), dst_zero);
}
}
}
else
{
std::vector<PixelAccum> buffer(width + kernel.size() - 1);
for (y_coord_t y = 0; y < height; ++y)
{
PixelAccum *it_buffer = &buffer.front();
if (option == convolve_option_extend_padded)
{
assign_pixels(
src_view.row_begin(y) - kernel.left_size(),
src_view.row_end(y) + kernel.right_size(),
it_buffer);
}
else if (option == convolve_option_extend_zero)
{
std::fill_n(it_buffer, kernel.left_size(), acc_zero);
it_buffer += kernel.left_size();
assign_pixels(src_view.row_begin(y), src_view.row_end(y), it_buffer);
it_buffer += width;
std::fill_n(it_buffer, kernel.right_size(), acc_zero);
}
else if (option == convolve_option_extend_constant)
{
PixelAccum filler;
pixel_assigns_t<src_pixel_ref_t, PixelAccum>()(*src_view.row_begin(y), filler);
std::fill_n(it_buffer, kernel.left_size(), filler);
it_buffer += kernel.left_size();
assign_pixels(src_view.row_begin(y), src_view.row_end(y), it_buffer);
it_buffer += width;
pixel_assigns_t<src_pixel_ref_t, PixelAccum>()(src_view.row_end(y)[-1], filler);
std::fill_n(it_buffer, kernel.right_size(), filler);
}
correlator(
&buffer.front(), &buffer.front() + width,
kernel.begin(),
dst_view.row_begin(y));
}
}
}
template <typename PixelAccum>
class correlator_n
{
public:
correlator_n(std::size_t size) : size_(size) {}
template <typename SrcIterator, typename KernelIterator, typename DstIterator>
void operator()(
SrcIterator src_begin,
SrcIterator src_end,
KernelIterator kernel_begin,
DstIterator dst_begin)
{
correlate_pixels_n<PixelAccum>(src_begin, src_end, kernel_begin, size_, dst_begin);
}
private:
std::size_t size_{0};
};
template <std::size_t Size, typename PixelAccum>
struct correlator_k
{
template <typename SrcIterator, typename KernelIterator, typename DstIterator>
void operator()(
SrcIterator src_begin,
SrcIterator src_end,
KernelIterator kernel_begin,
DstIterator dst_begin)
{
correlate_pixels_k<Size, PixelAccum>(src_begin, src_end, kernel_begin, dst_begin);
}
};
} // namespace detail
/// \ingroup ImageAlgorithms
/// \brief Correlate 1D variable-size kernel along the rows of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void correlate_rows(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
detail::correlate_rows_impl<PixelAccum>(
src_view, kernel, dst_view, option, detail::correlator_n<PixelAccum>(kernel.size()));
}
/// \ingroup ImageAlgorithms
/// \brief Correlate 1D variable-size kernel along the columns of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void correlate_cols(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
correlate_rows<PixelAccum>(
transposed_view(src_view), kernel, transposed_view(dst_view), option);
}
/// \ingroup ImageAlgorithms
/// \brief Convolve 1D variable-size kernel along the rows of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve_rows(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
correlate_rows<PixelAccum>(src_view, reverse_kernel(kernel), dst_view, option);
}
/// \ingroup ImageAlgorithms
/// \brief Convolve 1D variable-size kernel along the columns of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve_cols(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
convolve_rows<PixelAccum>(
transposed_view(src_view), kernel, transposed_view(dst_view), option);
}
/// \ingroup ImageAlgorithms
/// \brief Convolve 1D variable-size kernel along both rows and columns of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve_1d(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
convolve_rows<PixelAccum>(src_view, kernel, dst_view, option);
convolve_cols<PixelAccum>(dst_view, kernel, dst_view, option);
}
/// \ingroup ImageAlgorithms
/// \brief Correlate 1D fixed-size kernel along the rows of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void correlate_rows_fixed(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
using correlator = detail::correlator_k<Kernel::static_size, PixelAccum>;
detail::correlate_rows_impl<PixelAccum>(src_view, kernel, dst_view, option, correlator{});
}
/// \ingroup ImageAlgorithms
/// \brief Correlate 1D fixed-size kernel along the columns of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum,typename SrcView,typename Kernel,typename DstView>
BOOST_FORCEINLINE
void correlate_cols_fixed(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
correlate_rows_fixed<PixelAccum>(
transposed_view(src_view), kernel, transposed_view(dst_view), option);
}
/// \ingroup ImageAlgorithms
/// \brief Convolve 1D fixed-size kernel along the rows of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve_rows_fixed(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
correlate_rows_fixed<PixelAccum>(src_view, reverse_kernel(kernel), dst_view, option);
}
/// \ingroup ImageAlgorithms
/// \brief Convolve 1D fixed-size kernel along the columns of image
/// \tparam PixelAccum TODO
/// \tparam SrcView Models ImageViewConcept
/// \tparam Kernel TODO
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve_cols_fixed(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
convolve_boundary_option option = convolve_option_extend_zero)
{
convolve_rows_fixed<PixelAccum>(
transposed_view(src_view), kernel, transposed_view(dst_view), option);
}
namespace detail
{
template <typename SrcView, typename DstView, typename Kernel>
void convolve_2d_impl(SrcView const& src_view, DstView const& dst_view, Kernel const& kernel)
{
int flip_ker_row, flip_ker_col, row_boundary, col_boundary;
float aux_total;
for (std::ptrdiff_t view_row = 0; view_row < src_view.height(); ++view_row)
{
for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
{
aux_total = 0.0f;
for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row)
{
flip_ker_row = kernel.size() - 1 - kernel_row; // row index of flipped kernel
for (std::size_t kernel_col = 0; kernel_col < kernel.size(); ++kernel_col)
{
flip_ker_col = kernel.size() - 1 - kernel_col; // column index of flipped kernel
// index of input signal, used for checking boundary
row_boundary = view_row + (kernel.center_vertical() - flip_ker_row);
col_boundary = view_col + (kernel.center_horizontal() - flip_ker_col);
// ignore input samples which are out of bound
if (row_boundary >= 0 && row_boundary < src_view.height() &&
col_boundary >= 0 && col_boundary < src_view.width())
{
aux_total +=
src_view(col_boundary, row_boundary) *
kernel.at(flip_ker_row, flip_ker_col);
}
}
}
dst_view(view_col, view_row) = aux_total;
}
}
}
} //namespace detail
/*!
convolve_2d can only use convolve_option_extend_zero as convolve_boundary_option
this is the default option and cannot be changed for now
(In future there are plans to improve the algorithm and allow user to use other options as well)
*/
template <typename SrcView, typename DstView, typename Kernel>
void convolve_2d(SrcView const& src_view, Kernel const& kernel, DstView const& dst_view)
{
BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions());
BOOST_ASSERT(kernel.size() != 0);
gil_function_requires<ImageViewConcept<SrcView>>();
gil_function_requires<MutableImageViewConcept<DstView>>();
static_assert(color_spaces_are_compatible
<
typename color_space_type<SrcView>::type,
typename color_space_type<DstView>::type
>::value, "Source and destination views must have pixels with the same color space");
for (std::size_t i = 0; i < src_view.num_channels(); i++)
{
detail::convolve_2d_impl(
nth_channel_view(src_view, i),
nth_channel_view(dst_view, i),
kernel
);
}
}
}} // namespace boost::gil
#endif