Skip to content

Instantly share code, notes, and snippets.

@tuncatunc
Last active February 22, 2022 20:25
Show Gist options
  • Save tuncatunc/d6488e88cbd3e3fcfc55cc5c737a1383 to your computer and use it in GitHub Desktop.
Save tuncatunc/d6488e88cbd3e3fcfc55cc5c737a1383 to your computer and use it in GitHub Desktop.
import cv2
# find intersection percentage between to images
def find_intersection_percent(im1_path, im2_path):
im1 = cv2.imread(im1_path, cv2.IMREAD_UNCHANGED)
_, mask1 = cv2.threshold(im1[:, :, 3], 1, 255, cv2.THRESH_BINARY)
cv2.imwrite('im1-mask.png', mask1)
im2 = cv2.imread(im2_path, cv2.IMREAD_UNCHANGED)
_, mask2 = cv2.threshold(im2[:, :, 3], 1, 255, cv2.THRESH_BINARY)
cv2.imwrite('im2-mask.png', mask2)
intersection = cv2.bitwise_and(mask1, mask2)
intersection_area = cv2.countNonZero(intersection)
intersection_area_percent = intersection_area / (mask1.shape[0] * mask1.shape[1])
intersection_area_percent = intersection_area_percent * 100
return intersection_area_percent
result = find_intersection_percent(
'../layers-prepared/Accessories/Black glasses#1000000.png',
'../layers-prepared/Clothes/Army vest#300000.png')
# print result
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment