test_sparse_image.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os.path as p
4 import cv2
5 from cv_bridge import CvBridge, CvBridgeError
6 
7 import unittest
8 
9 from sensor_msgs.msg import Image
10 
11 import rospy
12 
13 
15  def __init__(self, imgpath):
16  self.pub = rospy.Publisher("/test_edge_image", Image)
17  self.sub_topic_name = "/sparse/image_decoded"
18  self.sub = rospy.Subscriber(self.sub_topic_name, Image, self._callback)
19  self.bridge = CvBridge()
20  self.edgeimg = cv2.imread(imgpath)
21  self.edgemsg = self.bridge.cv2_to_imgmsg(self.edgeimg, encoding="8UC3")
22  self.response = False
23 
24  def _publish(self):
25  try:
26  self.edgemsg.header.stamp = rospy.Time.now()
27  self.pub.publish(self.edgemsg)
28  except CvBridgeError as e:
29  print(e)
30 
31  def _check_img_msg_diff(self, msg1, msg2):
32  cnt = 0
33  if msg1.encoding == "8UC3":
34  channel1 = 3
35  else:
36  channel1 = 1
37  channel2 = 1
38  if msg1.width != msg2.width:
39  return -1
40  if msg1.height != msg2.height:
41  return -1
42  if msg1.header.stamp != msg2.header.stamp:
43  return -1
44  receivedimg = self.bridge.imgmsg_to_cv2(msg2)
45  for x in range(self.edgeimg.shape[1]):
46  for y in range(self.edgeimg.shape[0]):
47  if not (self.edgeimg[y,x] == receivedimg[y,x]).any():
48  rospy.loginfo(self.edgeimg[y,x])
49  rospy.loginfo(receivedimg[y,x])
50  rospy.loginfo("")
51  cnt += 1
52  rospy.logdebug(cnt)
53  return cnt
54 
55  def _callback(self, msg):
56  if self._check_img_msg_diff(self.edgemsg, msg) == 0:
57  self.response = True
58 
59  def checkSparseImage(self):
60  cnt = 0
61  while cnt <= 100:
62  try:
63  self._publish()
64  rospy.wait_for_message(self.sub_topic_name, Image, timeout=1.0)
65  except:
66  pass
67  rospy.loginfo("waiting for subscribe...")
68  if self.response:
69  return True
70  cnt += 1
71  return False
72 
73 class TestSparseImage(unittest.TestCase):
75  imgpath = p.join(p.dirname(__file__), "../sample/edge_image_test_240x180.bmp")
76  ei = EdgeImagePublisher(imgpath)
77  self.assertTrue(ei.checkSparseImage())
78 
80  imgpath = p.join(p.dirname(__file__), "../sample/edge_image_test_640x480.bmp")
81  ei = EdgeImagePublisher(imgpath)
82  self.assertTrue(ei.checkSparseImage())
83 
84 
85 if __name__ == "__main__":
86  import rostest
87  rospy.init_node("sparse_image_test_py")
88  rostest.rosrun("jsk_perception", "test_sparse_image", TestSparseImage)
89 
def _check_img_msg_diff(self, msg1, msg2)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27