SIFT替代品OpenCV

时间:2019-04-22 08:05:39

标签: python opencv sift

我使用旧的SIFT中的OpenCV编写了一种算法,它可以很好地满足我的需求。但是,我意识到SIFT的新版本不再支持OpenCV。关于可以替代它的任何建议,以便我仍然可以保留现有代码?我尝试过ORB,但它似乎工作方式不同,似乎无法以相同的方式工作。

SIFT /法兰绒材料

def siftMatcher(image, logoToSearch):
    MIN_MATCH_COUNT = 45
    img1 = logoToSearch
    img2 = image

    # Initiate SIFT detector
    sift = cv2.xfeatures2d.SIFT_create()
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)
    # FLANN parameters
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=30)
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)
    # store all the good matches as per Lowe's ratio test.

    mCords = []
    nCords = []
    hasSeen = []

    for m, n in matches:
        if m.distance < 0.65 * n.distance:
            mC = kp2[m.trainIdx].pt
            nC = kp2[n.trainIdx].pt
            if ([mC,nC] in hasSeen or [nC,mC] in hasSeen):
                continue
            mCords.append(mC)
            nCords.append(nC)
            hasSeen.append([mC,nC])
            hasSeen.append([nC,mC])
    symMatches = numberOfSymetricMatches(mCords,nCords)
    return symMatches

创建分数

degreesTolerance = 2

def numberOfSymetricMatches(mCords,nCords):

    allMatchesCounts = []
    counter1 = 0

    for mCord1 in mCords:
        nCord1 = nCords[counter1]
        counter2 = 0
        tempMatchesCount = 0

        for mCord2 in mCords:
            if (counter1 == counter2):
                counter2 += 1
                continue
            nCord2 = nCords[counter2]

            xDist1 = mCord1[0] - mCord2[0]
            yDist1 = mCord1[1] - mCord2[1]
            angle1 = 0
            if (yDist1 != 0):
                angle1 = math.degrees(math.atan(xDist1/yDist1))

            xDist2 = nCord1[0] - nCord2[0]
            yDist2 = nCord1[1] - nCord2[1]
            angle2 = 0
            if (yDist2 != 0):
                angle2 = math.degrees(math.atan(xDist2/yDist2))

            if (angle1 - degreesTolerance <= angle2 and angle1 + degreesTolerance >= angle2):
                tempMatchesCount += 1
            counter2 += 1

        allMatchesCounts.append(tempMatchesCount)
        counter1 += 1

    meanMatch = 0
    maxMatch = 0

    if (len(allMatchesCounts) != 0):
        meanMatch = mean(allMatchesCounts)
        maxMatch = max(allMatchesCounts)

    numMatches = len(mCords)

    return meanMatch * maxMatch * numMatches

编辑1

Explanation on why I can't use older version of OpenCV

编辑2:ORB实施效果不佳

# Initiate ORB detector
orb = cv2.ORB_create(10000, 1.2, nlevels=8, edgeThreshold = 5)
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
# FLANN parameters
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 30)
flann = cv2.FlannBasedMatcher(index_params, search_params)
des1 = np.float32(des1)
des2 = np.float32(des2)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

0 个答案:

没有答案