So Im iterating over rows of pixels that'll look like this
row = [0, 0, 0, 0, 255, 255, 0, 0, 255, 0, 0, 0, 0, 0,0]
and I'll have another array called whiteLine which looks like
whiteLine = [255, 255, 255, 255, 255, 255]
And i want to check if there are 6 white pixels in a row. Or basically check if the whiteLine array explicitly exists in the row array
Example
row = [0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 255, 0, 0, 0, 0, 0,0]
True
row = [0, 0, 0, 0, 255, 255, 0, 255, 255, 255, 0, 0, 255, 0, 0, 0, 0, 0,0]
False
CodePudding user response:
I think this can be done via looking at the convolution between row
and whiteLine
, and checking to see if one gets the value of convolvin whiteLine
with itself.
Which makes this a one-liner:
np.any(np.convolve(whiteLine, whiteLine, 'valid')[0] == np.convolve(whiteLine, row, 'valid'))
Testing with the two examples you gave produces the correct outputs, but more importantly, this is a much more general than just searching for "6 consecutive 255s"...
CodePudding user response:
You can use sliding_window_view
on row
and compare the values against whiteList
calling all at axis=1 then any sebsequently:
>>> from numpy.lib.stride_tricks import sliding_window_view
>>> (sliding_window_view(row, window_shape=len(whiteLine))==whiteLine).all(axis=1).any()
True