繁体   English   中英

Opencv Python - 来自特征匹配+同形的相似度得分

[英]Opencv Python - Similarity score from Feature matching + Homograpy

我的数据库中有几个鱼图像,我的目标是在用户输入的鱼图像和数据库中的图像之间找到相似度得分。 为此我从这个链接使用opencv功能匹配+ Homograpy。

http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html#feature-homography

我目前的代码如下。

query_image = '/home/zealous/Pictures/train_images/AbudefdufWhitleyiJER.jpg'
trained_image_folder = '/home/zealous/Pictures/train_images'

我目前的代码如下。

def feature_matcher(query_image, image_folder):

    min_match_count = 10

    img1 = cv2.imread(query_image, 0)
    surf = cv2.xfeatures2d.SURF_create(800)
    kp1, des1 = surf.detectAndCompute(img1, None)

    bf = cv2.BFMatcher(cv2.NORM_L2)

    all_files = next(os.walk(image_folder))[2]

    for file_name_temp in all_files:
        try:
            train_image = image_folder + '/' + file_name_temp
            img2 = cv2.imread(train_image, 0)
            surf = cv2.xfeatures2d.SURF_create(800)
            kp2, des2 = surf.detectAndCompute(img2, None)

            matches = bf.knnMatch(des1, des2, k=2)

            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 = cv2.findHomography(src_pts, dst_pts, cv2.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 = cv2.perspectiveTransform(pts, M)

                if not M==None:
                    print "\n"
                    print "-"*2, file_name_temp
                    print "number of good matches", len(good)
                    print "*"*10, matchesMask

我得到了相当不错的输出,我通过看到好的匹配数和匹配的变量(包含一些0和1)来假设。 如果数据库包含与输入图像相同的图像,那么将有许多良好的匹配,并且所有matchesMask元素将为1。

我的问题是如何基于此计算相似度得分? 我应该假设在matchesMask中有更多的1(Inliers),两个图像都相似,或者我应该采用1(内点)和0(异常值)之间的比例,并根据它计算相似度。

我知道这已在很多问题中讨论过,但所有的建议和答案都是用C ++语言编写的,所以我无法找到解决方案..

在相似性得分中,您不希望包含异常值 - 它们是异常值,因为它们对您的数据没有帮助。 只需将1s(内部)的数量作为相似度得分 - 你应该得到不错的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM