程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Violence feature matching and FLANN feature matching in opencv (implemented in Python)

編輯:Python

List of articles

    • 1.ORB Key point detection ,SURF Key point detection ,SIFT Key point detection ,Shi-Tomasi Corner detection ,Harris Corner detection
    • 2. Feature matching method
    • 3. Violent characteristics match
    • 4. Feature matching steps
      • (1) Create matchers
      • (2) Feature matching
      • (3) Draw points for feature matching
    • 5. Code combat
    • 6.FLANN Feature matching
    • 7.FLANN step
      • (1) establish FLANN matcher FlannBasedMatches(……)
    • (2) Feature matching :flann.match/knnMatch(……)
    • (3) Draw feature matching points :cv2.drawMatches/drawMatchesKnn(……)
    • 8. Code combat

1.ORB Key point detection ,SURF Key point detection ,SIFT Key point detection ,Shi-Tomasi Corner detection ,Harris Corner detection

Harris:
https://blog.csdn.net/Keep_Trying_Go/article/details/125384144
Shi-Tomasi:
https://blog.csdn.net/Keep_Trying_Go/article/details/125384218
SIFT:
https://blog.csdn.net/Keep_Trying_Go/article/details/125384278
SURF:
https://blog.csdn.net/Keep_Trying_Go/article/details/125384513
ORB:
https://mydreamambitious.blog.csdn.net/article/details/125384635


2. Feature matching method

(1)BF(Brute-Force), Methods of violence feature matching ;
(2)FLANN The fastest neighborhood feature matching method ;


3. Violent characteristics match

Use the descriptors of each feature in the first group to match all the feature descriptors in the second group ; Calculate the gap between them , Then close to a match and return .


4. Feature matching steps

(1) Create matchers

Tf=cv2.BFMatcher(normType=None, crossCheck=None):
normType:NORM_L1(L1 Regularization ), NORM_L2(L2 Regularization —— The default value is ), NORM_HAMMING, NORM_HAMMING2.
crossCheck: Whether to cross match , The default value is false;

(2) Feature matching

Tf.match(queryDescriptors, trainDescriptors, mask=None)
Parameter is SIFT,SURE,ORB, And so on ; Calculate the descriptors of the two pictures .

(3) Draw points for feature matching

drawMatches(img1, keypoints1, img2, keypoints2, matches1to2, outImg, matchColor=None, singlePointColor=None, matchesMask=None, flags=None):

Img1: Enter the graph to search ;
Keypoints1: Enter the graph to search kp;
Img2: Enter the matching graph ;
Keypoints2: Enter the matching graph kp;
Matches1to2: Matching from the first picture to the second picture ;
outImg: Output the drawn result graph ;
matchColor: Match the color of the line keys , If -1, Represents a random color ;
singlePointColor: No matching key colors ,-1 Represents random colors ;
matchesMask: Determine which matching masks to draw , If it is empty , This draws all the matches ;
Flags: Some signs of drawing
(1)cv2.DRAW_MATCHES_FLAGS_DEFAULT: Create output image matrix , Draw matching pairs and feature points using existing output images , Draw only the middle point for each key
(2)cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG: Do not create output image matrix , Instead, draw matching pairs on the output image
(3)cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS: Draw a key point graph with size and direction for each feature point
(4)cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS: The feature points of a single point are not drawn

Function returns a matching result ;


5. Code combat

import os
import cv2
# Read and zoom pictures 
img1=cv2.imread('images/img1.jpg')
img1=cv2.resize(src=img1,dsize=(450,450))
gray1=cv2.cvtColor(src=img1,code=cv2.COLOR_BGR2GRAY)
img2=cv2.imread('images/img2.jpg')
img2=cv2.resize(src=img2,dsize=(450,450))
gray2=cv2.cvtColor(src=img2,code=cv2.COLOR_BGR2GRAY)
# establish SIFT
sift=cv2.xfeatures2d.SIFT_create()
# Calculate feature points and description points 
kp1,des1=sift.detectAndCompute(gray1,None)
kp2,des2=sift.detectAndCompute(gray2,None)
# Create matchers 
tf=cv2.BFMatcher(cv2.NORM_L1)
# Special point match 
match=tf.match(des1,des2)
# Draw matching feature points 
dest=cv2.drawMatches(img1=img1,keypoints1=kp1,img2=img2,keypoints2=kp2,matches1to2=match,outImg=None)
cv2.imshow('dest',dest)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')


6.FLANN Feature matching

advantage : Batch feature matching ,FLANN Fast ;
shortcoming : Due to the use of adjacent approximations , So the accuracy is poor ;


7.FLANN step

(1) establish FLANN matcher FlannBasedMatches(……)

Index_params Dictionaries : matching algorithm KDTREE,LSH;
Search_parames Dictionaries : Appoint KDTREE The number of times to traverse the tree in the algorithm ;
among KDTREE Set up :
Index_params=dict(
Algorithm=FLANN_INDEX_KDTREE,
Trees=5
)
Search_params=dict(checks=50)

(2) Feature matching :flann.match/knnMatch(……)

knnMatch: Parameter is SIFT,SURE,ORB, And so on ;k It means to take the front of the nearest Euclidean distance k A key point ; Return to one DMatch object .
among DMatch The content in :
Distanc: The distance between descriptors , The lower the value, the better ;
queryIdx: The index value of the descriptor of the first picture ;
TrainIdx: The descriptor index value of the second picture ;
imgIdx: Index value of the second picture ;


(3) Draw feature matching points :cv2.drawMatches/drawMatchesKnn(……)

drawMatchesKnn(img1, keypoints1, img2, keypoints2, matches1to2, outImg, matchColor=None, singlePointColor=None, matchesMask=None, flags=None):

Img1: Enter the graph to search ;
Keypoints1: Enter the graph to search kp;
Img2: Enter the matching graph ;
Keypoints2: Enter the matching graph kp;
Matches1to2: Matching from the first picture to the second picture ;
outImg: Output the drawn result graph ;
matchColor: Match the color of the line keys , If -1, Represents a random color ;
singlePointColor: No matching key colors ,-1 Represents random colors ;
matchesMask: Determine which matching masks to draw , If it is empty , This draws all the matches ;
Flags: Some signs of drawing
(1)cv2.DRAW_MATCHES_FLAGS_DEFAULT: Create output image matrix , Draw matching pairs and feature points using existing output images , Draw only the middle point for each key
(2)cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG: Do not create output image matrix , Instead, draw matching pairs on the output image
(3)cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS: Draw a key point graph with size and direction for each feature point
(4)cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS: The feature points of a single point are not drawn


8. Code combat

import os
import cv2
# Read and zoom pictures
img1=cv2.imread('images/img1.jpg')
img1=cv2.resize(src=img1,dsize=(450,450))
gray1=cv2.cvtColor(src=img1,code=cv2.COLOR_BGR2GRAY)
img2=cv2.imread('images/img2.jpg')
img2=cv2.resize(src=img2,dsize=(450,450))
gray2=cv2.cvtColor(src=img2,code=cv2.COLOR_BGR2GRAY)
# establish SIFT
sift=cv2.xfeatures2d.SIFT_create()
# Calculate feature points and description points
kp1,des1=sift.detectAndCompute(gray1,None)
kp2,des2=sift.detectAndCompute(gray2,None)
# Use KDTREE Algorithm , The tree hierarchy uses 5
index_params=dict(algorithm=1,trees=5)
search_params=dict(checks=50)
# Create matchers
flann=cv2.FlannBasedMatcher(index_params,search_params)
# Feature point matching
match=flann.knnMatch(des1,des2,k=2)
# Draw matching feature points
good=[]
for i ,(m,n) in enumerate(match):
if m.distance<0.7*n.distance:
good.append(m)
dest=cv2.drawMatchesKnn(img1=img1,keypoints1=kp1,img2=img2,keypoints2=kp2,matches1to2=[good],outImg=None,matchColor=(0,255,0))
cv2.imshow('dest',dest)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved