How would you implement a simple box blur on a grayscale image?

Spatial convolution and image filtering basics.
Iterate interior pixels, sum the N by N neighborhood, divide by kernel area, write to a new buffer.
In place updates that blur already blurred values.
WHAT THIS TESTS: This question checks whether you can move from a mathematical description of a filter to a working implementation. The interviewer cares about your understanding of discrete convolution in the spatial domain, memory access patterns, and boundary handling. It is not about knowing OpenCV or PIL; it is about nested loops, accumulator logic, and the difference between reading from source and writing to destination.
A GOOD ANSWER COVERS: First, state that you need two buffers: a source grayscale image and a destination image of identical width and height. Second, describe the iteration: for each pixel that is not on the border, define a neighborhood around it. For a 3 by 3 box blur, that means looking at the pixel itself plus its eight neighbors. Third, explain the accumulation: sum all intensity values in that neighborhood, then divide by the number of elements in the kernel, which is nine for a 3 by 3 kernel. Fourth, write the averaged value into the corresponding location in the destination buffer. Fifth, mention border handling explicitly, such as skipping edge pixels, clamping, or mirroring, so you never read outside the array bounds.
COMMON WRONG ANSWERS: A major red flag is proposing an in place update where you overwrite the source image while iterating. Doing so causes already blurred values to leak into the next neighborhood, turning the box blur into an unintended recursive filter. Another red flag is confusing the kernel with the image itself, for example by sliding the kernel matrix over the image and multiplying arbitrary weights instead of performing a uniform average. A third mistake is forgetting integer division or overflow issues when working with uint8 data; a strong candidate notes that the accumulator should use a larger type such as an integer or float before dividing and casting back.
LIKELY FOLLOW UPS: The interviewer may ask how to optimize the naive approach. You should mention the separable property of the box blur, which lets you perform two one dimensional passes instead of one two dimensional pass, reducing complexity from order of N squared times kernel area to order of N squared times kernel radius. They may also ask about the sliding window or summed area table techniques that let you update the neighborhood sum in constant time. Another follow up is how the filter behaves at edges and whether zero padding introduces dark borders.
ONE CONCRETE EXAMPLE: Suppose the source image is 5 pixels wide by 5 pixels tall and you use a 3 by 3 kernel. The center pixel at row 2 column 2 has neighbors from rows 1 through 3 and columns 1 through 3. You add those nine grayscale values, divide by 9, and place the result at destination row 2 column 2. Pixels in row 0, row 4, column 0, and column 4 are left unprocessed or handled with a chosen border strategy. The entire operation is linear and shift invariant, meaning the same arithmetic applies uniformly across the whole image.
Source: Wikipedia: Box blur
Read the original → Wikipedia: Box blur
- #computer vision
- #image processing
- #convolution
- #spatial filtering
- #algorithms
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.