I have an array containing some pixel indices, and a binary image. I would like to filter the first array based on the value of the image at the corresponding image point. Currently I am doing following:
mask = np.random.rand(512, 512)
mask[mask < 0.5] = 0
mask[mask >= 0.5] = 1
x = np.random.randint(0, mask.shape[0], 10)
y = np.random.randint(0, mask.shape[1], 10)
img_pts = np.vstack([x, y]).T
occ = np.zeros(len(img_pts))
for i, img_pt in enumerate(img_pts):
if mask[img_pt[1], img_pt[0]]:
occ[i] = 1
Definitely not the best way to do it. Is there a faster way?