# ---------------------------------------------------------------------------------------------------------------------- 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)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter