我有三张图像:A、B 和 C。我想比较(A,B)和(C,B)之间的面积差异。有没有更有效、更准确的方法使用Python来做到这一点?
这是我目前正在做的事情,我认为这是相当不准确的:
from PIL import Image
from pycocotools import mask as coco_mask
import pandas as pd, json, cv2, numpy as np, matplotlib.pyplot as plt, os
A = r"/path/to/A"
B = r"/path/to/B"
C = r"/path/to/C"
for a_path in os.listdir(A):
for b_path in os.listdir(B):
if b_path in a_path:
a_image = Image.open(A + os.sep + a_path).convert('L')
b_image = Image.open(B + os.sep + b_path).convert('L')
a_array = np.array(a_image)
b_array = np.array(b_image)
overlap_array = np.bitwise_and(a_array, b_array)
overlap_pixel_count = np.sum(overlap_array > 0)
n_pixel_count = np.sum(b_array > 0)
overlap_percentage = (overlap_pixel_count / n_pixel_count) * 100
print(overlap_percentage)
else:
pass
for c_path in os.listdir(C):
for b_path in os.listdir(B):
if b_path in c_path:
c_image = Image.open(bounding_boxes_path + os.sep + c_path).convert('L') # Convert to grayscale
b_image = Image.open(segment_masks_path + os.sep + b_path).convert('L') # Convert to grayscale
c_array = np.array(c_image)
b_array = np.array(b_image)
overlap_array = np.bitwise_and(c_array, b_array)
overlap_pixel_count = np.sum(overlap_array > 0)
n_pixel_count = np.sum(b_array > 0)
overlap_percentage = (overlap_pixel_count / n_pixel_count) * 100
print(overlap_percentage)
else:
pass