Python OpenCV3 FLANN Algorithm

Foreword

Next,i will show you how to use the FLANN Algorithm in Python.
AS usual,let’s see what FLANN algorithm is?

What’s the FLANN?

FLANN is the abbreviation of faster library approximate nearest neighbor.It’s a faster classification algorithm.FLANN is a single-layer network which introduces a non-linear expansion function. The basic idea is to extend the pattern vectors of the original input samples by using a set of linear independent (or orthogonal) functions, and to represent and distinguish the patterns in a higher dimension space. Several independent new input samples are obtained in the enhanced space, and then input them into the single-layer forward network

Library

Since some algorithms after opencv3.4 have already been patented.You’d better reduce the opencv version to before 3.4.

pip install opencv-python 3.2.0.7

pip install opencv-contrib-python 3.3.1.11

Python code

import cv2
# 按照灰度圖片讀入
img1 = cv2.imread("./8.jpg", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("./9.jpg", cv2.IMREAD_GRAYSCALE)
# 創建sift檢測器
sift = cv2.xfeatures2d.SIFT_create()
# 查找監測點和匹配符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
"""
keypoint是檢測到的特徵點的列表
descriptor是檢測到特徵的局部圖像的列表
"""
# 獲取flann匹配器
FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
searchParams = dict(checks=50)
flann = cv2.FlannBasedMatcher(indexParams, searchParams)
# 進行匹配
matches = flann.knnMatch(des1, des2, k=2)
# 準備空的掩膜 畫好的匹配項
matchesMask = [[0, 0] for i in range(len(matches))]

for i, (m, n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        matchesMask[i] = [1, 0]

drawPrams = dict(matchColor=(0, 255, 0),
                 singlePointColor=(0, 0, 255),
                 matchesMask=matchesMask,
                 flags=0)
# 匹配結果圖片
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **drawPrams)

cv2.imshow("FLANN", img3)
cv2.waitKey()
cv2.destroyAllWindows()

Display

在這裏插入圖片描述

We can see that this algorithm is better than BF.

我缺的不是話題,而是和你聊話題的身份。有時候兩個人的關係漸行漸遠的原因在於我們在某些觀念上彼此做了對方的主。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章