Image data and vision tasks
Image data and task choices
Computer vision turns image or video measurements into useful predictions. A pixel is a sampled image location. A grayscale image may store one intensity per pixel; RGB commonly stores red, green and blue components per pixel. The shape H by W by C describes height, width and channel count, while C by H by W places channels first. A 4 by 4 RGB array has 48 scalar values, not 48 pixels. A batch adds a separate sample dimension. Channel order, color space, numeric type and range are all part of the input contract; the file format alone does not tell you the tensor layout.
Normalize input values
For a decoded 8-bit grayscale image, dividing each intensity by 255 maps zero to 0.0 and 255 to 1.0. It does not change the spatial size. This example uses rectangular nonempty lists of integer intensities and rejects invalid input. It does not decode files or normalize a floating-point HDR image. A trained model may instead expect channel-specific mean and standard deviation, a particular resolution or a different color order. Apply the same documented input pipeline at training and inference.
Scale grayscale pixels
def scale_image(image): if not image or not image[0]: raise ValueError("empty image") width = len(image[0]) if any(len(row) != width for row in image): raise ValueError("ragged image") if any(type(p) is not int or not 0 <= p <= 255 for row in image for p in row): raise ValueError("expected 8-bit grayscale values") return [[p / 255 for p in row] for row in image]
print(scale_image([[0, 255], [128, 64]]))print(scale_image([[255, 0, 255]]))The first printed grid contains 0.0, 1.0, about 0.502 and about 0.251. The second stays one row wide by three columns. New rows are created rather than modifying the original image.
Choose a representation for the job
Image classification predicts image-level categories, such as whether a tile contains a rectangle. Object detection produces individual regions, typically class labels, boxes and scores; it can help count objects if duplicate and missed detections are handled. Semantic segmentation assigns a class to each pixel. Instance segmentation additionally distinguishes individual objects of the same class. A box is a coarse region, while a mask can follow an irregular outline. A single multi-label classifier cannot generally tell how many separate instances of a category exist. CNNs and vision transformers can learn useful features from training data, but these examples use explicit deterministic rules rather than a trained model.
Build a box from a mask
In our 4 by 4 synthetic grid, the foreground occupies columns 1 and 2 and rows 1 and 2. Index rows by y and columns by x, starting at zero at the top left. Represent boxes as [x_min, y_min, x_max, y_max] with excluded upper bounds: [1,1,3,3] encloses those four cells. This convention uses the right and bottom edges, not the index of the last foreground pixel. The next function finds the enclosing box for all ones in a binary mask. If none exist it returns None.
Enclose foreground pixels
def mask_box(mask): if not mask or not mask[0]: raise ValueError("empty image") width = len(mask[0]) if any(len(row) != width for row in mask): raise ValueError("ragged mask") if any(p not in (0, 1) for row in mask for p in row): raise ValueError("expected binary mask") points = [(x, y) for y, row in enumerate(mask) for x, p in enumerate(row) if p == 1] if not points: return None xs, ys = zip(*points) return [min(xs), min(ys), max(xs) + 1, max(ys) + 1]
mask = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]print(mask_box(mask))print(mask_box([[0, 0], [0, 0]]))print(mask_box([[1, 0, 1]]))The outputs are [1,1,3,3], None and [0,0,3,1]. The last mask has two separated foreground pixels, yet the enclosing box includes a background pixel between them. It is not an instance detector. For counting separate objects, connected components may be an appropriate rule on a controlled mask, but touching objects and noise may require different methods.
Preprocessing, reliability and practice
Resizing can distort objects or remove small details; cropping changes coordinate origins and may exclude an object. When augmenting training images, transform their boxes and masks consistently (categorical masks typically require label-preserving nearest-neighbor interpolation). Keep related images from the same source in the same split to avoid leakage. Evaluate on lighting, scale and background conditions expected in use; a bright-pixel threshold can fail when lighting changes. Use synthetic shapes and text alternatives for every grid; no personal images or face recognition are needed. Practice: decide whether a count of separate tiles requires a label, individual boxes or an instance mask, and explain why a single enclosing box is insufficient.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Image data and vision tasks quiz
Apply image representation and overlap concepts.
How many scalar channel values are in a 4 by 4 RGB image?
Which task assigns a category to every pixel without separating instances?
Under the lesson scaling rule, what becomes of an intensity of 255?
Using zero-based [x_min,y_min,x_max,y_max] with excluded upper bounds, what box encloses the single foreground pixel (x=2,y=1)?
When cropping a training image for object detection, how should its boxes be handled?