Skip to content

Commit

Permalink
fix: Convolution in convolve_2d (#723)
Browse files Browse the repository at this point in the history
Fixes #722
  • Loading branch information
cgringmuth authored Feb 7, 2023
1 parent 1df8c24 commit 712b827
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/boost/gil/detail/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace boost { namespace gil { namespace detail {

static constexpr double pi = 3.14159265358979323846;

static constexpr std::array<float, 9> dx_sobel = {{-1, 0, 1, -2, 0, 2, -1, 0, 1}};
static constexpr std::array<float, 9> dx_sobel = {{1, 0, -1, 2, 0, -2, 1, 0, -1}};
static constexpr std::array<float, 9> dx_scharr = {{-1, 0, 1, -1, 0, 1, -1, 0, 1}};
static constexpr std::array<float, 9> dy_sobel = {{1, 2, 1, 0, 0, 0, -1, -2, -1}};
static constexpr std::array<float, 9> dy_scharr = {{1, 1, 1, 0, 0, 0, -1, -1, -1}};
Expand Down
2 changes: 1 addition & 1 deletion include/boost/gil/image_processing/convolve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void convolve_2d_impl(SrcView const& src_view, DstView const& dst_view, Kernel c
{
aux_total +=
src_view(col_boundary, row_boundary)[0] *
kernel.at(flip_ker_row, flip_ker_col);
kernel.at(flip_ker_col, flip_ker_row);
}
}
}
Expand Down
93 changes: 93 additions & 0 deletions test/core/image_processing/convolve_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,102 @@ gil::detail::convolve_2d(src_view, kernel, view(img_gray_out));
BOOST_TEST(gil::equal_pixels(exp_out_view, view(img_gray_out)));
}

void test_convolve_2d_with_sobel_x_filter()
{
const gil::uint8_t img[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

const gil::int16_t exp_output[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 0, 0, -255, -255, 0, 0, 0,
0, 765, 765, 0, 0, -765, -765, 0, 0, 0,
0, 1020, 1020, 0, 0, -1020, -1020, 0, 0, 0,
0, 1020, 1020, 0, 0, -1020, -1020, 0, 0, 0,
0, 1020, 1020, 0, 0, -1020, -1020, 0, 0, 0,
0, 765, 765, 0, 0, -765, -765, 0, 0, 0,
0, 255, 255, 0, 0, -255, -255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

gil::gray16s_image_t img_gray_out(10, 13);
const auto src_view =
gil::interleaved_view(10, 13, reinterpret_cast<const gil::gray8_pixel_t*>(img), 10);
const auto exp_out_view =
gil::interleaved_view(10, 13, reinterpret_cast<const gil::gray16s_pixel_t*>(exp_output), 20);
gil::detail::convolve_2d(src_view, gil::generate_dx_sobel(1), view(img_gray_out));

BOOST_TEST(gil::equal_pixels(exp_out_view, view(img_gray_out)));
}

void test_convolve_2d_with_sobel_y_filter()
{
const gil::uint8_t img[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

const gil::int16_t exp_output[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 765, 1020, 1020, 765, 255, 0, 0, 0,
0, 255, 765, 1020, 1020, 765, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -255, -765, -1020, -1020, -765, -255, 0, 0, 0,
0, -255, -765, -1020, -1020, -765, -255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

gil::gray16s_image_t img_gray_out(10, 13);
const auto src_view =
gil::interleaved_view(10, 13, reinterpret_cast<const gil::gray8_pixel_t*>(img), 10);
const auto exp_out_view =
gil::interleaved_view(10, 13, reinterpret_cast<const gil::gray16s_pixel_t*>(exp_output), 20);
gil::detail::convolve_2d(src_view, gil::generate_dy_sobel(1), view(img_gray_out));
BOOST_TEST(gil::equal_pixels(exp_out_view, view(img_gray_out)));
}

int main()
{
test_convolve_2d_with_normalized_mean_filter();
test_convolve_2d_with_image_using_float32_t();
test_convolve_2d_with_sobel_x_filter();
test_convolve_2d_with_sobel_y_filter();
return ::boost::report_errors();
}

0 comments on commit 712b827

Please sign in to comment.