BoxBlur2D

The BoxBlur2D kernel does a two-dimensional box blur on its input. The horizontal radius, RadiusX, and vertical radius, RadiusY can be separately controlled.

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

param:
  int xRadius;  //The horizontal radius of the box blur
  int yRadius;  //The vertical radius of the box blur

local:
  int filterSize;

  void define() {
    defineParam(xRadius, "RadiusX", 5, eParamProxyScale);
    defineParam(yRadius, "RadiusY", 3, eParamProxyScale);
  }

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

    filterSize = (2 * xRadius + 1) * (2 * yRadius + 1);
  }

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

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