Improving influence with convolution filters

Convolution filters are usually applied via image processing software, but we can use the same principles to change a grid's influence given a unit's value and its surroundings. In this recipe, we will explore a couple of algorithms to modify a grid using matrix filters.

Getting ready

It is important to have grasped the concept of influence maps before implementing this recipe, so that you can understand the context in which it is applied.

How to do it…

We will implement the Convolve function:

  1. Declare the Convolve function:
    public static void Convolve(
            float[,] matrix,
            ref float[,] source,
            ref float[,] destination)
    {
        // next steps
    }
  2. Initialize the variables for handling the computations and traversal of arrays:
    int matrixLength = matrix.GetLength(0);
    int size = (int)(matrixLength – 1) / 2;
    int height = source.GetLength(0);
    int width = source.GetLength(1);
    int I, j, k, m;
  3. Create the first loop for iterating over the destination and source grids:
    for (i = 0; i < width–- size; i++)
    {
        for (j = 0; j < height–- size; j++)
        {
            // next steps
        }
    }
  4. Implement the second loop for iterating over the filter matrix:
    destination[i, j] = 0f;
    for (k = 0; k < matrixLength; k++)
    {
        for (m = 0; m < matrixLength; m++)
        {
            int row = i + k–- size;
            int col = j + m–- size;
            float aux = source[row, col] * matrix[k,m];
            destination[i, j] += aux;
        }
    }

How it works…

We create a new grid to be swapped with the original source grid after the application of the matrix filter on each position. Then, we iterate over each position that is to be created as a destination grid and compute its result, taking the original grid's value and applying the matrix filter to it.

It is important to note that the matrix filter must be an odd-square array for the algorithm to work as expected.

There is more…

The following ConvolveDriver function helps us iterate using the Convolve function implemented previously:

  1. Declare the ConvolveDriver function:
    public static void ConvolveDriver(
            float[,] matrix,
            ref float[,] source,
            ref float[,] destination,
            int iterations)
    {
        // next steps
    }
  2. Create the auxiliary variables for holding the grids:
    float[,] map1;
    float[,] map2;
    int i;
  3. Swap the maps, regardless of whether the iterations are odd or even:
    if (iterations % 2 == 0)
    {
        map1 = source;
        map2 = destination;
    }
    else
    {
        destination = source;
        map1 = destination;
        map2 = source;
    }
  4. Apply the previous function during the iterations and swap:
    for (i = 0; i < iterations; i++)
    {
        Convolve(matrix, ref source, ref destination);
        float[,] aux = map1;
        map1 = map2;
        map2 = aux;
    }

See also

  • The Introducing influence maps recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.30