У меня есть шаблонное изображение рисунка.
У меня также есть несколько изображений, которые могут содержать попытки воспроизвести его с вариациями (размер, положение, поворот).
Я хочу дать оценку точности каждой попытки по сравнению с шаблоном.
Я пробовал некоторые методы opencv, такие как моменты Ху, но не получил хороших результатов.
Какой более эффективный подход или алгоритм для достижения этой цели?
Я новичок в обработке изображений, поэтому объясните простыми словами.
Сейчас я работаю с openCV в Python3, но решение должно работать и в Java.
У меня есть шаблонное изображение рисунка. У меня также есть несколько изображений, которые могут содержать попытки воспроизвести его с вариациями (размер, положение, поворот). Я хочу дать оценку точности каждой попытки по сравнению с шаблоном. Я пробовал некоторые методы opencv, такие как моменты Ху, но не получил хороших результатов. Какой более эффективный подход или алгоритм для достижения этой цели? Я новичок в обработке изображений, поэтому объясните простыми словами. Сейчас я работаю с openCV в Python3, но решение должно работать и в Java. [code]import cv2 as cv import numpy as np from scipy.optimize import linear_sum_assignment
shapes = [] for i in range(1, num_labels): # Ignore the background (label 0) # Create a mask for this component mask = (labels == i).astype(np.uint8) * 255
# Find the contour contours, _ = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
if contours and len(contours[0]) >= 5: # At least 5 points to be a valid shape shapes.append( { "contour": contours[0], "area": stats[i, cv.CC_STAT_AREA], "centroid": centroids[i], } )
return shapes
def compare_two_shapes(contour1, contour2): # matchShapes returns a distance (0 = identical, larger means more different) distance = cv.matchShapes(contour1, contour2, cv.CONTOURS_MATCH_I2, 0)
# Convert to similarity score 0-100 # Typical distance: 0-0.5 for similar shapes, >1 for very different similarity = max(0, 100 - (distance * 100))
return similarity
def match_shapes_between_images(shapes1, shapes2): if not shapes1 or not shapes2: return [], 0
n1, n2 = len(shapes1), len(shapes2)
# Cost matrix (distance between each pair of shapes) cost_matrix = np.zeros((n1, n2))
for i in range(n1): for j in range(n2): similarity = compare_two_shapes( shapes1[i]["contour"], shapes2[j]["contour"] ) cost_matrix[i, j] = 100 - similarity # Cost = inverse of similarity
# Hungarian algorithm to find the optimal assignment row_ind, col_ind = linear_sum_assignment(cost_matrix)
matches = [] for i, j in zip(row_ind, col_ind): similarity = 100 - cost_matrix[i, j] matches.append({"model_idx": i, "drawing_idx": j, "similarity": similarity})
return matches, len(shapes1)
def evaluate_drawing(template: str, image: str, min_similarity=60): # Extract shapes from both images model_shapes = extract_shapes(template) drawing_shapes = extract_shapes(image)
# Check that shapes were found if not model_shapes: return { "score": 0, "quality": "Error", "details": "No shapes detected in the template", }
if not drawing_shapes: return { "score": 0, "quality": "Very Bad", "details": "No shapes detected in the drawing", }