boostorg / gil

Boost.GIL - Generic Image Library | Requires C++14 since Boost 1.80
https://boostorg.github.io/gil
Boost Software License 1.0
178 stars 164 forks source link

Convolution2d seems to be not correct. #722

Closed cgringmuth closed 1 year ago

cgringmuth commented 1 year ago

Actual behavior

If i provide the example sobel_scharr.cpp with following image: small_box2 I get (for sobel filter) Grad X Grad Y
output2-x output2-y

Which is not correct in my opinion.

Expected behavior

I have written a unit test which explains what I would expect from a 2 d convolution with sobel filter for the x direction.

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);
  auto src_view =
      gil::interleaved_view(10, 13, reinterpret_cast<const gil::gray8_pixel_t*>(img), 10);
  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)));
}

C++ Minimal Working Example

See unit test in Expected behavior.

Environment

Should be independent of environment. I didn't have any crash or something.

PS: I also don't know how to properly debug code in GIL properly. How I can see the content of an image in the debugger?

cgringmuth commented 1 year ago

I think I fixed the issue already. Will create PR.