test_point_indices_to_mask_image.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import unittest
00004 
00005 import numpy as np
00006 
00007 import cv_bridge
00008 from pcl_msgs.msg import PointIndices
00009 import rospy
00010 import rostest
00011 from sensor_msgs.msg import Image
00012 
00013 
00014 class TestPointIndicesToMaskImage(unittest.TestCase):
00015 
00016     def setUp(self):
00017         self.msg = {}
00018         self.pub_indices = rospy.Publisher(
00019             '~output/indices', PointIndices, queue_size=1)
00020         self.pub_img = rospy.Publisher('~output/image', Image, queue_size=1)
00021         self.sub_mask1 = rospy.Subscriber(
00022             '~input/mask1', Image, self.mask_cb, callback_args='mask1')
00023         self.sub_mask2 = rospy.Subscriber(
00024             '~input/mask2', Image, self.mask_cb, callback_args='mask2')
00025 
00026     def mask_cb(self, msg, name):
00027         self.msg[name] = msg
00028 
00029     def test_conversion(self):
00030         for name in ['mask1', 'mask2']:
00031             self._test_each_conversion(name)
00032 
00033     def _test_each_conversion(self, name):
00034         bridge = cv_bridge.CvBridge()
00035 
00036         imgmsg = bridge.cv2_to_imgmsg(np.zeros((30, 30), dtype=np.uint8),
00037                                       encoding='mono8')
00038         indices_msg = PointIndices()
00039         indices_msg.indices = np.arange(10, 30)
00040         while self.msg.get(name) is None:
00041             imgmsg.header.stamp = indices_msg.header.stamp = rospy.Time.now()
00042             self.pub_indices.publish(indices_msg)
00043             self.pub_img.publish(imgmsg)
00044             rospy.sleep(0.1)
00045 
00046         mask_in = bridge.imgmsg_to_cv2(
00047             self.msg[name], desired_encoding='mono8')
00048         mask_in = np.squeeze(mask_in)
00049         mask_expected = np.zeros((imgmsg.height, imgmsg.width), dtype=np.uint8)
00050         mask_expected.ravel()[np.arange(10, 30)] = 255
00051         np.testing.assert_array_equal(mask_in, mask_expected)
00052 
00053 
00054 if __name__ == "__main__":
00055     PKG = 'jsk_pcl_ros_utils'
00056     ID = 'test_point_indices_to_mask_image'
00057     rospy.init_node(ID)
00058     rostest.rosrun(PKG, ID, TestPointIndicesToMaskImage)


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Tue Jul 2 2019 19:40:52