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.

我缺的不是话题,而是和你聊话题的身份。有时候两个人的关系渐行渐远的原因在于我们在某些观念上彼此做了对方的主。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值