如何使用 Canny 在图像比较中管理 MIN_MATCH_COUNT?

How to manage MIN_MATCH_COUNT in image comparison using canny?

我正在使用 canny 进行图像比较。在使用 canny 图像边缘进行比较后,我得到了匹配和不匹配对象的正确结果。有时它没有给出正确的结果,为此我需要不断更改 MIN_MATCH_COUNT。 任何保持 MIN_MATCH_COUNT 和 canny 的解决方案都应该比较图像的每个边缘。

MIN_MATCH_COUNT = 20

img1 = canny.copy()
img2 = canny1.copy()


# Initiate SIFT detector
sift = cv.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
# store all the good matches as per Lowe's ratio test.
good = []
for m,n in matches:
    if m.distance < 0.7*n.distance:
        good.append(m)
if len(good)>MIN_MATCH_COUNT:
    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
    M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC,5.0)
    matchesMask = mask.ravel().tolist()
    h,w = img1.shape
    pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
    dst = cv.perspectiveTransform(pts,M)
    img2 = cv.polylines(img2,[np.int32(dst)],True,255,3, cv.LINE_AA)
    print ("Both the images are matching")
else:
    print( "Not enough matches are found and hence images are not same - {}/{} and hence both the images are not matching".format(len(good), MIN_MATCH_COUNT) )
    matchesMask = None
draw_params = dict(matchColor = (0,255,0), # draw matches in green color 
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)
img3 = cv.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)
plt.imshow(img3, 'gray'),plt.show()   

下面是当 MIN_MATCH_COUNT 为 20 时我得到的结果不匹配的图像,如果我将其更改为 9 那么它会说图像匹配。

类似地,在下图中,键的实际脊线也不匹配,但它仍然给出图像在不考虑匹配点的情况下匹配。

您可以使用相对标准,因此您可以使用匹配关键点占模型关键点总数的百分比,而不是为 MIN_MATCH_COUNT 使用 asbolute 值。通过这种方式,您可以根据您的特定测试设置阈值,比方说..30%(IDK,只是一个例子)。这就是我在类似问题中所做的。 类似于:

matching = len(good)/len(kp1)*100

这样0<匹配度<100表示​​相似度的百分比,所以你可以这样做:

min_threshold = 40

if matching > min_threshold:
...