ChainCodeData#

class ktch.io.ChainCodeData(specimen_name: str, x: float, y: float, area_per_pixel: float, chain_code: ndarray, area_pixels: int | None = None)[source]#

Chain code data class.

Chain codes represent 2D contours using directional codes from 0 to 7:

3 2 1
4 * 0
5 6 7
Parameters:
specimen_namestr

Specimen name.

xfloat

X coordinate.

yfloat

Y coordinate.

area_per_pixelfloat

Area (mm2) per pixel.

chain_codenp.ndarray

Chain code sequence with values from 0 to 7 representing directions.

area_pixelsint or None, optional

Area in pixels. If None, computed from the chain code using the Shoelace formula. If provided and differs from the computed value, a warning is issued and the computed value is used.

get_chain_code()[source]#

Get the raw chain code as a numpy array.

Returns:
chain_codenp.ndarray

Raw chain code values (0-7) representing directions.

property sample_name: str#

Deprecated. Use specimen_name instead.

to_dataframe()[source]#

Convert chain code to 2D coordinates as a pandas DataFrame.

The chain code is converted to a sequence of 2D coordinates, starting from (0, 0) and applying the directional changes based on the chain code values. The coordinates are scaled using the area_per_pixel value.

Chain codes represent 2D contours using directional codes from 0 to 7:

3 2 1
4 * 0
5 6 7
Returns:
dfpd.DataFrame

DataFrame with x and y columns for the coordinates and chain_code column for the direction codes. The first point has chain_code=-1 since it has no direction.

to_numpy()[source]#

Convert chain code to 2D coordinates as a numpy array.

The chain code is converted to a sequence of 2D coordinates, starting from (x, y) and applying the directional changes based on the chain code values. Displacements are scaled by sqrt(area_per_pixel); when area_per_pixel <= 0 (e.g. no scale marker was set in SHAPE), pixel units are used (scale factor = 1).

The returned coordinates are in image coordinates where X increases rightward and Y increases downward. The starting point (x, y) and displacements share the same coordinate system (pixels when area_per_pixel <= 0, physical units otherwise).

Chain codes represent 2D contours using directional codes from 0 to 7:

3 2 1
4 * 0
5 6 7
Returns:
coordsnp.ndarray

2D coordinates with shape (n, 2) where n is the number of points. The first column is the x-coordinate and the second column is the y-coordinate.