-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathorb.py
More file actions
48 lines (39 loc) · 1.45 KB
/
orb.py
File metadata and controls
48 lines (39 loc) · 1.45 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
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
def orb_features(n):
"""
ORB特征提取
"""
print 'reading images ...'
descriptors = []
orb = cv2.ORB_create(10000) #表示提取一幅图像的n个特征, 如需生成字典文件,改为10000
for i in range(n):
img = cv2.imread('images_0/{0}.png'.format(i), 0)
keypoint, descriptor = orb.detectAndCompute(img, None)
#特征转int型
# print descriptor
descriptor = descriptor.astype(int)
#特征转列表
descriptor = descriptor.tolist()
descriptors.append(descriptor)
return descriptors
def update_image(n):
orb = cv2.ORB_create(500)
img = cv2.imread('images_0/{0}.png'.format(n), 0)
keypoint, descriptor = orb.detectAndCompute(img, None)
descriptor = descriptor.astype(int)
descriptor = descriptor.tolist()
return descriptor
if __name__ == '__main__':
img1=cv2.imread('images_0/12.png', cv2.IMREAD_GRAYSCALE)
img2=cv2.imread('images_0/13.png', cv2.IMREAD_GRAYSCALE)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key=lambda x:x.distance)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:40], img2,flags=2)
plt.imshow(img3), plt.show()