3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
13 import dynamic_reconfigure.server
14 from jsk_perception.cfg
import ExtractImageChannelConfig
15 from jsk_topic_tools
import ConnectionBasedTransport
18 from sensor_msgs.msg
import Image
27 return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
28 except cv_bridge.CvBridgeError
as e:
30 vals = re.split(
'(.+)C(.+)', encoding)
31 dtype, n_channels = self.numpy_type_to_cvtype[vals[1]],
int(vals[2])
32 return dtype, n_channels
33 except Exception
as e:
34 raise cv_bridge.CvBridgeError(e)
40 super(self.__class__, self).
__init__()
41 dynamic_reconfigure.server.Server(
43 self.
pub = self.advertise(
'~output', Image, queue_size=1)
59 img = bridge.imgmsg_to_cv2(imgmsg)
61 img = img[:, :, np.newaxis]
62 if not (self.
channel < img.shape[2]):
64 'Invalid channel {} is specified for image with {} channels' 65 .format(self.
channel, img.shape[2]))
67 out_img = np.zeros(img.shape[:2], dtype=img.dtype)
68 out_img = img[:, :, self.
channel]
69 out_imgmsg = bridge.cv2_to_imgmsg(out_img)
70 out_imgmsg.header = imgmsg.header
71 self.pub.publish(out_imgmsg)
74 if __name__ ==
'__main__':
75 rospy.init_node(
'extract_image_channel')