depth_image_filter.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import cv_bridge
4 from dynamic_reconfigure.server import Server
5 from jsk_perception.cfg import DepthImageFilterConfig as Config
6 from jsk_topic_tools import ConnectionBasedTransport
7 import numpy as np
8 import rospy
9 from sensor_msgs.msg import Image
10 
11 
12 class DepthImageFilter(ConnectionBasedTransport):
13 
14  def __init__(self):
15  super(DepthImageFilter, self).__init__()
16 
17  self.bridge = cv_bridge.CvBridge()
18  self.srv = Server(Config, self.config_callback)
19  self.pub_mask = self.advertise(
20  '~output/mask', Image, queue_size=1)
21 
22  def subscribe(self):
23  self.sub = rospy.Subscriber('~input', Image, self.callback,
24  queue_size=1, buff_size=2**24)
25 
26  def unsubscribe(self):
27  self.sub.unregister()
28 
29  def config_callback(self, config, level):
30  self.depth_threshold = config.threshold
31  self.negative = config.negative
32  return config
33 
34  def callback(self, depth_img_msg):
35  bridge = self.bridge
36 
37  supported_encodings = {'16UC1', '32FC1'}
38  if depth_img_msg.encoding not in supported_encodings:
39  rospy.logwarn('Unsupported depth image encoding: {0}'
40  .format(depth_img_msg.encoding))
41 
42  depth = bridge.imgmsg_to_cv2(depth_img_msg)
43  if depth_img_msg.encoding == '16UC1':
44  depth = depth / 1000.0 # convert metric: mm -> m
45 
46  mask = np.zeros(depth.shape, dtype=np.uint8)
47  mask_idx = np.logical_and(
48  0 < depth,
49  depth < self.depth_threshold)
50  if self.negative is True:
51  mask_idx = np.logical_not(mask_idx)
52  mask[mask_idx] = 255
53 
54  mask = (mask).astype(np.uint8)
55  mask_msg = bridge.cv2_to_imgmsg(mask, encoding='mono8')
56  mask_msg.header = depth_img_msg.header
57  self.pub_mask.publish(mask_msg)
58 
59 
60 if __name__ == '__main__':
61  rospy.init_node('depth_image_filter')
63  rospy.spin()
node_scripts.depth_image_filter.DepthImageFilter.callback
def callback(self, depth_img_msg)
Definition: depth_image_filter.py:34
node_scripts.depth_image_filter.DepthImageFilter.subscribe
def subscribe(self)
Definition: depth_image_filter.py:22
node_scripts.depth_image_filter.DepthImageFilter.pub_mask
pub_mask
Definition: depth_image_filter.py:19
node_scripts.depth_image_filter.DepthImageFilter
Definition: depth_image_filter.py:12
node_scripts.depth_image_filter.DepthImageFilter.depth_threshold
depth_threshold
Definition: depth_image_filter.py:30
node_scripts.depth_image_filter.DepthImageFilter.srv
srv
Definition: depth_image_filter.py:18
node_scripts.depth_image_filter.DepthImageFilter.config_callback
def config_callback(self, config, level)
Definition: depth_image_filter.py:29
node_scripts.depth_image_filter.DepthImageFilter.bridge
bridge
Definition: depth_image_filter.py:17
node_scripts.depth_image_filter.DepthImageFilter.negative
negative
Definition: depth_image_filter.py:31
node_scripts.depth_image_filter.DepthImageFilter.sub
sub
Definition: depth_image_filter.py:23
node_scripts.depth_image_filter.DepthImageFilter.__init__
def __init__(self)
Definition: depth_image_filter.py:14
node_scripts.depth_image_filter.DepthImageFilter.unsubscribe
def unsubscribe(self)
Definition: depth_image_filter.py:26


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Fri May 16 2025 03:11:16