Cython Generate Row Boundaries

PHOTO EMBED

Mon Feb 13 2023 22:41:48 GMT+0000 (Coordinated Universal Time)

Saved by @MaVCArt #python #cython

# ----------------------------------------------------------------------------------------------------------------------
cpdef object get_row_boundaries(object row):
    """
    For a given row of pixels, this method returns a white pixel wherever a transition of values happens. The output is
    pure black and white, and expects an input of pure black and white.

    The way this is achieved, is by shifting the pixels in the row first left, then right, by one pixel. The shifted
    row (missing pixels filled with a value of 0) is then subtracted from the original, resulting in, for the right-
    shifted version, a white pixel where black turned into white, and for the left-shifted version, a white pixel
    where white turned into black.

    Taking the max() value of the combination of both of these gives us our boundary pixels for this row.
    """
    # -- this will give us a white pixel where black turned into white
    rising_diff = row - shift(row, 1, fill_value=0)

    # -- this will give us a white pixel where white turned into black
    decreasing_diff = row - shift(row, -1, fill_value=0)

    # -- this will combine both masks and give us the max() of both combined.
    return np.maximum(rising_diff, decreasing_diff)
content_copyCOPY