test_point_indices_to_mask_image.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import unittest
4 
5 import numpy as np
6 
7 import cv_bridge
8 from pcl_msgs.msg import PointIndices
9 import rospy
10 import rostest
11 from sensor_msgs.msg import Image
12 
13 
14 class TestPointIndicesToMaskImage(unittest.TestCase):
15 
16  def setUp(self):
17  self.msg = {}
18  self.pub_indices = rospy.Publisher(
19  '~output/indices', PointIndices, queue_size=1)
20  self.pub_img = rospy.Publisher('~output/image', Image, queue_size=1)
21  self.sub_mask1 = rospy.Subscriber(
22  '~input/mask1', Image, self.mask_cb, callback_args='mask1')
23  self.sub_mask2 = rospy.Subscriber(
24  '~input/mask2', Image, self.mask_cb, callback_args='mask2')
25 
26  def mask_cb(self, msg, name):
27  self.msg[name] = msg
28 
29  def test_conversion(self):
30  for name in ['mask1', 'mask2']:
31  self._test_each_conversion(name)
32 
33  def _test_each_conversion(self, name):
34  bridge = cv_bridge.CvBridge()
35 
36  imgmsg = bridge.cv2_to_imgmsg(np.zeros((30, 30), dtype=np.uint8),
37  encoding='mono8')
38  indices_msg = PointIndices()
39  indices_msg.indices = np.arange(10, 30)
40  while self.msg.get(name) is None:
41  imgmsg.header.stamp = indices_msg.header.stamp = rospy.Time.now()
42  self.pub_indices.publish(indices_msg)
43  self.pub_img.publish(imgmsg)
44  rospy.sleep(0.1)
45 
46  mask_in = bridge.imgmsg_to_cv2(
47  self.msg[name], desired_encoding='mono8')
48  mask_in = np.squeeze(mask_in)
49  mask_expected = np.zeros((imgmsg.height, imgmsg.width), dtype=np.uint8)
50  mask_expected.ravel()[np.arange(10, 30)] = 255
51  np.testing.assert_array_equal(mask_in, mask_expected)
52 
53 
54 if __name__ == "__main__":
55  PKG = 'jsk_pcl_ros_utils'
56  ID = 'test_point_indices_to_mask_image'
57  rospy.init_node(ID)
58  rostest.rosrun(PKG, ID, TestPointIndicesToMaskImage)


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:15