Zum Hauptinhalt springen

raygeo.image.preprocess

Functions

compute_adaptive_threshold()

compute_adaptive_threshold(areas: list[int]) -> int

Compute an adaptive area threshold to separate noise from content.

Analyses the distribution of connected component areas and finds the largest gap to determine a threshold that separates noise (small components) from meaningful content.

ParameterTypeDescription
areaslist[int]Sorted list of component pixel areas.
ReturnsintAdaptive threshold value (minimum area to keep).
ComplexityO(n) where n = number of unique area values

Adaptive threshold from component area distribution

Adaptive threshold from component area distribution

denoise_binary()

denoise_binary(
binary: numpy.NDArray[numpy.uint8],
) -> numpy.NDArray[numpy.uint8]

Remove small noise components from a binary image using adaptive thresholding.

Computes connected components, finds the largest gap in component area distribution to separate noise from content, and removes small components.

ParameterTypeDescription
binarynumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1).
Returnsnumpy.NDArray[numpy.uint8]2D binary uint8 array with noise removed.
ComplexityO(w*h)

Binary image denoised via adaptive thresholding

Binary image denoised via adaptive thresholding

filter_components()

filter_components(
binary: numpy.NDArray[numpy.uint8],
min_area: int,
) -> numpy.NDArray[numpy.uint8]

Remove connected components smaller than min_area.

Uses 8-connectivity for component detection.

ParameterTypeDescription
binarynumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1).
min_areaintMinimum pixel count to keep a component.
Returnsnumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1).
ComplexityO(w*h)

Component filtering by minimum area

Component filtering by minimum area

get_component_areas()

get_component_areas(binary: numpy.NDArray[numpy.uint8]) -> list[int]

Compute the pixel area of each connected component.

Uses 8-connectivity. Areas are returned sorted ascending. Background (0-valued pixels) is excluded.

ParameterTypeDescription
binarynumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1).
Returnslist[int]Sorted list of component pixel areas.
ComplexityO(w*h)

Connected component areas sorted ascending

Connected component areas sorted ascending

grayscale_to_binary()

grayscale_to_binary(
gray: numpy.NDArray[numpy.uint8],
threshold: float = 0.5,
invert: bool = False,
auto_threshold: bool = True,
) -> numpy.NDArray[numpy.uint8]

Convert grayscale image to binary using Otsu or fixed threshold.

Pixels at or below the threshold become foreground (1). Uses Otsu's method when auto_threshold is True.

ParameterTypeDescription
graynumpy.NDArray[numpy.uint8]2D grayscale uint8 image.
thresholdfloat = 0.5Fixed threshold (0.0-1.0), used only if auto_threshold is False.
invertbool = FalseIf True, pixels above threshold become foreground.
auto_thresholdbool = TrueIf True, compute threshold via Otsu's method.
Returnsnumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1).
ComplexityO(w*h)

Grayscale to binary via Otsu and fixed threshold

Grayscale to binary via Otsu and fixed threshold