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?