-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.py
99 lines (68 loc) · 2.24 KB
/
vision.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
90
91
92
93
94
95
96
97
98
99
import random
import cv2
import numpy as np
import rospy
import message_filters
import time
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import vgg
image = None
def start_camera_node():
print("Camera Started")
subscriber = message_filters.Subscriber("/camera/image_raw", Image)
syncro = message_filters.TimeSynchronizer([subscriber], 1)
syncro.registerCallback(save_image)
#image = rospy.wait_for_message("/camera/image_raw", Image)
rospy.spin()
def save_image(camera_image):
global image
cv2_image = CvBridge().imgmsg_to_cv2(camera_image, "bgr8")
image = cv2_image
def sift_descriptor(img):
sift_des = cv2.convertScaleAbs(img, 0.5, 2.5)
sift_des = cv2.rotate(sift_des, cv2.ROTATE_90_CLOCKWISE)
sift_des = cv2.cvtColor(sift_des, cv2.COLOR_BGR2GRAY)
sift_des = sift_des[105:230, 65:190]
sift = cv2.xfeatures2d.SIFT_create()
sift_key, sift_des = sift.detectAndCompute(sift_des, None)
return sift_des
def sift_compare(des1, des2):
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
threshold = 0.75
good_matches = []
for m, n in matches:
if m.distance < threshold * n.distance:
good_matches.append(m)
return len(good_matches) > 25
def tm_descriptor(img):
# The descriptor is the entire image
tm_des = cv2.convertScaleAbs(img, 0.5, 2.5)
tm_des = cv2.rotate(tm_des, cv2.ROTATE_90_CLOCKWISE)
tm_des = cv2.cvtColor(tm_des, cv2.COLOR_BGR2GRAY)
return tm_des
def tm_compare(des1, des2):
template = des2[105:230, 65:190]
result = cv2.matchTemplate(des1, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where(result >= threshold)
return len(loc[0]) > 0
def get_descriptor(method):
global image
time.sleep(0.5)
if method == "SIFT":
desc = sift_descriptor(image)
elif method == "TM":
desc = tm_descriptor(image)
elif method == "NN":
desc = vgg.vgg_descriptor(image)
cv2.imwrite("imgs/img"+str(random.randint(0,100000))+".jpg", image)
return desc
def get_compare_func(method):
if method == "SIFT":
return sift_compare
elif method == "TM":
return tm_compare
elif method == "NN":
return vgg.vgg_compare