73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
"""
|
|
Find all CMA logo matches in YDQ23_001838.pdf
|
|
"""
|
|
import cv2
|
|
import numpy as np
|
|
from pathlib import Path
|
|
|
|
pdf_name = "YDQ23_001838.pdf"
|
|
page_img_path = Path(f"test_reports_full/{pdf_name}/doc_page.png")
|
|
template_path = Path("template/CMA_Logo.png")
|
|
|
|
# Load images
|
|
page_img = cv2.imread(str(page_img_path))
|
|
page_gray = cv2.cvtColor(page_img, cv2.COLOR_BGR2GRAY)
|
|
|
|
template = cv2.imread(str(template_path), cv2.IMREAD_GRAYSCALE)
|
|
h, w = page_img.shape[:2]
|
|
template_h, template_w = template.shape
|
|
|
|
print(f"Page size: {w}x{h}")
|
|
print(f"Template size: {template_w}x{template_h}")
|
|
print()
|
|
|
|
# Template matching with TM_CCORR_NORMED
|
|
result = cv2.matchTemplate(page_gray, template, cv2.TM_CCORR_NORMED)
|
|
|
|
# Find all matches above threshold
|
|
threshold = 0.5
|
|
loc = np.where(result >= threshold)
|
|
|
|
matches = []
|
|
for pt in zip(*loc[::-1]):
|
|
confidence = result[pt[1], pt[0]]
|
|
matches.append({
|
|
'position': pt,
|
|
'confidence': float(confidence)
|
|
})
|
|
|
|
# Sort by confidence
|
|
matches.sort(key=lambda x: x['confidence'], reverse=True)
|
|
|
|
print(f"Found {len(matches)} matches above threshold {threshold}")
|
|
print()
|
|
|
|
for i, match in enumerate(matches[:10]):
|
|
x, y = match['position']
|
|
conf = match['confidence']
|
|
center_x = x + template_w // 2
|
|
center_y = y + template_h // 2
|
|
|
|
# Calculate relative position
|
|
rel_x = center_x / w * 100
|
|
rel_y = center_y / h * 100
|
|
|
|
print(f"Match #{i+1}:")
|
|
print(f" Position: ({x}, {y})")
|
|
print(f" Center: ({center_x}, {center_y})")
|
|
print(f" Relative: ({rel_x:.1f}%, {rel_y:.1f}%)")
|
|
print(f" Confidence: {conf:.3f}")
|
|
print()
|
|
|
|
# Visualize all matches
|
|
viz = page_img.copy()
|
|
for match in matches[:5]:
|
|
x, y = match['position']
|
|
cv2.rectangle(viz, (x, y), (x + template_w, y + template_h), (0, 255, 0), 2)
|
|
cv2.putText(viz, f"{match['confidence']:.2f}", (x, y - 10),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
|
|
|
output_path = Path("test_reports_full") / pdf_name / "all_matches.png"
|
|
cv2.imwrite(str(output_path), viz)
|
|
print(f"Visualization saved to: {output_path}")
|