14 #ifndef GIL_CONVOLVE_HPP 15 #define GIL_CONVOLVE_HPP 32 #include "../../gil_config.hpp" 33 #include "../../image_view_factory.hpp" 34 #include "../../algorithm.hpp" 35 #include "../../metafunctions.hpp" 37 #include "algorithm.hpp" 39 namespace boost {
namespace gil {
44 convolve_option_output_ignore,
53 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView,
typename Correlator>
56 Correlator correlator) {
57 assert(src.dimensions()==dst.dimensions());
58 assert(ker.size()!=0);
62 typedef typename Kernel::value_type kernel_type;
65 view_multiplies_scalar<PixelAccum>(src,*ker.begin(),dst);
69 int width=src.width(),height=src.height();
74 if (width<(
int)ker.size()) {
76 fill_pixels(dst,dst_zero);
78 std::vector<PixelAccum> buffer(width);
79 for(
int rr=0;rr<height;++rr) {
80 assign_pixels(src.row_begin(rr),src.row_end(rr),&buffer.front());
81 typename DstView::x_iterator it_dst=dst.row_begin(rr);
83 std::fill_n(it_dst,ker.left_size(),dst_zero);
84 it_dst+=ker.left_size();
85 correlator(&buffer.front(),&buffer.front()+width+1-ker.size(),
87 it_dst+=width+1-ker.size();
89 std::fill_n(it_dst,ker.right_size(),dst_zero);
93 std::vector<PixelAccum> buffer(width+ker.size()-1);
94 for(
int rr=0;rr<height;++rr) {
95 PixelAccum* it_buffer=&buffer.front();
97 assign_pixels(src.row_begin(rr)-ker.left_size(),
98 src.row_end(rr)+ker.right_size(),
101 std::fill_n(it_buffer,ker.left_size(),acc_zero);
102 it_buffer+=ker.left_size();
103 assign_pixels(src.row_begin(rr),src.row_end(rr),it_buffer);
105 std::fill_n(it_buffer,ker.right_size(),acc_zero);
109 std::fill_n(it_buffer,ker.left_size(),filler);
110 it_buffer+=ker.left_size();
111 assign_pixels(src.row_begin(rr),src.row_end(rr),it_buffer);
114 std::fill_n(it_buffer,ker.right_size(),filler);
116 correlator(&buffer.front(),&buffer.front()+width,
122 template <
typename PixelAccum>
127 correlator_n(std::size_t size_in) : _size(size_in) {}
128 template <
typename SrcIterator,
typename KernelIterator,
typename DstIterator>
129 void operator()(SrcIterator src_begin,SrcIterator src_end,
130 KernelIterator ker_begin,
131 DstIterator dst_begin) {
132 correlate_pixels_n<PixelAccum>(src_begin,src_end,ker_begin,_size,dst_begin);
135 template <std::
size_t Size,
typename PixelAccum>
136 struct correlator_k {
138 template <
typename SrcIterator,
typename KernelIterator,
typename DstIterator>
139 void operator()(SrcIterator src_begin,SrcIterator src_end,
140 KernelIterator ker_begin,
141 DstIterator dst_begin){
142 correlate_pixels_k<Size,PixelAccum>(src_begin,src_end,ker_begin,dst_begin);
149 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
153 detail::correlate_rows_imp<PixelAccum>(src,ker,dst,option,detail::correlator_n<PixelAccum>(ker.size()));
158 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
162 correlate_rows<PixelAccum>(transposed_view(src),ker,transposed_view(dst),option);
167 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
169 void convolve_rows(
const SrcView& src,
const Kernel& ker,
const DstView& dst,
176 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
178 void convolve_cols(
const SrcView& src,
const Kernel& ker,
const DstView& dst,
180 convolve_rows<PixelAccum>(transposed_view(src),ker,transposed_view(dst),option);
185 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
189 detail::correlate_rows_imp<PixelAccum>(src,ker,dst,option,detail::correlator_k<Kernel::static_size,PixelAccum>());
194 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
198 correlate_rows_fixed<PixelAccum>(transposed_view(src),ker,transposed_view(dst),option);
203 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
207 correlate_rows_fixed<PixelAccum>(src,
reverse_kernel(ker),dst,option);
212 template <
typename PixelAccum,
typename SrcView,
typename Kernel,
typename DstView>
216 convolve_rows_fixed<PixelAccum>(transposed_view(src),ker,transposed_view(dst),option);
void correlate_rows_imp(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option, Correlator correlator)
compute the correlation of 1D kernel with the rows of an image
void correlate_rows(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
Kernel reverse_kernel(const Kernel &kernel)
reverse a kernel
void correlate_cols(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
void correlate_rows_fixed(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
assume the source boundaries to be padded already
void convolve_cols(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
void convolve_cols_fixed(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
void convolve_rows_fixed(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
Returns the reference proxy associated with a type that has a "reference" member typedef.
Structures for pixel-wise numeric operations /.
assume the source boundaries to be zero
construct for setting a pixel to zero (for whatever zero means)
void convolve_rows(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)
void correlate_cols_fixed(const SrcView &src, const Kernel &ker, const DstView &dst, convolve_boundary_option option=convolve_option_extend_zero)