BoxBlur1D

The BoxBlur1D kernel does a one-dimensional horizontal box blur on its input. The Radius of the blur can be user-controlled. Since the box blur is a separable filter, two box blurs (one for each axis) can applied consecutively, resulting in a optimised 2D box blur.

// Copyright (c) 2024 The Foundry Visionmongers Ltd.  All Rights Reserved.
// WARNING: This file is referenced within Images/RangedAccess1D.rst and Kernels.rst. Line number references will need updating if changes are made to this file.
/// BoxBlur1D kernel: Applies a 1D box-blur on the input.
kernel BoxBlur1D : public ImageComputationKernel<eComponentWise>
{
  Image<eRead, eAccessRanged1D, eEdgeClamped> src;
  Image<eWrite, eAccessPoint> dst;

param:
  int radius;   //The radius of the box blur

local:
  int filterWidth;

  void define() {
    defineParam(radius, "Radius", 5, eParamProxyScale);
  }

  void init() {
    // Set the range we need to access from the source 
    src.setRange(-radius, radius);

    // Set the axis for the 1D-range to be horizontal
    src.setAxis(eX);

    filterWidth = 2 * radius + 1;
  }

  void process() {
    // Sum all the pixel values within radius
    float sum = 0.0f;
    for(int i = -radius; i <= radius; ++i) {
      sum += src(i);
    }

    // Write out the average value
    dst() = sum / filterWidth;
  }
};