7 from jsk_topic_tools
import ConnectionBasedTransport
8 from jsk_recognition_msgs.msg
import RectArray, Rect
11 from sensor_msgs.msg
import Image
18 return max(self.rect.width, self.rect.height) - max(another.rect.width, another.rect.height)
20 return min(self.rect.width, self.rect.height) - min(another.rect.width, another.rect.height)
22 return self.rect.height - another.rect.height
24 return self.rect.width - another.rect.width
31 def fit(self, blocks):
34 self.
root =
Block(Rect(x=0, y=0, width=blocks[0].rect.width, height=blocks[0].rect.height))
36 node = self.
find_node(self.
root, block.rect.width, block.rect.height)
38 block.fit_location = self.
split(node, block.rect.width, block.rect.height)
40 block.fit_location = self.
grow_node(block.rect.width, block.rect.height)
44 elif w <= root.rect.width
and h <= root.rect.height:
50 node.down =
Block(Rect(x=node.rect.x, y=node.rect.y + h, width=w, height=node.rect.height - h))
51 node.right =
Block(Rect(x=node.rect.x + w, y=node.rect.y, width=node.rect.width - w, height=h))
54 can_grow_down = w <= self.root.rect.width
55 can_grow_right = h <= self.root.rect.height
56 should_grow_right = can_grow_right
and self.root.rect.height >= self.root.rect.width + w
57 should_grow_down = can_grow_down
and self.root.rect.width >= self.root.rect.height + h
60 elif should_grow_down:
70 self.
root =
Block(Rect(x=0, y=0, width=self.root.rect.width + w, height=self.root.rect.height))
71 self.root.down = prev_root
72 self.root.right =
Block(Rect(x=prev_root.rect.width, y=0, width=w, height=prev_root.rect.height))
76 return self.
split(node, w, h)
81 self.
root =
Block(Rect(x=0, y=0, width=self.root.rect.width, height=self.root.rect.height + h))
82 self.root.right = prev_root
83 self.root.down =
Block(Rect(x=0, y=prev_root.rect.height, width=prev_root.rect.width, height=h))
87 return self.
split(node, w, h)
99 self.
pub_ = self.advertise(
'~output', Image, queue_size=10)
105 if rospy.get_param(
'~approximate_sync',
False):
106 self.
sync = message_filters.ApproximateTimeSynchronizer(
111 self.sync.registerCallback(self.
_apply)
113 self.sub_.sub.unregister()
114 self.sub_rects_.sub.unregister()
116 bridge = cv_bridge.CvBridge()
117 img = bridge.imgmsg_to_cv2(img_msg)
118 root_rect, blocks = self.
bin_pack(rects)
119 output_img = np.zeros((root_rect.height, root_rect.width, img.shape[2]), dtype=img.dtype)
121 location = block.fit_location
122 output_img[location.rect.y:location.rect.y + block.rect.height,
123 location.rect.x:location.rect.x + block.rect.width] = img[block.rect.y:block.rect.y+block.rect.height, block.rect.x:block.rect.x + block.rect.width]
124 output_img_msg = bridge.cv2_to_imgmsg(output_img, encoding=img_msg.encoding)
125 output_img_msg.header = img_msg.header
126 self.pub_.publish(output_img_msg)
130 blocks = [
Block(rect)
for rect
in rects.rects]
133 blocks.sort(reverse=
True)
135 print((packer.root.rect.width, packer.root.rect.height))
136 return (packer.root.rect, blocks)
143 if __name__ ==
'__main__':
144 rospy.init_node(
'binpack')
def min_cmp(self, another)
def grow_down(self, w, h)
def grow_node(self, w, h)
def split(self, node, w, h)
def height_cmp(self, another)
def bin_pack(self, rects)
def __cmp__(self, another)
def max_cmp(self, another)
def grow_right(self, w, h)
def width_cmp(self, another)
def _apply(self, img_msg, rects)
def find_node(self, root, w, h)