-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrgb2gray.py
59 lines (46 loc) · 1.67 KB
/
rgb2gray.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import rospy
import cv2
import sys
import signal
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from sensor_msgs.msg import Imu
from cv_bridge import CvBridge, CvBridgeError
def signal_handler(signal, frame): # ctrl + c -> exit program
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class robot():
def __init__(self):
rospy.init_node('robot_controller', anonymous=True)
#self.img_subscriber = rospy.Subscriber('/raspicam_node/image/compressed',CompressedImage,self.callback_compressed_img)
self.img_publisher = rospy.Publisher('/image_mono',Image,queue_size=1)
self.img_subscriber = rospy.Subscriber('/uav/camera/left/image_rect_color',Image,self.callback)
self.imu_subscriber = rospy.Subscriber('/uav/sensors/imu', Imu, self.imu_callback)
self.imu_publisher = rospy.Publisher('/imu_data', Imu, queue_size=1)
self.bridge = CvBridge()
# def callback_compressed_img(self,data):
# np_arr = np.fromstring(data.data, np.uint8)
# self.image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # OpenCV >= 3.0:
def callback(self,data):
try :
# self.time = time.time()
cvimage=self.bridge.imgmsg_to_cv2(data,"bgr8")
cv_image=cv2.cvtColor(cvimage,cv2.COLOR_BGR2GRAY)
img=self.bridge.cv2_to_imgmsg(cv_image, "mono8")
img.header.stamp = rospy.Time.now()
self.img_publisher.publish(img)
# print(time.time()-self.time)
except CvBridgeError as e:
pass
def imu_callback(self,data):
data.header.stamp = rospy.Time.now()
self.imu_publisher.publish(data)
if __name__=='__main__':
gray=robot()
time.sleep(1)
while 1:
pass