TL;DR:
Knowing the value of NUM_ROWS, rmin and rmax, how do I construct a bool array my_idx such that np.arange(NUM_ROWS)[my_idx] == np.arange(NUM_ROWS)[rmin:rmax]? Can the construction operation be broadcasted, if rmin and rmax are arrays ad I'm interested in all slices [slice(from, to) for from, to in zip(rmin, rmax)]?
Long version with details
I have an array of polygons in a 2D image and I want to find rows and columns of the image that don't contain a polygon. In order to do this fast, I'm trying to vectorize the code as much as possible.
I calculate the extreme points of each polygon on both dimension and obtain for each polygon the min_row, min_col, max_row and max_col values. Let's consider just the rows (as for the columns it 's the same algorithm) and assume that, for example, these are the values I obtain for two polygons:
NUM_ROWS = 10
# Two intervals: slice(1,5) and slice(7,8)
row_mins = np.array([1, 7], dtype=np.int32)
row_maxs = np.array([5, 8], dtype=np.int32)
I want now to merge the intervals in a way equivalent to:
row_mask = np.zeros(NUM_ROWS)
for rmin, rmax in zip(row_mins, row_maxs):
row_mask[rmin:rmax] = 1
however, it should avoid the for loop and repeated setting of values in row_mask.
I thought of doing this by turning each range into a bool array and using np.logical_or.reduce(), but I can't find a way to generate the bool array equivalent to the [rmin:rmax] index.
Is there a way to convert a slice object to a bool index?