1

I'm trying to develop a sobel edge detector using Halide.

Because I need to compute the 3x3 square around each pixel, I'm trying to realize the function over a smaller rectangle (and avoid the outer border). Here's what I came up with so far:

Buffer<uint8_t> image;
//Read image into buffer (omitted)
gamma(x, y, c) = cast<uint8_t>(255 * pow(image(x, y, c) * 1.0f / 255, gamma_exponent)); //Gamma correction
//Sobel edge detector
h(x, y, c) = gamma(x + 1, y - 1, c) + 2 * gamma(x + 1, y, c) +
             gamma(x + 1, y + 1, c) - gamma(x - 1, y - 1, c) -
             2 * gamma(x - 1, y, c) - gamma(x - 1, y + 1, c);
v(x, y, c) = -gamma(x - 1, y + 1, c) - 2 * gamma(x, y + 1, c) -
             gamma(x + 1, y + 1, c) + gamma(x - 1, y - 1, c) +
             2 * gamma(x, y - 1, c) + gamma(x + 1, y - 1, c);
sobel_ed(x, y, c) = (h(x, y, c) + v(x, y, c)) / 4;
sobel_bounded(x, y, c) = BoundaryConditions::constant_exterior(sobel_ed, 0, 2, image.width() - 4, 2, image.height() - 4)(x, y, c);
Buffer<uint8_t> tmp(image.width() - 4, image.height() - 4, image.channels());
tmp.set_min(2, 2, 0);
sobel_bounded.realize(tmp);

When I run this code, I get the following error:

Error: Input buffer b0 is accessed at -1, which is before the min (0) in dimension 0

It seems like Halide evaluates the statement for every value of x and y, even though I set boundary conditions. What can I do?

  • This should be taken as the perfect opportunity to learn how to *debug* your programs. For example print the values of all involved variables. Will any of them be `-1`? Or `0` (which together with `- 1` will lead to the problem)? – Some programmer dude Dec 11 '21 at 16:23
  • 1
    @Someprogrammerdude I'm guessing that'll be less trivial than with regular C/C++ because this is *halide*, yet there is https://halide-lang.org/tutorials/tutorial_lesson_04_debugging_2.html – Christoph Rackwitz Dec 11 '21 at 17:16
  • Can you turn this into a _full_ MRE? The core issue is that you're applying the boundary condition to the _output_ rather than to the _input_. – Alex Reinking Dec 12 '21 at 07:55

0 Answers0