NumPy · Chapter 17 of 18

Grids and images

By the end of this chapter you can make arrays of coordinates with np.indices, np.meshgrid, np.mgrid and np.ogrid (and avoid meshgrid's rows-and-columns trap), compute a value for every point of a grid such as a distance with np.hypot, treat a thermal image and a colour photo as the arrays they are, crop, flip and rotate them, turn three channels into one with a weighted sum over the last axis, find hot spots with a mask and argmax, and change a uint8 image without wrapping round. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real work
  • 4 tasks
  • NumPy
  • In the all labs plan

The first 2 chapters of NumPy are free. This one comes with the all labs plan.

Why you need this

Bhanwar flew the thermal drone over block C this morning. Its camera does not see panels: it sees 19,200 temperatures, a (120, 160) array of °C, and somewhere in it are two cell hot spots and a failed diode. He also took a colour photo of two rows for the dust, a (180, 320, 3) array of whole numbers from 0 to 255. Both are arrays, so everything you know works on them; what is new is thinking of rows and columns as places.

Rukmini asks a question about places too: the cables from the far panels to the inverter station are long, and long cables lose power. "How far is every panel from the station?" Each of the 2,400 panels needs its own row and column number to work out its position, and so far the only way you have to give it one is a loop over rows and columns. NumPy makes the coordinates themselves into arrays, and then the distance is one line of arithmetic, for every panel at once.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    hot_spot.py: the hottest pixel, and its panel

    Write two functions in hot_spot.py.

    hottest_pixel(image): image is a 2-D float array (the thermal image is (120, 160), in °C). Return the row and the column of its hottest pixel as two whole numbers (a tuple of two is fine): for an image whose hottest pixel is at row 37, column 3, (37, 3). The Check tries the thermal image and a made-up (50, 70) image.

    panel_at(pixel_row, pixel_col, first_row, first_col): the image shows a rectangle of panels whose top left panel is plant row first_row, column first_col, and each panel fills 30 rows by 20 columns of pixels (its border included). Return the id of the panel the pixel lies on, as a string: panel (r, c) of the plant is P followed by r × 60 + c + 1 in four digits. panel_at(0, 0, 0, 0) is 'P0001'; pixel (31, 0) with first_row 20 and first_col 10 is on panel (21, 10), 'P1271'. The Check tries the thermal image's hottest pixel with 20 and 10, the last pixel of a panel (29, 19), the first of the next (30, 20), the image's last pixel (119, 159), and other first rows and columns.

  2. Task 2

    gray.py: the photo in grayscale

    Write to_gray(photo) in gray.py. photo is a colour image of shape (height, width, 3), dtype uint8, with red, green and blue in the last axis. Each pixel's gray is 0.299 × red + 0.587 × green + 0.114 × blue, rounded to the nearest whole number. Return an array of shape (height, width) with dtype uint8: for the drone's (180, 320, 3) photo, (180, 320). (The Check allows a difference of 1 on any pixel, because correct ways of summing can round a value that is very close to .5 either way.)

    The Check tries the drone's photo, a (2, 2, 3) image with a white pixel, a black one and two coloured ones, and a (60, 40, 3) crop of the photo.

  3. Task 3

    distance.py: every panel's distance from the station

    Write panel_distances(n_rows, n_cols, station_y, station_x) in distance.py. Panel (r, c) sits at y = 5.0 × r metres and x = 1.2 × c metres (rows are 5.0 m apart, panels in a row 1.2 m apart). Return an array of shape (n_rows, n_cols) of floats: each panel's straight-line distance in metres from the point (station_y, station_x). panel_distances(1, 2, 0.0, 0.0) is [[0. 1.2]].

    The plant's station is at y = 97.5, x = -8.0. The Check tries (40, 60, 97.5, -8.0), (3, 4, 0.0, 0.0), (1, 5, 0.0, 2.4) and (6, 2, 12.0, 30.0). Use no Python loop: a for, a while or a comprehension in panel_distances fails the task.

  4. Task 4 · interview question

    Interview: brighten.py, + 60 turned the sky yellow

    Bhanwar brightened the drone's photo by adding 60 to every channel. The sky turned yellow-green, the sand turned turquoise, and brighten(photo, -80) stops with OverflowError: Python integer -80 out of bounds for uint8.

    Fix brighten(photo, amount) in brighten.py. photo is a colour image of shape (height, width, 3), dtype uint8; amount is a whole number from -255 to 255. Add amount to every channel of every pixel; a value above 255 becomes 255 and a value below 0 becomes 0. Return an array of the photo's shape with dtype uint8: for a pixel [250, 10, 128] and amount 10, [255, 20, 138]. The Check tries the drone's photo with 60 and with -80, and a (1, 2, 3) image with 10 and with 0.

    Imran will ask: why did + 60 turn the sky yellow, and what has to happen before the add?

What you will learn

  • np.indices, np.meshgrid, np.mgrid: arrays that hold each point's row or column.
  • indexing="xy" against "ij": why meshgrid's grids come out the other way round.
  • np.ogrid: open grids, a column and a row that broadcast into the full grid.
  • Distance fields with np.hypot on coordinate grids.
  • Images are arrays: a thermal image of floats, a photo of uint8 with three channels; cropping, np.flip and np.rot90.
  • A weighted sum over the last axis: three channels into one number a pixel.
  • The uint8 trap: adding to an image wraps round.

More in Real work

Build "Grids and images" yourself

DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of NumPy are free. This one comes with the all labs plan.