contours¶
Utilities for detecting, filtering, and analyzing contours within images.
This module provides functions to:
- Find external contours in a binary mask,
- Calculate centroid and orientation for each contour (via SVD on central moments),
- Filter contours by size and shape,
- Construct minimal masks,
- And visualize the resulting contours for debugging.
ContourInfo ¶
ContourInfo(contour: ndarray, moments: tuple[ndarray, ndarray], rect: tuple[int, int, int, int], mask: ndarray)
Holds geometric and orientation data about a single contour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contour
|
|
The raw points making up the contour (numpy array). |
required |
moments
|
|
A tuple |
required |
rect
|
|
A bounding rectangle |
required |
mask
|
|
A binary mask of just this contour, cropped to |
required |
Source code in src/page_dewarp/contours.py
proj_x ¶
Compute the scalar projection of a point onto this contour's tangent axis.
The tangent axis is defined by self.center and self.tangent.
Source code in src/page_dewarp/contours.py
local_overlap ¶
Compute the overlap of this contour's local axis range with another contour's.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
|
Another ContourInfo instance. |
required |
Returns:
| Type | Description |
|---|---|
|
The 1D overlap along the tangent axis, as computed by |
Source code in src/page_dewarp/contours.py
blob_mean_and_tangent ¶
Compute the centroid and principal orientation of a contour.
Constructs the blob image's covariance matrix from second-order central moments
by dividing them by the 0th-order 'area moment' to make them translation-invariant.
The eigenvectors of the covariance matrix provide the blob's principal axes,
from which we extract a 2D orientation vector (tangent) and a centroid (center).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contour
|
|
A single contour (numpy array) representing the boundary of a shape. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple
|
|
Returns |
Source code in src/page_dewarp/contours.py
interval_measure_overlap ¶
Return the overlap length of two 1D intervals.
Each interval is given as (start, end). The overlap is computed as: min(a_end, b_end) - max(a_start, b_start).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
int_a
|
|
The first interval, e.g. (a_start, a_end). |
required |
int_b
|
|
The second interval, e.g. (b_start, b_end). |
required |
Returns:
| Type | Description |
|---|---|
|
The overlap length (which may be negative if no overlap exists). |
Source code in src/page_dewarp/contours.py
make_tight_mask ¶
Create a minimal binary mask for a contour.
The mask is cropped to the bounding rectangle (xmin, ymin, width, height).
The contour is shifted so it fits in the top-left corner of the mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contour
|
|
The raw contour points (numpy array). |
required |
xmin
|
|
X coordinate of the bounding rect's top-left corner. |
required |
ymin
|
|
Y coordinate of the bounding rect's top-left corner. |
required |
width
|
|
Width of the bounding rectangle. |
required |
height
|
|
Height of the bounding rectangle. |
required |
Returns:
| Type | Description |
|---|---|
|
A 2D uint8 mask with the same shape as the bounding rectangle, |
|
filled in for the region covered by |
Source code in src/page_dewarp/contours.py
get_contours ¶
Detect and filter contours in a binary mask, returning their ContourInfo objects.
This function finds external contours, filters them by size/aspect,
computes the centroid/orientation, and wraps everything in ContourInfo.
If DEBUG_LEVEL >= 2, it visualizes the resulting contours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
|
A string identifier for debugging/logging. |
required |
small
|
|
A downsampled image (for visualization). |
required |
mask
|
|
A 2D binary mask in which to find contours. |
required |
Returns:
| Type | Description |
|---|---|
|
A list of |
Source code in src/page_dewarp/contours.py
visualize_contours ¶
Overlay colored contours on a copy of the image for debugging or inspection.
Each contour is filled with a unique color. The center and principal axis are drawn in white lines. A half-and-half blend of the filled contour and the original image is used for better visibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
|
A string identifier for debugging/logging. |
required |
small
|
|
The downsampled image in which to draw. |
required |
cinfo_list
|
|
A list of |
required |