extract_image_channel.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from __future__ import absolute_import
00004 from __future__ import division
00005 from __future__ import print_function
00006 
00007 import re
00008 import sys
00009 
00010 import numpy as np
00011 
00012 import cv_bridge
00013 import dynamic_reconfigure.server
00014 from jsk_perception.cfg import ExtractImageChannelConfig
00015 from jsk_topic_tools import ConnectionBasedTransport
00016 from jsk_topic_tools.log_utils import logwarn_throttle
00017 import rospy
00018 from sensor_msgs.msg import Image
00019 
00020 
00021 # See https://github.com/ros-perception/vision_opencv/pull/141
00022 # for PR to upstream
00023 class CvBridgeArbitraryChannels(cv_bridge.CvBridge):
00024 
00025     def encoding_to_dtype_with_channels(self, encoding):
00026         try:
00027             return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
00028         except cv_bridge.CvBridgeError as e:
00029             try:
00030                 vals = re.split('(.+)C(.+)', encoding)
00031                 dtype, n_channels = self.numpy_type_to_cvtype[vals[1]], int(vals[2])
00032                 return dtype, n_channels
00033             except Exception as e:
00034                 raise cv_bridge.CvBridgeError(e)
00035 
00036 
00037 class ExtractImageChannel(ConnectionBasedTransport):
00038 
00039     def __init__(self):
00040         super(self.__class__, self).__init__()
00041         dynamic_reconfigure.server.Server(
00042             ExtractImageChannelConfig, self._config_callback)
00043         self.pub = self.advertise('~output', Image, queue_size=1)
00044 
00045     def _config_callback(self, config, level):
00046         self.channel = config.channel
00047         return config
00048 
00049     def subscribe(self):
00050         self.sub = rospy.Subscriber('~input', Image, self._callback)
00051 
00052     def unsubscribe(self):
00053         self.sub.unregister()
00054 
00055     def _callback(self, imgmsg):
00056         if self.channel < 0:
00057             return
00058         bridge = CvBridgeArbitraryChannels()
00059         img = bridge.imgmsg_to_cv2(imgmsg)
00060         if img.ndim == 2:
00061             img = img[:, :, np.newaxis]
00062         if not (self.channel < img.shape[2]):
00063             logwarn_throttle(10,
00064                 'Invalid channel {} is specified for image with {} channels'
00065                 .format(self.channel, img.shape[2]))
00066             return
00067         out_img = np.zeros(img.shape[:2], dtype=img.dtype)
00068         out_img = img[:, :, self.channel]
00069         out_imgmsg = bridge.cv2_to_imgmsg(out_img)
00070         out_imgmsg.header = imgmsg.header
00071         self.pub.publish(out_imgmsg)
00072 
00073 
00074 if __name__ == '__main__':
00075     rospy.init_node('extract_image_channel')
00076     ExtractImageChannel()
00077     rospy.spin()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Sun Oct 8 2017 02:43:23