跳转到主要内容

raygeo.image.scan

ScanLine

A single scan line with its pixel coverage and mm-space endpoints.

Produced by generate_scan_lines. Each line has a unique index, start/end positions in mm, and the set of pixels it intersects in the image.

end_mm

end_mm: tuple[float, float]

index

index: int

line_interval_mm

line_interval_mm: float

pixels

pixels: list[tuple[int, int]]

start_mm

start_mm: tuple[float, float]

direction()

direction() -> tuple[float, float]
ParameterTypeDescription
Returnstuple[float, float]

length_mm()

length_mm() -> float
ParameterTypeDescription
Returnsfloat

pixel_to_mm()

pixel_to_mm(
px: int,
py: int,
pixels_per_mm: tuple[float, float],
) -> tuple[float, float]
ParameterTypeDescription
pxint
pyint
pixels_per_mmtuple[float, float]
Returnstuple[float, float]

ScanMode

Scan mode for raster operations.

SEGMENTED skips zero-power gaps within a scan line. FULL_SWEEP emits the full line with power values (zeros included).

Functions

downsample_power_values()

downsample_power_values(
power_values: numpy.ndarray,
start_mm: tuple[float, float],
end_mm: tuple[float, float],
sample_interval_mm: float,
) -> tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Downsample power values along a scan segment.

If the sample interval is larger than the native pixel spacing, the power values are resampled by nearest-neighbour at the target spacing. Otherwise the original values are returned with their corresponding positions.

ParameterTypeDescription
power_valuesnumpy.ndarray1-D array of byte power values.
start_mmtuple[float, float](x, y) start position of the segment in mm.
end_mmtuple[float, float](x, y) end position of the segment in mm.
sample_interval_mmfloatDesired sample spacing in mm.
Returnstuple[numpy.ndarray, numpy.ndarray, numpy.ndarray](power, x_mm, y_mm) of downsampled values.
ComplexityO(n) where n = number of power values

extract_zero_power_segments()

extract_zero_power_segments(
start: tuple[float, float, float],
end: tuple[float, float, float],
power_values: bytes,
) -> list[float]

Extract zero-power segment endpoints from scanline power data.

Finds contiguous runs of zero values in power_values and computes their 3D start/end points via linear interpolation along the scanline segment from start to end.

ParameterTypeDescription
starttuple[float, float, float](x, y, z) start position of the scanline in mm.
endtuple[float, float, float](x, y, z) end position of the scanline in mm.
power_valuesbytesPer-step power bytes.
Returnslist[float]Flat list of [sx, sy, sz, ex, ey, ez, ...] segments.
ComplexityO(n) where n = number of steps

find_mask_bounding_box()

find_mask_bounding_box(mask: numpy.ndarray) -> tuple[int, int, int, int] | None

Find the bounding box of non-zero pixels in a binary mask.

Scans the mask and returns the (y_min, y_max, x_min, x_max) of the smallest axis-aligned rectangle covering all non-zero pixels.

ParameterTypeDescription
masknumpy.ndarray2-D binary mask array.
Returnstuple[int, int, int, int] | None(y_min, y_max, x_min, x_max) pixel coordinates, or None if the mask is entirely zero.
ComplexityO(h*w)

find_segments()

find_segments(values: numpy.ndarray) -> list[tuple[int, int]]

Find contiguous non-zero segments in a 1-D array.

Returns a list of (start, end) index pairs covering every run of consecutive non-zero values.

ParameterTypeDescription
valuesnumpy.ndarray1-D array of byte values.
Returnslist[tuple[int, int]]List of (start, end) index pairs.
ComplexityO(n)

generate_horizontal_scan_positions()

generate_horizontal_scan_positions(
y_min_px: int,
y_max_px: int,
height_px: int,
pixels_per_mm: tuple[float, float],
line_interval_mm: float,
offset_y_mm: float,
) -> tuple[list[float], list[float]]

Compute Y positions for horizontal scan lines.

Given a vertical pixel range, computes the mm and pixel Y coordinates of evenly-spaced scan lines (aligned to a global grid defined by line_interval_mm and offset_y_mm).

ParameterTypeDescription
y_min_pxintMinimum Y pixel coordinate.
y_max_pxintMaximum Y pixel coordinate.
height_pxintImage height in pixels.
pixels_per_mmtuple[float, float](x, y) pixel density in px/mm.
line_interval_mmfloatSpacing between scan lines in mm.
offset_y_mmfloatGlobal Y offset in mm.
Returnstuple[list[float], list[float]](y_coords_mm, y_coords_px) tuple of Y positions.
ComplexityO(n) where n = number of scan lines

generate_scan_lines()

generate_scan_lines(
bbox: tuple[int, int, int, int],
image_size: tuple[int, int],
pixels_per_mm: tuple[float, float],
line_interval_mm: float,
direction_degrees: float = 0,
offset_x_mm: float = 0,
offset_y_mm: float = 0,
global_center_mm: tuple[float, float] | None = None,
) -> list[ScanLine]

Generate scan lines covering a bounding box.

Creates a set of parallel scan lines at a given angle and spacing that cover the bounding box region. Each line is rasterised to pixels and stored as a ScanLine.

ParameterTypeDescription
bboxtuple[int, int, int, int](y_min, y_max, x_min, x_max) of the region.
image_sizetuple[int, int](width, height) of the image in pixels.
pixels_per_mmtuple[float, float](x, y) pixel density in px/mm.
line_interval_mmfloatSpacing between scan lines in mm.
direction_degreesfloat = 0Scan direction angle in degrees.
offset_x_mmfloat = 0Global X offset in mm.
offset_y_mmfloat = 0Global Y offset in mm.
global_center_mmtuple[float, float] | None = NoneOptional rotation centre in mm; defaults to the bbox centre + offset.
Returnslist[ScanLine]List of ScanLine objects.
ComplexityO(n * p) where n = number of lines, p = pixels per line

line_pixels()

line_pixels(
start: tuple[float, float],
end: tuple[float, float],
width: int,
height: int,
) -> list[tuple[int, int]]

Rasterise a line segment into pixel coordinates.

Uses Bresenham's line algorithm to enumerate all integer pixel positions intersecting the line from start to end, clipped to the image dimensions (width, height).

ParameterTypeDescription
starttuple[float, float](x, y) start position in pixel coordinates.
endtuple[float, float](x, y) end position in pixel coordinates.
widthintImage width in pixels.
heightintImage height in pixels.
Returnslist[tuple[int, int]]List of (x, y) pixel coordinates on the line.
ComplexityO(n) where n = number of pixels on the line

resample_rows()

resample_rows(
image: numpy.NDArray[numpy.uint8],
y_coords_px: numpy.ndarray,
) -> numpy.NDArray[numpy.uint8]

Resample image rows at arbitrary Y coordinates.

Performs linear interpolation between adjacent rows to sample the image at the given (potentially fractional) Y positions.

ParameterTypeDescription
imagenumpy.NDArray[numpy.uint8]2-D input image array.
y_coords_pxnumpy.ndarray1-D array of Y pixel coordinates.
Returnsnumpy.NDArray[numpy.uint8]2-D array with shape (len(y_coords_px), width).
ComplexityO(m * w) where m = output rows, w = image width