Binary image 2026-11-05
A binary image assigns one of two values to every pixel, commonly and or false and true. It can represent a selected region, such as marks separated from a page background, without retaining all the original brightness values. The two values express a chosen classification of pixels, not necessarily two physical colors in the original scene.
For example, define image thresholding by selecting intensities at least :The output preserves which side of the threshold each value occupied but loses the distinction between and . Changing the threshold changes the selected region, so interpreting the result requires the selection rule as well as the image.
A binary image is useful as input to connected-component labeling. There, pixel connectivity determines whether diagonally touching selected pixels belong to the same region. SciPy's labeling operation accepts zero as background and all nonzero values as foreground; an input with several nonzero intensities is consequently treated as a foreground mask by that particular API.[ref]
label in the SciPy documentation. Parameters, input: zero background and nonzero foreground
Two possibilities require one bit of information per pixel in a direct packed representation. An actual array or image file can use more storage, or compression can exploit repeated patterns. Also distinguish a binary image from a label image containing several region identifiers: a label value of three need not mean brighter foreground than a label value of one.
Connected-component labeling 2026-11-05
Connected-component labeling assigns an identifying number to each separate connected region of an image after choosing which pixels belong to the foreground and which neighboring pixels count as connected. In a binary image, interpret each foreground pixel as a vertex of a graph, with an edge for each allowed neighboring pair. Each component receives one label; background pixels receive a separate background value. The choice of pixel connectivity is part of the problem definition, not an incidental display setting.
For example, consider the binary imageIf a foreground pixel connects only to foreground pixels immediately above, below, left or right, the two ones form two regions. One valid label image is . If diagonal neighbors also connect, the same input has one region, with label image . SciPy's
ndimage.label treats zero as background and nonzero entries as foreground; its default two-dimensional neighborhood uses the four axial neighbors. Its neighborhood mask must be symmetric about its centre so that a connection works in both directions.[ref]label in the SciPy documentation. Parameters, input and structure; Notes, requirement for two-way connections
One direct algorithm scans for an unlabelled foreground pixel, starts a flood fill there and assigns a fresh label to every reachable foreground pixel. It then resumes scanning for another unlabelled region. For a fixed-size neighborhood, every pixel and a bounded number of neighbors need be inspected, giving time complexity for image pixels, with storage for labels and the pending search. Another approach records provisional labels and merges equivalent ones using a disjoint-set data structure.
The numerical labels are identifiers: swapping labels and does not change the segmentation. A single printed character can have several disconnected regions, while touching characters can share one region. Connected-component labeling therefore supplies geometric grouping for image segmentation and optical character recognition; it does not itself determine what an object means.
Flood fill 2026-11-05
Flood fill finds the region reachable from a seed pixel through allowed neighboring pixels that satisfy a membership rule, often replacing the values in that region. A paint program's fill operation is a familiar application. The seed, membership rule and pixel connectivity together specify what will be filled.
Consider the imageStart at the upper-left pixel, select values equal to one, and allow only horizontal or vertical steps. Exactly three pixels are reachable, giving replacement by nine asWith diagonal steps also allowed, all six ones are reachable and replaced. The fill is a connectivity computation, not a rule to replace every matching value everywhere.
An iterative implementation keeps pending positions in a stack, takes one position and adds eligible unvisited neighbors. Marking a position when it enters the pending collection prevents repeated insertion. This is a depth-first search of the implicit graph. For a fixed neighborhood, visiting a region of pixels takes time complexity, excluding initialization or copying of a whole image. Explicit pending and visited storage avoids relying on a deeply nested recursive call for a large region.
Membership need not require exact equality. The scikit-image
flood_fill API can accept values within a specified tolerance of the original seed value, with connectivity or a supplied neighborhood footprint controlling adjacency. Comparing each candidate to the seed is different from allowing a chain of small local changes to drift arbitrarily far from the seed value.[ref]flood_fill in the scikit-image documentation. flood_fill, seed_point, tolerance, footprint and connectivity parameters
Record visited positions independently when necessary: replacing a value by itself must not make the algorithm revisit it forever. Repeated flood fills with fresh region labels can implement connected-component labeling.
Image thresholding 2026-11-05
Image thresholding classifies pixels by comparing their intensities with a threshold, often producing a binary image. A global threshold uses the same value throughout the image; adaptive thresholding permits the threshold to depend on position and nearby intensities.[ref]
Image Thresholding in the OpenCV documentation. Simple Thresholding; Adaptive Thresholding
For example, defineIntensities then become . The comparison at equality matters: a rule using would instead give . OpenCV's
THRESH_BINARY uses the strict greater-than rule, assigning its chosen maximum output value only above the threshold.[ref]Image Thresholding in the OpenCV documentation. Simple Thresholding, threshold equality and maxval
Threshold choice affects which distinctions survive. Two different bright intensities can become the same foreground value, while a small change near the boundary can change a pixel's classification. The resulting mask can then feed connected-component labeling, where pixel connectivity separately decides which selected pixels form a region.
A useful motivating experiment keeps one image of dark marks fixed while adding or recording a gradual illumination variation across the background. Compare a global threshold with a declared local-neighborhood rule, inspecting lost strokes and falsely selected background. OpenCV documents local mean and Gaussian-weighted rules with an offset; this addresses varying illumination rather than proving that every resulting region is a meaningful object.[ref]
Image Thresholding in the OpenCV documentation. Adaptive Thresholding, neighborhood-based thresholds and varying illumination
A histogram can guide global selection, but intensity statistics alone do not determine semantic identity. Noise and overlapping foreground/background intensities can make every threshold imperfect. Report the input representation, comparison convention and any filtering or local-window settings so that a thresholded result can be reproduced.
Pixel connectivity 2026-11-05
Pixel connectivity specifies which neighboring pixels may be joined directly when forming paths through an image. A region is connected when its pixels can be reached through such allowed steps while remaining in the region. This makes the region a component of an implicitly defined graph.
For a two-dimensional square grid and pixel position , four-neighbor connectivity usesrestricted to positions inside the image. Eight-neighbor connectivity adds the four diagonals:Thus four-neighbor steps cross an edge between pixel cells, while eight-neighbor steps can also pass through a corner.
The diagonal binary imagehas three foreground regions under four-neighbor connectivity and one under eight-neighbor connectivity. SciPy's default two-dimensional connected-component labeling uses the four axial neighbors; a full three-by-three neighborhood also connects diagonals. Its neighborhood must encode connections in both directions.[ref]
label in the SciPy documentation. Parameters, structure; Notes, centrosymmetry; Examples, diagonal connectivity
Connectivity also affects how a boundary separates foreground from background. In a two-by-two checkerboard, allowing diagonal links for both values makes each value connected across its own diagonal. A convention that treats foreground and background differently can avoid interpreting both diagonal paths as uninterrupted planar crossings. The chosen convention should therefore accompany any claim about separate objects or enclosed regions.
A flood fill and connected-component labeling can give different answers on the same stored array solely because their pixel connectivity differs. Check the neighborhood before interpreting that disagreement as a bug or a physical change in the imaged object.