Skip to content

Instantly share code, notes, and snippets.

@Jojozzc
Created December 15, 2018 12:34
Show Gist options
  • Save Jojozzc/3346ef72faa6268af50f3c447053fb7c to your computer and use it in GitHub Desktop.
Save Jojozzc/3346ef72faa6268af50f3c447053fb7c to your computer and use it in GitHub Desktop.
Intersection over Union
def cal_IoU(img, lab, nb_class):
for i in range(nb_class):
mask1 = (img == (i + 1)).astype('uint8')
mask2 = (lab == (i + 1)).astype('uint8')
mask_and = cv2.bitwise_and(mask1, mask2)
mask_or = cv2.bitwise_or(mask1, mask2)
s1 = np.sum(mask_and)
s2 = np.sum(mask_or)
if np.sum(mask2) == 0:
if np.sum(mask1) == 0:
return 1.0
else:
return 0.0
else:
return s1/s2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment