depth.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import struct
5 
6 import cv2
7 import numpy as np
8 from skimage.segmentation import slic
9 from skimage.feature import peak_local_max
10 from skimage.morphology import binary_closing
11 import sensor_msgs.msg
12 
13 from jsk_recognition_utils.mask import descent_closing
14 
15 
16 def split_fore_background(depth_img, footprint=None):
17  if footprint is None:
18  footprint = np.ones((3, 3))
19  segments = slic(depth_img)
20 
21  local_maxi = peak_local_max(
22  depth_img, labels=segments, footprint=footprint, indices=False)
23 
24  fg_mask = descent_closing(local_maxi, init_selem=np.ones((3, 3)), n_times=6)
25  bg_mask = ~fg_mask
26  return fg_mask, bg_mask
27 
28 
29 def depth_to_compressed_depth(depth, depth_max=None, depth_quantization=100,
30  encoding='32FC1'):
31  if depth_max is None:
32  depth_max = depth.max() + 1.0
33 
34  # compressed format is separated by ';'.
35  # https://github.com/ros-perception/image_transport_plugins/blob/f0afd122ed9a66ff3362dc7937e6d465e3c3ccf7/compressed_depth_image_transport/src/codec.cpp#L234 # NOQA
36  compressed_msg = sensor_msgs.msg.CompressedImage()
37  compressed_msg.format = '{}; compressedDepth png'.format(
38  encoding)
39 
40  if encoding == '32FC1':
41  depth_quant_a = depth_quantization * (depth_quantization + 1.0)
42  depth_quant_b = 1.0 - depth_quant_a / depth_max
43  inv_depth_img = np.zeros_like(depth, dtype=np.uint16)
44  target_pixel = np.logical_and(depth_max > depth, depth > 0)
45  inv_depth_img[target_pixel] = depth_quant_a / \
46  depth[target_pixel] + depth_quant_b
47 
48  compressed_msg.data = struct.pack(
49  'iff', 0, depth_quant_a, depth_quant_b)
50  compressed_msg.data += np.array(
51  cv2.imencode('.png', inv_depth_img)[1]).tostring()
52  else:
53  raise NotImplementedError("Not supported compressedDepth encoding {}"
54  .format(encoding))
55  return compressed_msg
static_virtual_camera.format
format
Definition: static_virtual_camera.py:44
jsk_recognition_utils.mask
Definition: mask.py:1
jsk_recognition_utils.descent_closing
descent_closing
Definition: __init__.py:72
jsk_recognition_utils.depth.split_fore_background
def split_fore_background(depth_img, footprint=None)
Definition: depth.py:16
jsk_recognition_utils.depth.depth_to_compressed_depth
def depth_to_compressed_depth(depth, depth_max=None, depth_quantization=100, encoding='32FC1')
Definition: depth.py:29


jsk_recognition_utils
Author(s):
autogenerated on Tue Jan 7 2025 04:04:52