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