I have a code that compares two figures (in my case the figures are polygons) for identity. And in most cases, this code works well. However, there are times when completely different figures turn out to be absolutely identical. Below is my code.
def compare_two_figures(template, figure_for_compare):
template = cv.imread(template, cv.IMREAD_GRAYSCALE)
figure_for_compare = cv.imread(figure_for_compare, cv.IMREAD_GRAYSCALE)
_, thresh_template = cv.threshold(template, 127, 255, 0)
_, thresh_figure_for_compare = cv.threshold(figure_for_compare, 127, 255, 0)
contours_template, _ = cv.findContours(thresh_template, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
particular_contour_template = contours_template[0]
contours_figure_for_compare, _ = cv.findContours(thresh_figure_for_compare, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
particular_contour_figure_for_compare = contours_figure_for_compare[0]
differences = cv.matchShapes(particular_contour_template, particular_contour_figure_for_compare, 1, 0.0)
print(differences)
if differences < 0.1:
return "Figures same"
else:
return "figures different"
If you need examples of figures, let me know. But for example, at the moment the current code can give a completely identical result, comparing a tetragon and a nonagon