I'm currently trying to do an iou analysis for a 3d image in different time points. These images contain around 1500 objects (cells) with an unique id in an around 2000x2000x2000 image.
I found logical_and and logical_or functions of numpy which take one variable at a time from each image so I made a very basic double for loop, to feed every combination of every value into the iou analysis. The code looks like this:
for i in [x for x in image_1_ids if x != 0]:
for j in [y for y in image_2_ids if y != 0]:
intersection = np.logical_and(image_1 == i, image_2 == j)
union = np.logical_or(image_1 == i, image_2 == j)
iou = np.sum(intersection) / np.sum(union)
df.at[i, j] = iou
This code takes forever to run due to the many variable feed one at a time. Which makes it a combination of basically 1500x1500. Is there a more efficient way in doing this in numpy?