-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage detection
More file actions
84 lines (65 loc) · 2.44 KB
/
image detection
File metadata and controls
84 lines (65 loc) · 2.44 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
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
#!/usr/bin/env python
import rospy, cv2, cv_bridge
import numpy as np
from sensor_msgs.msg import Image
from geometry_msgs.msg import Twist
from geometry_msgs.msg import PoseArray
from std_msgs.msg import Int32
class WayPoint:
def __init__(self):
rospy.init_node('ros_bridge')
# Create a ROS Bridge
self.ros_bridge = cv_bridge.CvBridge()
# Subscribe to whycon image_out
self.image_sub = rospy.Subscriber('whycon/image_out', Image, self.image_callback)
def image_callback(self,msg):
# 'image' is now an opencv frame
# You can run opencv operations on 'image'
###DETECTING COLOUR###
image = self.ros_bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
blur3 = cv2.medianBlur(image,5)
hsv = cv2.cvtColor(blur3,cv2.COLOR_BGR2HSV)
image = self.ros_bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
blur3 = image
hsv = cv2.cvtColor(blur3,cv2.COLOR_BGR2HSV)
###DETECTING BLUE COLOUR##
lower1 = np.array([110, 50, 50])
upper1 = np.array([130, 255, 255])
mask1 = cv2.inRange(hsv,lower1,upper1)
res1 = cv2.bitwise_and(image,image,mask=mask1)
blur1 = cv2.medianBlur(mask1,5)
image1=blur3
gray1 = cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY)
ret1,thresh1 = cv2.threshold(gray1,2,255,0)
abc1, contours1, hierarchy1 = cv2.findContours(mask1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(res1,contours1,-1,(0,255,0),3) ##neglect 0
print len(contours1)
self.red=contours1
##Detecting red colour##
lower2 = np.array([0, 100, 100])
upper2 = np.array([20, 255, 255])
mask2 = cv2.inRange(hsv,lower2,upper2)
res2 = cv2.bitwise_and(image,image,mask=mask2)
blur2 = cv2.medianBlur(mask2,5)
image2=blur2
gray2 = cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)
ret2,thresh2 = cv2.threshold(gray2,2,255,0)
abc2, contours2, hierarchy2 = cv2.findContours(mask2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(res2,contours2,-1,(0,255,0),3) ##neglect 0
print len(contours2)
self.blue2=contours2
##Detectingt the green colour
lower = np.array([120, 100, 100])
upper = np.array([145, 255, 255])
mask = cv2.inRange(hsv,lower,upper)
res = cv2.bitwise_and(image,image,mask=mask)
blur3 = cv2.medianBlur(mask,5)
image=blur3
gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,2,255,0)
abc, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print len(contours)
self.green=contours
if __name__ == '__main__':
test = WayPoint()
rospy.spin()