-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsift_extractor.py
87 lines (60 loc) · 2.09 KB
/
sift_extractor.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cv2
import numpy as np
###
# Class to extract image features using SIFT.
###
class SIFT_Extractor:
def __init__(self, maxNumberOfKeyPoints, scale = False, scaleFactor = 200):
self.SIFT = cv2.xfeatures2d.SIFT_create(maxNumberOfKeyPoints)
self.scale = scale
self.scaleFactor = scaleFactor
'''
Return the features from a grayScaleImage.
'''
def extractFeaturesFromImage(self, grayImage, includeKeyPoints = False):
keyPoints, descriptors = self.SIFT.detectAndCompute(grayImage, None)
return (keyPoints, descriptors) if includeKeyPoints else descriptors
'''
Return the features from the image located at imagePath
'''
def extractFeatures(self, imagePath, includeKeyPoints = False):
grayScaleImage = self.getGrayscaleImage(imagePath)
return self.extractFeaturesFromImage(grayScaleImage, includeKeyPoints)
'''
Return a map of file:descriptors for all the images in the list.
'''
def getFileToDescriptorsMap(self, listOfImagePaths):
fileToDescriptorsMap = {}
for imagePath in listOfImagePaths:
fileToDescriptorsMap[imagePath] = self.extractFeatures(imagePath)
return fileToDescriptorsMap
'''
Return a list of all descriptors in the file:descriptors map.
'''
def extractAllDescriptors(self, fileToDescriptorsMap):
allDescriptors = []
for imagePath in fileToDescriptorsMap:
allDescriptors.extend( fileToDescriptorsMap[imagePath] )
return allDescriptors
'''
Visualize the keypoints for an image.
'''
def visualizeKeyPoints(self, imagePath):
grayScaleImage = self.getGrayscaleImage(imagePath)
keyPoints, descriptors = \
self.extractFeaturesFromImage(grayScaleImage, True)
result = cv2.drawKeypoints(grayScaleImage,keyPoints, None, None, \
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('dst_rt', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
'''
Get a grayscale version of the image.
'''
def getGrayscaleImage(self, imagePath):
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if self.scale:
image = cv2.resize(image, (self.scaleFactor, self.scaleFactor))
return image