-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_SIFT_ .py
89 lines (70 loc) · 1.86 KB
/
_SIFT_ .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
88
89
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 23 17:16:03 2018
@author: Abhishek
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
from Gradient_image import gradient
from sklearn.decomposition import PCA
import itertools
from centreOfMass import com
def sift(image):
#image=cv2.imread('dvmm3.tif')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
sift= cv2.xfeatures2d.SIFT_create()
#compute SIFT descriptors
kps, desc = sift.detectAndCompute(gray, None)
if len(kps)==0:
return np.zeros(1200)
#Applying PCA
pca = PCA(n_components = 8)
desc = pca.fit_transform(desc)
# desc=desc.astype("float64")
new_desc=[]
for de in desc:
x=[]
for val in de:
x.append(val)
l=len(x)
while l<8:
x.append(0.0)
l+=1
new_desc.append(x)
(center,_)=com(image)
def distance(x,y):
return np.sqrt(np.sum((x-y)**2))
#w,h= gray.shape[:2]
d=[]
#check=np.zeros(w*h)
index=0
for p in kps:
p=p.pt
dist = distance(np.array(center),np.array(p))
# num=int(dist)
# if check[num]==0:
# check[num]=1
d.append((dist,index))
index=index+1
d.sort()
#final_kps=[]
features = []
ctr=0
#Creating Final feature descriptor
#To choose How much Keypoints you want
while ctr<150:
for x in d:
if ctr<150:
#final_kps.append(kps[x[1]])
features.append(new_desc[x[1]])
ctr=ctr+1
else:
break
#Visualizing keypoints on image
#plt.imshow(cv2.drawKeypoints(image, final_kps, image))
#plt.scatter([center[0]],[center[1]],c='r')
#plt.show()
flattenFeatures= list(itertools.chain(*features))
return flattenFeatures
#return desc