Kumita← Open visual lesson

Slide the Kernel

The code behind the picture

Follow the lesson’s implementation, one code cell at a time.

Choose a language and read the cells in order. These are small teaching examples; the visual lesson runs separately.

Follow the calculation

Same inputs · Same operation · Same result

3 teaching cells · Python standard library · Read-only

  1. 1. Prepare the patch and the kernel

    The 3 × 3 patch of the watermelon picture at the melon's right outline (brightness 0–9), and the kernel whose nine weights will be placed over it.

    import json
    
    patch = [[0, 8, 8], [0, 8, 8], [5, 0, 8]]
    kernel = [[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]
  2. 2. Multiply matching cells and add

    Each weight multiplies the pixel directly under it, and the nine products are added into one response.

    result = sum(
        patch[i][j] * kernel[i][j]
        for i in range(3)
        for j in range(3)
    )
  3. 3. Inspect the result

    Print the result as JSON so both languages use the same output format.

    print(json.dumps(result))

Expected result

Running the three cells in order produces this result in either language.

19