欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv_python FLANN近邻匹配笔记

程序员文章站 2022-03-25 20:37:09
...

opencv_python FLANN近邻匹配笔记

FLANN邻近搜索

FLANN库全称是Fast Library for Approximate Nearest Neighbors,它是目前最完整的(近似)最近邻开源库。不但实现了一系列查找算法,还包含了一种自动选取最快算法的机制。
使用FLANN的搜索,整体来说分为两步,一是建立索引,二是搜索。

代码实现:

import cv2
import numpy as np
from matplotlib import pyplot as plt

def FLANN():
    queryImage = cv2.imread('img/left_01.png', 0)
    trainingImage = cv2.imread('img/right_01.png', 0)

    # 创建SIFT检测和计算
    sift = cv2.xfeatures2d.SIFT_create()
    kp1, des1 = sift.detectAndCompute(queryImage, None)
    kp2, des2 = sift.detectAndCompute(trainingImage, None)

    # FLANN匹配参数,定义FLANN匹配器,使用KNN算法实现匹配
    # 这里使用FLANN_INDEX_KDTREE,5kd-trees和50 checks迭代
    FLANN_INDEX_KDTREE = 1
    indexParams = dict(algorithm=1, trees=5)
    searchParams = dict(check=100)

    flann = cv2.FlannBasedMatcher(indexParams, searchParams)
    matches = flann.knnMatch(des1, des2, k=2)

    # 根据matches生成相同长度的matchesMask列表,列表元素为[0, 0]
    matchesMask = [[0, 0] for i in range(len(matches))]

    # 去除错误匹配
    # 比值检测认为第一个匹配和第二个匹配的比值小于一个给定的值(一般是0.5)
    for i, (m, n) in enumerate(matches):
        if m.distance < 0.5*n.distance:
            matchesMask[i] = [1, 0]
    # 将图像显示
    # mathColor是两图的匹配连接线,连接线与matchesMask相关
    # singlePointColor是勾画关键点
    drawParams = dict(matchColor=(0, 255, 0),
                      singlePointColor=(255, 0, 0),
                      matchesMask=matchesMask,
                      flags=0)
    resultImage = cv2.drawMatchesKnn(queryImage, kp1, trainingImage, kp2, matches,
                                     None, **drawParams)
    return resultImage

if __name__ == '__main__':
    resultImage = FLANN()
    plt.imshow(resultImage)
    plt.show()

opencv_python FLANN近邻匹配笔记

单应性变换(Homography)

单应性是一个条件,该条件表明当两幅图像中的一副出现投影畸变时,它们还能彼此匹配。
一个平面到另一个平面的映射关系。
两张图分别有四个相对位置相同的点,Homography就是一个变换(3*3矩阵),将一张图中的点映射到另一张图中对应的点。
opencv_python FLANN近邻匹配笔记
opencv_python FLANN近邻匹配笔记
RANSAC
随机一致性采样RANSAC是一种鲁棒的模型拟合算法,能够从有外点的数据中拟合准确的模型。

代码实现:

import cv2
import numpy as np
from matplotlib import pyplot as plt

MIN_MATCH_COUNT = 10

queryImage = cv2.imread('img/box.png', 0)
trainingImage = cv2.imread('img/box_in_scene.png', 0)

sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(queryImage, None)
kp2, des2 = sift.detectAndCompute(trainingImage, None)

FLANN_INDEX_KDTREE = 1
indexParams = dict(algorithm=1, trees=5)
searchParams = dict(check=100)

flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)

good = []
for m, n in matches:
    if m.distance < 0.5*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 = queryImage.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)

    trainingImage = cv2.polylines(trainingImage, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)

else:
    print("Not enough matches are found %d/%d" % (len(good), MIN_MATCH_COUNT))
    matchesMask = None

draw_params = dict(matchColor=(0, 255, 0), singlePointColor=None, matchesMask=matchesMask, flags=2)

img3 = cv2.drawMatches(queryImage, kp1, trainingImage, kp2, good, None, **draw_params)

plt.imshow(img3, 'gray'), plt.show()

opencv_python FLANN近邻匹配笔记

参考文章:

https://www.jianshu.com/p/57fc7068cfee