-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIFT_Matching.py
More file actions
66 lines (43 loc) · 2.08 KB
/
SIFT_Matching.py
File metadata and controls
66 lines (43 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 10 11:46:39 2022
@author: aniltanaktan
"""
import cv2
import matplotlib.pyplot as plt
def SIFT(imageL,imageR):
# Convert images into gray scale
grayL = cv2.cvtColor(imageL, cv2.COLOR_BGR2GRAY)
grayR = cv2.cvtColor(imageR, cv2.COLOR_BGR2GRAY)
# Convert original images into RGB for display
imageL = cv2.cvtColor(imageL, cv2.COLOR_BGR2RGB)
imageR = cv2.cvtColor(imageR, cv2.COLOR_BGR2RGB)
# SIFT Keypoint Detection
sift = cv2.xfeatures2d.SIFT_create()
left_keypoints, left_descriptor = sift.detectAndCompute(grayL, None)
right_keypoints, right_descriptor = sift.detectAndCompute(grayR, None)
print("Number of Keypoints Detected In The Left Image: ", len(left_keypoints))
print("Number of Keypoints Detected In The Right Image: ", len(right_keypoints))
# Brute Force Matching
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck = True) #crossCheck is enabled to get better matching points
#crossCheck checks both matching points' distances
matches = bf.match(left_descriptor, right_descriptor)
# Get only matches with only a short distance (eliminate false matches)
matches = sorted(matches, key = lambda x : x.distance)
# We will only display first 100 matches for simplicity
result = cv2.drawMatches(imageL, left_keypoints, imageR, right_keypoints, matches[:100], grayR, flags = 2)
# Display the matches
plt.rcParams['figure.figsize'] = [14.0, 7.0]
plt.title('Matches')
plt.imshow(result)
plt.show()
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
cv2.imshow('Matches', result)
# Print total number of matching points between the training and query images
print("\nImage is ready. \nNumber of Matching Keypoints: ", len(matches))
cv2.waitKey(0)
# Load images
image1 = cv2.imread('Images/imageLeft.jpg')
image2 = cv2.imread('Images/imageRight.jpg')
SIFT(image1,image2)