import cv2
import numpy as np
import os
def calculate_black_pixels_in_sectors(image,sector):
_, image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)
height, width = image.shape
center_x, center_y = width // 2, height // 2
angle_step = 360 / sector
black_pixel_counts = np.zeros(sector)
for i in range(sector):
start_angle = np.deg2rad(i * angle_step)
end_angle = np.deg2rad((i + 1) * angle_step)
y, x = np.ogrid[:height, :width]
mask = ((x - center_x) * np.cos(start_angle) + (y - center_y) * np.sin(start_angle) >= 0) & \
((x - center_x) * np.cos(end_angle) + (y - center_y) * np.sin(end_angle) <= 0)
black_pixel_counts[i] = np.sum(image[mask] == 0)
return black_pixel_counts
def compare_images(image_path1, image_path2, pixel_threshold, sector_threshold,sector):
image1 = cv2.imread(image_path1, cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread(image_path2, cv2.IMREAD_GRAYSCALE)
black_pixels1 = calculate_black_pixels_in_sectors(image1,sector)
black_pixels2 = calculate_black_pixels_in_sectors(image2,sector)
diff = np.abs(black_pixels1 - black_pixels2)
num_similar_sectors = np.sum(diff <= pixel_threshold)
similarity_percentage = (num_similar_sectors / len(black_pixels1)) * 100
num_exceeding_sectors = np.sum(diff > pixel_threshold)
is_within_sector_threshold = num_exceeding_sectors <= sector_threshold
return similarity_percentage, is_within_sector_threshold
def compare_directories(dirA, dirB, pixel_threshold, sector_threshold,similarity_threshold):
if not os.path.exists(dirA) or not os.path.exists(dirB):
raise ValueError("One or both directories do not exist.")
dirsA = os.listdir(dirA)
dirsB = os.listdir(dirB)
common_dirs = list(set(dirsA).intersection(set(dirsB)))
results = {}
unmatched_files_A = {}
unmatched_files_B = {}
high_similarity_pairs = []
for common_dir in common_dirs:
pathA = os.path.join(dirA, common_dir)
pathB = os.path.join(dirB, common_dir)
filesA = os.listdir(pathA)
filesB = os.listdir(pathB)
common_files = list(set(filesA).intersection(set(filesB)))
for file_name in common_files:
img_path_A = os.path.join(pathA, file_name)
img_path_B = os.path.join(pathB, file_name)
similarity, within_threshold = compare_images(img_path_A, img_path_B, pixel_threshold, sector_threshold,sector)
if common_dir not in results:
results[common_dir] = {}
results[common_dir][file_name] = {'similarity': similarity, 'within_threshold': within_threshold}
if similarity >= similarity_threshold:
high_similarity_pairs.append((img_path_A, img_path_B, similarity))
unmatched_files_A[common_dir] = list(set(filesA) - set(common_files))
unmatched_files_B[common_dir] = list(set(filesB) - set(common_files))
results['unmatched_files_A'] = unmatched_files_A
results['unmatched_files_B'] = unmatched_files_B
results['high_similarity_pairs'] = high_similarity_pairs
return results
dirA = 'imgA'
dirB = 'imgB'
pixel_threshold = 50
sector_threshold = 5
sector=72
similarity_threshold = 70
results = compare_directories(dirA, dirB, pixel_threshold, sector_threshold,similarity_threshold)
for dir_name, files in results.items():
if isinstance(files, dict) and dir_name not in ('unmatched_files_A', 'unmatched_files_B'):
print(f"Directory: {dir_name}")
for file_name, info in files.items():
print(f" File: {file_name}, Similarity: {info['similarity']:.2f}%, Within Threshold: {info['within_threshold']}")
if 'unmatched_files_A' in results:
print("\nUnmatched files in imgA:")
for dir_name, files in results['unmatched_files_A'].items():
print(f" Directory: {dir_name}")
for file_name in files:
print(f" File: {file_name}")
if 'unmatched_files_B' in results:
print("\nUnmatched files in imgB:")
for dir_name, files in results['unmatched_files_B'].items():
print(f" Directory: {dir_name}")
for file_name in files:
print(f" File: {file_name}")
if 'high_similarity_pairs' in results:
print("\nHigh Similarity Pairs:")
for imgA, imgB, sim in results['high_similarity_pairs']:
print(f" Pair: {os.path.basename(imgA)} and {os.path.basename(imgB)}, Similarity: {sim}%")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix