Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 49 ยท Projects

Project: Image Filters from Scratch

Build blur, sharpen and edge-detection filters with plain NumPy. Make a synthetic image, slide windows with sliding_window_view, and tune brightness and contrast.

Photo apps feel like magic, but most of their basic filters are small pieces of arithmetic. A blur replaces each pixel with the average of its neighbors. An edge detector measures how quickly brightness changes from one pixel to the next. Brightness and contrast are a single multiply and add. All of this is array math, which is exactly what NumPy is for.

In this project you will build a tiny image-filter toolkit with nothing but NumPy and Matplotlib for display. You will create your own test image (no files needed), write a reusable convolve2d function with sliding_window_view, blur and sharpen the picture, detect edges, and adjust brightness and contrast. If images as arrays are new to you, read Arrays as Images and Plots first.

Step 1: a synthetic test image

A grayscale image is a 2D array: one number per pixel, where 0.0 is black and 1.0 is white. We will use floats between 0 and 1 because the math is easier than with 8-bit integers, which wrap around when they overflow (see Data Types).

Our test image has a soft horizontal gradient as background, a bright disk and a darker square. Hard shapes give edge detectors something to find. We build it with broadcasting and boolean masks, no loops:

Python Build a 64 by 64 test image

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

np.mgrid[0:size, 0:size] returns two arrays of shape (64, 64): y holds the row number of every pixel and x the column number. The disk is every pixel whose distance from the center (22, 24) is at most 11, the circle equation turned into a boolean mask. Assigning through the mask paints exactly those pixels.

flowchart LR
    A["Image<br/>(64, 64)"] --> B["Pad edges<br/>(66, 66)"]
    B --> C["Sliding windows<br/>(64, 64, 3, 3)"]
    C --> D["Multiply by kernel<br/>and sum per window"]
    D --> E["Filtered image<br/>(64, 64)"]

Every filter in this lesson follows the same pipeline: pad the image, cut it into overlapping windows, combine each window with a small kernel.

Step 2: convolution with sliding windows

Most image filters are convolutions (strictly speaking, cross-correlations, which is what image libraries actually compute). A small grid of weights called a kernel slides over the image. At each position you multiply the kernel with the patch of pixels underneath it, add up the products, and write the sum to the output pixel.

You could write two nested Python loops over all pixels, and for a 64 by 64 image that would even work. But NumPy can do the whole thing in one expression. np.lib.stride_tricks.sliding_window_view gives a read-only view of every k by k patch without copying any data. Then np.einsum multiplies all patches by the kernel and sums them at once. For the notation, see Einstein Summation.

Python A reusable convolve2d function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The single bright pixel has spread into a 3 by 3 patch of value 1.0 each: that is a blur. Notice the total brightness stays the same, because the kernel weights add up to 1. Any smoothing kernel should do that, or your image will get brighter or darker.

Two details matter. Padding: at the border the window would hang off the image, so we pad first. mode="edge" repeats the border pixel, which avoids dark halos; mode="constant" (zeros) would darken the edges. Window shape: the view has shape (H, W, kh, kw), meaning position (i, j) holds the kh by kw patch around that pixel. Because it is a view over the padded array, it costs almost no memory even though it looks like it holds nine times more numbers.

sliding_window_view returns a read-only view. Never try to assign into it, and remember that operations like einsum still read the same memory cells many times. That is fine for small kernels; for very large kernels, FFT-based convolution (see Signals and the FFT) is faster.

Step 3: blur and sharpen

With convolve2d ready, new filters are just new kernels. A bigger box kernel blurs more. A sharpen kernel does the opposite: it boosts the center pixel and subtracts the neighbors, which exaggerates local differences. Its weights also sum to 1, so overall brightness is preserved.

Python Blur and sharpen kernels side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the printed rows across the left edge of the square. The original jumps abruptly from the dark background to 0.6. The 3 by 3 blur turns that jump into a short ramp; the 7 by 7 blur makes a longer ramp. The sharpen result overshoots on both sides of the edge (a little darker before it, a little brighter after it), which is exactly why sharpened photos look crisper, and also why too much sharpening creates halos. np.clip keeps the result in the valid 0 to 1 range, because the sharpen kernel can push values outside it.

Step 4: edge detection

An edge is a place where brightness changes quickly. The Sobel operator estimates how fast brightness changes in each direction using two kernels. One responds to horizontal change (vertical edges), the other to vertical change (horizontal edges). Combine the two responses with the Pythagorean theorem to get the edge strength at every pixel.

Python Sobel edge detection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The faint background gradient produces a small, uniform response everywhere, so we threshold: only pixels above 0.25 of the maximum count as edges. The outlines of the disk and the square appear as thin lines, and the flat area prints a value close to zero. Notice we reused sobel_x.T: rotating the kernel with a transpose is free (see Reshaping Arrays).

Step 5: brightness and contrast

These do not need a window at all. They are pointwise operations, one formula applied to every pixel:

TEXT
output = (pixel - 0.5) * contrast + 0.5 + brightness

Brightness shifts every value up or down. Contrast stretches values away from mid-gray (greater than 1) or squeezes them toward it (less than 1). Always np.clip afterwards, otherwise values leave the 0 to 1 range and Matplotlib or an 8-bit conversion will behave unexpectedly.

Python Brightness and contrast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The standard deviation is a handy number for contrast: it grows with high contrast and shrinks with low contrast, while the mean tracks brightness. The final conversion multiplies by 255 and rounds before astype(np.uint8). Without the round, casting truncates toward zero and darkens the image slightly (see Converting and Casting Types).

Ideas to extend the project

  • Write a Gaussian kernel generator: np.exp(-(x**2 + y**2) / (2 * sigma**2)), normalized to sum to 1.
  • Add an emboss kernel and a "difference of two blurs" filter.
  • Make convolve2d work on color images by applying it to each channel with a loop over the last axis.
  • Compare your function with scipy.ndimage.convolve and check the results with np.allclose.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why must the weights of a blur kernel add up to 1?

    Each output pixel is a weighted sum. If the weights sum to 1, a flat region keeps its value and the average brightness does not change. Weights summing to more than 1 brighten the image, less than 1 darken it.
  2. 2. What does sliding_window_view(padded, (3, 3)) return for a padded array of shape (66, 66)?

    A 3 by 3 window fits 66 - 3 + 1 = 64 times along each axis, so the view has shape (64, 64, 3, 3). It is a read-only view over the same memory, not a copy.
  3. 3. Which line correctly combines the horizontal and vertical Sobel responses into one edge strength?

    np.hypot computes sqrt(gx2 + gy2) elementwise, the length of the gradient vector at each pixel. Adding the responses would let positive and negative values cancel each other.
  4. 4. Why do we call np.clip after sharpening or changing contrast?

    These operations can push values below 0 or above 1. Clipping keeps them valid, so display and 8-bit conversion behave predictably.

Summary

  • A grayscale image is a 2D float array in [0, 1]; shapes are built with np.mgrid, broadcasting and boolean masks.
  • A convolution slides a small kernel over the image: pad, take sliding_window_view, multiply and sum with einsum.
  • Blur kernels average neighbors (weights sum to 1); sharpen kernels boost the center and subtract neighbors.
  • Sobel kernels measure horizontal and vertical change; np.hypot merges them into edge strength.
  • Brightness and contrast are pointwise formulas followed by np.clip.

Next project: Linear Regression from Scratch.