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
(1)BF(Brute-Force), Methods of violence feature matching ;
(2)FLANN The fastest neighborhood feature matching method ;
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 .
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;
Tf.match(queryDescriptors, trainDescriptors, mask=None)
Parameter is SIFT,SURE,ORB, And so on ; Calculate the descriptors of the two pictures .
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 ;
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')
advantage : Batch feature matching ,FLANN Fast ;
shortcoming : Due to the use of adjacent approximations , So the accuracy is poor ;
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)
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 ;
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
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')