-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobdetect
76 lines (59 loc) · 1.93 KB
/
obdetect
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
#!/usr/bin/env python
from __future__ import print_function
import roslib
import sys
import rospy
import cv2
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class image_converter:
def __init__(self):
self.image_pub = rospy.Publisher("/myresult", Image, queue_size=2)
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber("/cf1/camera/image_raw", Image, self.callback)
def callback(self,data):
# Convert the image from OpenCV to ROS format
try:
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print(e)
# Convert BGR to HSV
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
#convert rgb to hsv for red
H = 100
S = 355
V = 100
# define range of the color we look for in the HSV space
lower = np.array([0,0,250])
upper = np.array([255,5,255])
rlower = np.array([H-5,S-8,V-5])
rupper = np.array([H+5,S+8,V+5])
# Threshold the HSV image to get only the pixels in ranage
maskw = cv2.inRange(hsv, lower, upper)
maskr = cv2.inRange(hsv, rlower, rupper)
# Bitwise-AND mask and original image
mask1 = cv2.bitwise_or(maskw, maskr) #why or is used
res = cv2.bitwise_and(cv_image, cv_image, mask= mask1)
# count nonzero values in the image such that it segments the sign
countwhite = np.count_nonzero(maskw)
countred = np.count_nonzero(maskr)
if countwhite > 90 and countred > 550:
rospy.loginfo("Obdetected")
# Publish the image
try:
self.image_pub.publish(self.bridge.cv2_to_imgmsg(res, "bgr8"))
except CvBridgeError as e:
print(e)
def main(args):
rospy.init_node('colorseg', anonymous=True)
ic = image_converter()
print("running...")
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == '__main__':
main(sys.argv)