-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstitch_impl.py
38 lines (32 loc) · 1.3 KB
/
stitch_impl.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
# -*- coding: utf-8 -*-
from stitch import kd_tree
import numpy as np
import cv2
import os
def matchKeypoints(kpsA, kpsB, featuresA, featuresB,ratio = 0.70):
mtree = kd_tree.tree(kpsA, featuresA)
tempM0 = np.apply_along_axis(mtree.knn_search,1,featuresB)
# get this gay loop straight, dunno why but this pussy complains on vectorization
matches = np.argwhere(np.array([ tempM0[:,1][i][0]/tempM0[:,1][i][1] for i in np.arange(tempM0.shape[0])]) < ratio)
matches = np.transpose(matches)[0]
ptsA = []
ptsB = []
for i in matches:
ptsA.append(tempM0[i][0][0].data[0])
ptsB.append(kpsB[i])
return np.array(ptsA), np.array(ptsB)
if __name__ == '__main__':
# figuring out how to get to the img directory
path = os.path.realpath(__file__)
path = path[:path.rindex('/')]
#importing 2 placeholder images
imA = cv2.imread(path + '/img/tree.jpeg')
imB = cv2.imread(path + '/img/home.jpeg')
# getting the grayscale
imAG = cv2.cvtColor(imA,cv2.COLOR_BGR2GRAY)
imBG = cv2.cvtColor(imB,cv2.COLOR_BGR2GRAY)
# get the kp and descriptors
kpA, desA = cv2.SIFT().detectAndCompute(imAG,None)
kpB, desB = cv2.SIFT().detectAndCompute(imBG,None)
# match the keypoints and kaboom
mptsA, mptsB = matchKeypoints(kpA,kpB,desA,desB)