Search code examples
halide

How to initialize External Image with Margins for convolution?


Constraints: 1. Have a pointer to an image with margins of size (ImHeight,ImWidth) 2. Filter size (FH,FW) ; FH,FW are odd 3. ActualImageHeight = ImHeight-2*(FH/2); ActualImageWidth = ImWidth-2*(FW/2);

How To:

  1. Initialize image with the pointer such that image(0,0) is pixel(0,0) and not the margin pixel ?
  2. Define a schedule without using boundary conditions / clamping - since the given image pointer memory already accounts for the margins

Solution

  • Modify the minimum coordinate of the image. You didn't state if you are using JIT or AOT, but here is a JIT implementation.

    Halide::Image input( ImWidth + 2 * FW, ImHeight + 2 * FH ), output;
    input.set_min( -FW, -FH );
    Func f;
    f(x,y) = ( input( x - FW, y - FH ) + input( x + FW - 1, y + FH - 1 ) ) / 2;
    output = f.realize( ImWidth, ImHeight );
    

    For AOT:

    • Use ImageParam for input.
    • Use Param<int> for ImWidth and ImHeight to have them be parameters to the AOT function.
    • Use int for ImWidth and ImHeight to have them be baked into the the AOT function.
    • Use set_bounds and set_stride for all dimensions of input and f.output_buffer(). These take Exprs so will accept ImWidth + 2 * FW if ImWidth is a Param<int>.