image_cluster_indices_decomposer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import cv_bridge
5 from jsk_recognition_msgs.msg import ClusterPointIndices
6 from jsk_topic_tools import ConnectionBasedTransport
7 import message_filters
8 import rospy
9 from sensor_msgs.msg import Image
10 
11 
12 class ImageClusterIndicesDecomposer(ConnectionBasedTransport):
13  def __init__(self):
14  super(ImageClusterIndicesDecomposer, self).__init__()
15  self._pub = self.advertise('~output', Image, queue_size=1)
16 
17  def subscribe(self):
18  self._sub = message_filters.Subscriber('~input', Image)
19  self._sub_cpi = message_filters.Subscriber('~input/cluster_indices',
20  ClusterPointIndices)
21  use_async = rospy.get_param('~approximate_sync', False)
22  queue_size = rospy.get_param('~queue_size', 100)
23  subs = [self._sub, self._sub_cpi]
24  if use_async:
25  slop = rospy.get_param('~slop', 0.1)
26  sync = message_filters.ApproximateTimeSynchronizer(
27  subs, queue_size, slop)
28  else:
29  sync = message_filters.TimeSynchronizer(subs, queue_size)
30  sync.registerCallback(self._decompose)
31 
32  def unsubscribe(self):
33  self._sub.unregister()
34  self._sub_cpi.unregister()
35 
36  def _decompose(self, imgmsg, cpi_msg):
37  bridge = cv_bridge.CvBridge()
38  img = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='rgb8')
39 
40  from jsk_recognition_utils import colorize_cluster_indices
41  cluster_indices = [list(i_msg.indices)
42  for i_msg in cpi_msg.cluster_indices]
43  out = colorize_cluster_indices(img, cluster_indices)
44 
45  out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8')
46  out_msg.header = imgmsg.header
47  self._pub.publish(out_msg)
48 
49 
50 if __name__ == '__main__':
51  rospy.init_node('image_cluster_indices_decomposer')
53  rospy.spin()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27