13 import dynamic_reconfigure.server
14 from jsk_recognition_utils
import bounding_rect_of_mask
15 from jsk_recognition_utils
import get_tile_image
16 from jsk_recognition_utils.color
import labelcolormap
17 from jsk_topic_tools
import ConnectionBasedTransport
18 from jsk_topic_tools
import warn_no_remap
19 import message_filters
21 from sensor_msgs.msg
import Image
23 from jsk_perception.cfg
import LabelImageDecomposerConfig
27 if color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114 > 170:
29 return (255, 255, 255)
32 def label2rgb(lbl, img=None, label_names=None, alpha=0.3, bg_label=0):
33 if label_names
is None:
34 n_labels = lbl.max() + 1
36 n_labels = len(label_names)
37 cmap = labelcolormap(256)
38 cmap = (cmap * 255).astype(np.uint8)
39 bg_color, cmap = cmap[0], cmap[1:]
41 lbl_viz = np.zeros((lbl.shape[0], lbl.shape[1], 3), dtype=np.uint8)
42 fg_mask = lbl != bg_label
43 lbl_viz[fg_mask] = cmap[lbl[fg_mask] % 255]
44 lbl_viz[~fg_mask] = bg_color
48 img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
52 img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
53 lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray
54 lbl_viz = lbl_viz.astype(np.uint8)
56 if label_names
is None:
60 labels = np.unique(lbl)
61 labels = labels[labels != 0]
64 mask = (mask * 255).astype(np.uint8)
65 y, x = scipy.ndimage.center_of_mass(mask)
66 y, x = map(int, [y, x])
68 if lbl[y, x] != label:
70 point_index = np.random.randint(0, len(Y))
71 y, x = Y[point_index], X[point_index]
73 text = label_names[label]
74 font_face = cv2.FONT_HERSHEY_SIMPLEX
77 text_size, baseline = cv2.getTextSize(
78 text, font_face, font_scale, thickness)
81 cv2.putText(lbl_viz, text,
82 (x - text_size[0] // 2, y),
83 font_face, font_scale, color, thickness)
90 super(LabelImageDecomposer, self).
__init__()
95 self.
pub_img = self.advertise(
'~output', Image, queue_size=5)
110 rospy.loginfo(
'~publish_tile: {}'.format(self.
_publish_tile))
112 rospy.logerr(
'Can not publish tile image when ~only_label is true,' 113 ' so forcely set ~publish_tile to false.')
116 self.
pub_tile = self.advertise(
'~output/tile', Image, queue_size=5)
125 self.sub_label.registerCallback(self.
_apply)
128 warn_no_remap(
'~input',
'~input/label')
129 use_async = rospy.get_param(
'~approximate_sync',
False)
130 queue_size = rospy.get_param(
'~queue_size', 10)
131 rospy.loginfo(
'~approximate_sync: {}, queue_size: {}' 132 .format(use_async, queue_size))
134 slop = rospy.get_param(
'~slop', 0.1)
135 rospy.loginfo(
'~slop: {}'.format(slop))
136 sync = message_filters.ApproximateTimeSynchronizer(
138 queue_size=queue_size, slop=slop)
139 sync.registerCallback(self.
_apply)
145 sync.registerCallback(self.
_apply)
150 self.sub_img.sub.unregister()
151 self.sub_label.sub.unregister()
153 def _apply(self, label_msg, img_msg=None):
154 bridge = cv_bridge.CvBridge()
155 label_img = bridge.imgmsg_to_cv2(label_msg)
157 img = bridge.imgmsg_to_cv2(img_msg)
161 applied_msg = bridge.cv2_to_imgmsg(applied, encoding=img_msg.encoding)
162 applied_msg.header = img_msg.header
163 self.pub_img.publish(applied_msg)
165 if img_msg.encoding
in {
'16UC1',
'32SC1'}:
167 min_value, max_value = img.min(), img.max()
168 img = (img - min_value) / (max_value - min_value) * 255
169 img = img.astype(np.uint8)
170 img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
176 label_viz_msg = bridge.cv2_to_imgmsg(label_viz, encoding=
'rgb8')
177 label_viz_msg.header = label_msg.header
178 self.pub_label_viz.publish(label_viz_msg)
182 bg_mask = (label_img == 0)
184 bg_mask = (bg_mask * 255).astype(np.uint8)
185 fg_mask = (fg_mask * 255).astype(np.uint8)
186 fg_mask_msg = bridge.cv2_to_imgmsg(fg_mask, encoding=
'mono8')
187 fg_mask_msg.header = label_msg.header
188 bg_mask_msg = bridge.cv2_to_imgmsg(bg_mask, encoding=
'mono8')
189 bg_mask_msg.header = label_msg.header
190 self.pub_fg_mask.publish(fg_mask_msg)
191 self.pub_bg_mask.publish(bg_mask_msg)
194 bridge = cv_bridge.CvBridge()
195 img = bridge.imgmsg_to_cv2(img_msg)
196 label_img = bridge.imgmsg_to_cv2(label_msg)
199 labels = np.unique(label_img)
206 mask = label_img == label
208 img_tmp = bounding_rect_of_mask(img_tmp, mask)
210 tile_img = get_tile_image(imgs)
211 tile_msg = bridge.cv2_to_imgmsg(tile_img, encoding=
'bgr8')
212 tile_msg.header = img_msg.header
213 self.pub_tile.publish(tile_msg)
216 if __name__ ==
'__main__':
217 rospy.init_node(
'label_image_decomposer')
def _apply_tile(self, label_msg, img_msg)
def get_text_color(color)
def _config_callback(self, config, level)
def _apply(self, label_msg, img_msg=None)
def label2rgb(lbl, img=None, label_names=None, alpha=0.3, bg_label=0)