两张图像进行比较,查看轮廓形状相似度
import cv2
import numpy as np
def cv_show(neme, img):
# cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img1 = cv2.imread("ce.png", 0)
img2 = cv2.imread("ce.png", 0)
ret, thresh = cv2.threshold(img1, 127, 255, 0)
ret, thresh2 = cv2.threshold(img2, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 2, 1)
cnt1 = contours[0]
sd = cv2.drawContours(img1, contours, -1, (0, 0, 255), 3)
cv_show('sd', sd)
contours, hierarchy = cv2.findContours(thresh2, 2, 1)
cnt2 = contours[0]
sd = cv2.drawContours(img2, contours, -1, (0, 0, 255), 3)
cv_show('sd', sd)
# 形状匹配 参数(轮廓1、轮廓2、比较的方法、方法相应的参数)
# 接近0代表好
ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0)
print(ret)