微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 canny 在图像比较中管理 MIN_MATCH_COUNT?

我正在使用 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,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,0] ]).reshape(-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,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,那么它会说图像匹配。

enter image description here

同样在下图中,键的实际脊也不匹配,但它仍然给出图像匹配而不考虑匹配点。

enter image description here

解决方法

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

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

以这种方式 0

min_threshold = 40

if matching > min_threshold:
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。