Skip to content

Commit

Permalink
Added 2D convolution definitions to numeric extension
Browse files Browse the repository at this point in the history
`convolve` function renamed to `convolve_1d`
  • Loading branch information
miralshah365 committed Aug 5, 2019
1 parent 308e4ea commit 27ae550
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion include/boost/gil/extension/numeric/convolve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void convolve_cols(
/// \tparam DstView Models MutableImageViewConcept
template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
BOOST_FORCEINLINE
void convolve(
void convolve_1d(
SrcView const& src_view,
Kernel const& kernel,
DstView const& dst_view,
Expand Down Expand Up @@ -355,6 +355,43 @@ void convolve_cols_fixed(
transposed_view(src_view), kernel, transposed_view(dst_view), option);
}

template <typename SrcView, typename DstView, typename Kernel>
void convolve_2d(SrcView src_view, DstView dst_view, Kernel 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) // rows
{
for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col) // columns
{
aux_total = 0.0f;
for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row) // kernel rows
{
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) // kernel columns
{
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 boost::gil

#endif
2 changes: 1 addition & 1 deletion include/boost/gil/image_processing/threshold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void threshold_adaptive
typename image<typename SrcView::value_type>::view_t temp_view = view(temp_img);
SrcView temp_conv(temp_view);

convolve<pixel<float, typename SrcView::value_type::layout_t>>(
convolve_1d<pixel<float, typename SrcView::value_type::layout_t>>(
src_view, kernel, temp_view
);

Expand Down

0 comments on commit 27ae550

Please sign in to comment.