Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add use_mask param in solidity_rag_merge #2855

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions jsk_perception/node_scripts/solidity_rag_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,39 +165,63 @@ def __init__(self):
queue_size=5)

def subscribe(self):
self.sub = message_filters.Subscriber('~input', Image)
self.sub_mask = message_filters.Subscriber('~input/mask', Image)
self.use_async = rospy.get_param('~approximate_sync', False)
rospy.loginfo('~approximate_sync: {}'.format(self.use_async))
if self.use_async:
sync = message_filters.ApproximateTimeSynchronizer(
[self.sub, self.sub_mask], queue_size=1000, slop=0.1)
self.use_mask = rospy.get_param('~use_mask', True)
if self.use_mask:
self.sub = message_filters.Subscriber('~input', Image)
self.sub_mask = message_filters.Subscriber('~input/mask', Image)
self.use_async = rospy.get_param('~approximate_sync', False)
rospy.loginfo('~approximate_sync: {}'.format(self.use_async))
if self.use_async:
sync = message_filters.ApproximateTimeSynchronizer(
[self.sub, self.sub_mask], queue_size=1000, slop=0.1)
else:
sync = message_filters.TimeSynchronizer(
[self.sub, self.sub_mask], queue_size=1000)
sync.registerCallback(self.sub_cb)
warn_no_remap('~input', '~input/mask')
else:
sync = message_filters.TimeSynchronizer(
[self.sub, self.sub_mask], queue_size=1000)
sync.registerCallback(self._apply)
warn_no_remap('~input', '~input/mask')
self.sub = rospy.Subscriber('~input', Image, self.sub_img_cb,
queue_size=1)
warn_no_remap('~input')

def unsubscribe(self):
self.sub.unregister()
self.sub_mask.unregister()
if self.use_mask:
self.sub_mask.unregister()

def sub_cb(self, imgmsg, maskmsg):
self._apply(imgmsg, maskmsg)

def sub_img_cb(self, imgmsg):
self._apply(imgmsg, None)

def _apply(self, imgmsg, maskmsg):
def _apply(self, imgmsg, maskmsg=None):
bridge = cv_bridge.CvBridge()
img = bridge.imgmsg_to_cv2(imgmsg)
if img.ndim == 2:
img = gray2rgb(img)
mask = bridge.imgmsg_to_cv2(maskmsg, desired_encoding='mono8')
mask = mask.reshape(mask.shape[:2])
# compute label
roi = closed_mask_roi(mask)
roi_labels = masked_slic(img=img[roi], mask=mask[roi],

if maskmsg is None:
# compute label
mask = np.ones(img.shape[:2], dtype=np.uint8)
labels = masked_slic(img=img, mask=mask,
n_segments=20, compactness=30)
if roi_labels is None:
return
labels = np.zeros(mask.shape, dtype=np.int32)
if labels is None:
return
labels = labels.astype(np.int32)
else:
mask = bridge.imgmsg_to_cv2(maskmsg, desired_encoding='mono8')
mask = mask.reshape(mask.shape[:2])
# compute label
roi = closed_mask_roi(mask)
roi_labels = masked_slic(img=img[roi], mask=mask[roi],
n_segments=20, compactness=30)
if roi_labels is None:
return
labels = np.zeros(mask.shape, dtype=np.int32)
labels[roi] = roi_labels

# labels.fill(-1) # set bg_label
labels[roi] = roi_labels
if self.is_debugging:
# publish debug slic label
slic_labelmsg = bridge.cv2_to_imgmsg(labels)
Expand Down
Loading