PIXELBANKv8.2.1
Menu

Box Blur

Implement a box blur, also known as a mean filter, to an image by averaging neighboring pixels. This technique is a fundamental concept in Linear Filtering for image processing.

The box blur operates by convolving an image with a kernel, a small matrix that slides over the entire image, computing a weighted average of neighboring pixels at each position. For a given kernel size, the kernel is typically a square matrix with all elements being equal, resulting in a uniform average of the neighboring pixels.

Here are the steps to apply the box blur:

  1. Define the kernel size and compute the kernel elements as 1/(kernel_size2)1 / (kernel\_size^2).
  2. Slide the kernel over the image, computing the average of neighboring pixels at each position.
  3. Assign the computed average to the corresponding pixel in the output image.
K=1kernel_size2(11...111...1............11...1)K = \frac{1}{kernel\_size^2}\begin{pmatrix} 1 & 1 &... & 1 \\ 1 & 1 &... & 1 \\... &... &... &... \\ 1 & 1 &... & 1 \end{pmatrix}

This technique is widely used in image processing applications to reduce noise.

Example:

Input:
image = [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]
kernel_size = 3
Output:
[[5.0]]
Reasoning:

Box blur (mean filter) averages all pixels in a neighborhood.

For a 3×33 \times 3 box filter on a 3×33 \times 3 image:

output[i,j]=1k2m,ninput[i+m,j+n]\text{output}[i,j] = \frac{1}{k^2} \sum_{m,n} \text{input}[i+m, j+n]

  1. Valid convolution produces 1×1 output:

    • Output size: (33+1)×(33+1)=1×1(3 - 3 + 1) \times (3 - 3 + 1) = 1 \times 1
  2. Calculate the average of all 9 pixels: mean=1+2+3+4+5+6+7+8+99\text{mean} = \frac{1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9}{9}

  3. Sum the values: sum=1+2+3+4+5+6+7+8+9=45\text{sum} = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

  4. Compute mean: mean=459=5.0\text{mean} = \frac{45}{9} = 5.0

  5. Result: [[5.0]][[5.0]]

The box blur smooths the image by replacing each pixel with the average of its neighborhood.

Constraints:

  • kernel_size is odd (3, 5, 7, etc.)
  • Round output to nearest integer
  • Use zero-padding
Editor

Test Results

0/0
Run code to see test results.