split_image.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import cv_bridge
5 import rospy
6 from sensor_msgs.msg import Image
7 from jsk_topic_tools import ConnectionBasedTransport
8 
9 
10 class SplitImage(ConnectionBasedTransport):
11 
12  def __init__(self):
13  super(SplitImage, self).__init__()
14  self.vertical_parts = rospy.get_param('~vertical_parts', 1)
15  self.horizontal_parts = rospy.get_param('~horizontal_parts', 1)
16  self.pubs = []
17  for v in range(self.vertical_parts):
18  pubs = []
19  for h in range(self.horizontal_parts):
20  pubs.append(
21  self.advertise(
22  '~output/vertical{0:02}/horizontal{1:02}'.format(v, h),
23  Image,
24  queue_size=10))
25  self.pubs.append(pubs)
26  self.bridge = cv_bridge.CvBridge()
27 
28  def subscribe(self):
29  self.sub = rospy.Subscriber('~input', Image, self._split_cb)
30 
31  def unsubscribe(self):
32  self.sub.unregister()
33 
34  def _split_cb(self, msg):
35  img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
36  height, width, _ = img.shape
37  for v in range(self.vertical_parts):
38  for h in range(self.horizontal_parts):
39  v_pixels = float(height) / self.vertical_parts
40  h_pixels = float(width) / self.horizontal_parts
41  split_img = img[int(v*v_pixels):int((v+1)*v_pixels),
42  int(h*h_pixels):int((h+1)*h_pixels)]
43  pub_msg = self.bridge.cv2_to_imgmsg(split_img, encoding='bgr8')
44  pub_msg.header = msg.header
45  self.pubs[v][h].publish(pub_msg)
46 
47 
48 if __name__ == '__main__':
49  rospy.init_node('split_image')
50  SplitImage()
51  rospy.spin()


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