Milestone 3 of 8

Extract patches with coordinates and masks

Preserve image, origin, bounds, padding, indexes, group, patch values, and boolean validity masks while keeping each patch independent of its source.

An extracted patch must tell you both what it contains and where it came from. Keep source bounds, padding, and validity in the same record as the pixels.

Goal

Extract uint8 patches and boolean masks from each grid origin while preserving composite identity, half-open bounds, padding counts, and source array independence.

Patch record

One patch has identity (image_id, top, left) and stores:

  • half-open source bounds before clipping;
  • valid source bounds after clipping;
  • bottom and right padding counts;
  • per-image and global patch indexes;
  • image group and source-array identity;
  • a patch of shape (PH, PW, C) and dtype uint8; and
  • a boolean mask of shape (PH, PW).

The mask is true exactly where a source pixel was copied and false for padding. Fill invalid pixels with the configured per-channel uint8 values. Under drop, every mask value is true. Padding never occurs above or to the left.

Deliverables

Implement extraction in src/patches.py. Save selected patch arrays, masks, coordinate records, and expected pixel tables for tiny grayscale and RGB fixtures. Keep source and patch memory independent.

Checks

Check complete patches, bottom and right edge patches, an image smaller than a patch, a one-pixel image, overlapping origins, and all configured pad values. Check every copied pixel against its source coordinate and every filled pixel against the pad value. Check valid bounds, padding counts, shape, dtype, mask shape, composite identity, and indexes.

Reject source mutation, writable memory sharing, invalid origins, duplicate spatial identities, wrong channel count, and a mask that marks a padded pixel as valid. Check that drop patches have all-true masks.

Workspace

Keep extraction and patch-record construction in src/patches.py. Do not stack arrays or group patches yet.

Hints

HintClip bounds, not the identity
The identity keeps the original origin (top, left). Clip only the source bounds used to copy valid pixels.
HintThe mask is two-dimensional
One mask value describes whether a row-column position is valid for every channel. Channel values still fill the same pixel position independently.

Review

Choose a padded bottom-right patch and map each of its four corners to either a source coordinate or a pad value. Which fields let another program perform that check without opening the original image?

How to check your work

Checks compare pixels, masks, bounds, padding counts, identity, and memory independence with the tiny fixtures. Extraction preserves source evidence; it does not label the image or patch.

LLM PrimerExtract patches with coordinates and maskshttps://llmprimer.com/python/projects/build-an-image-patch-dataset/extract-patches-with-coordinates-and-masks© 2026 LLM Primer