def _apply(self, imgmsg, maskmsg):
     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])
     mask = gray2rgb(mask)
     # 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.fill(-1)  # set bg_label
     labels[roi] = roi_labels
     if self.is_debugging:
         # publish debug slic label
         slic_labelmsg = bridge.cv2_to_imgmsg(labels)
         slic_labelmsg.header = imgmsg.header
         self.pub_slic.publish(slic_labelmsg)
     # compute rag
     g = rag_solidity(labels, connectivity=2)
     if self.is_debugging:
         # publish debug rag drawn image
         rag_img = draw_rag(labels, g, img)
         rag_img = img_as_uint(rag_img)
         rag_imgmsg = bridge.cv2_to_imgmsg(rag_img.astype(np.uint8),
                                           encoding='rgb8')
         rag_imgmsg.header = imgmsg.header
         self.pub_rag.publish(rag_imgmsg)
     # merge rag with solidity
     merged_labels = merge_hierarchical(labels,
                                        g,
                                        thresh=1,
                                        rag_copy=False,
                                        in_place_merge=True,
                                        merge_func=_solidity_merge_func,
                                        weight_func=_solidity_weight_func)
     merged_labels += 1
     merged_labels[mask == 0] = 0
     merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32))
     merged_labelmsg.header = imgmsg.header
     self.pub.publish(merged_labelmsg)
     if self.is_debugging:
         out = label2rgb(merged_labels, img)
         out = (out * 255).astype(np.uint8)
         out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8')
         out_msg.header = imgmsg.header
         self.pub_label.publish(out_msg)
 def _apply(self, imgmsg, maskmsg):
     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])
     mask = gray2rgb(mask)
     # 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.fill(-1)  # set bg_label
     labels[roi] = roi_labels
     if self.is_debugging:
         # publish debug slic label
         slic_labelmsg = bridge.cv2_to_imgmsg(labels)
         slic_labelmsg.header = imgmsg.header
         self.pub_slic.publish(slic_labelmsg)
     # compute rag
     g = rag_solidity(labels, connectivity=2)
     if self.is_debugging:
         # publish debug rag drawn image
         rag_img = draw_rag(labels, g, img)
         rag_img = img_as_uint(rag_img)
         rag_imgmsg = bridge.cv2_to_imgmsg(
             rag_img.astype(np.uint8), encoding='rgb8')
         rag_imgmsg.header = imgmsg.header
         self.pub_rag.publish(rag_imgmsg)
     # merge rag with solidity
     merged_labels = merge_hierarchical(
         labels, g, thresh=1, rag_copy=False,
         in_place_merge=True,
         merge_func=_solidity_merge_func,
         weight_func=_solidity_weight_func)
     merged_labels += 1
     merged_labels[mask == 0] = 0
     merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32))
     merged_labelmsg.header = imgmsg.header
     self.pub.publish(merged_labelmsg)
     if self.is_debugging:
         out = label2rgb(merged_labels, img)
         out = (out * 255).astype(np.uint8)
         out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8')
         out_msg.header = imgmsg.header
         self.pub_label.publish(out_msg)
 def _apply(self, imgmsg, maskmsg):
     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],
                              n_segments=20,
                              compactness=30)
     if roi_labels is None:
         return
     labels = np.zeros(mask.shape, dtype=np.int32)
     # 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)
         slic_labelmsg.header = imgmsg.header
         self.pub_slic.publish(slic_labelmsg)
     # compute rag
     g = rag_solidity(labels, connectivity=2)
     if self.is_debugging:
         # publish debug rag drawn image
         if LooseVersion(skimage.__version__) >= '0.13.0':
             fig, ax = plt.subplots(figsize=(img.shape[1] * 0.01,
                                             img.shape[0] * 0.01))
             show_rag(labels, g, img, ax=ax)
             ax.axis('off')
             plt.subplots_adjust(0, 0, 1, 1)
             fig.canvas.draw()
             w, h = fig.canvas.get_width_height()
             rag_img = np.fromstring(fig.canvas.tostring_rgb(),
                                     dtype=np.uint8)
             rag_img.shape = (h, w, 3)
             plt.close()
         else:
             rag_img = draw_rag(labels, g, img)
             rag_img = img_as_uint(rag_img)
         rag_imgmsg = bridge.cv2_to_imgmsg(rag_img.astype(np.uint8),
                                           encoding='rgb8')
         rag_imgmsg.header = imgmsg.header
         self.pub_rag.publish(rag_imgmsg)
     # merge rag with solidity
     merged_labels = merge_hierarchical(labels,
                                        g,
                                        thresh=1,
                                        rag_copy=False,
                                        in_place_merge=True,
                                        merge_func=_solidity_merge_func,
                                        weight_func=_solidity_weight_func)
     merged_labels += 1
     merged_labels[mask == 0] = 0
     merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32))
     merged_labelmsg.header = imgmsg.header
     self.pub.publish(merged_labelmsg)
     if self.is_debugging:
         out = label2rgb(merged_labels, img)
         out = (out * 255).astype(np.uint8)
         out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8')
         out_msg.header = imgmsg.header
         self.pub_label.publish(out_msg)
Ejemplo n.º 4
0
def main():
    import argparse
    import matplotlib
    import matplotlib.pyplot as plt
    import os
    import time
    from skimage.io import imread
    from util import closed_mask_roi
    from util import masked_slic

    parser = argparse.ArgumentParser()
    parser.add_argument('img_file')
    parser.add_argument('depth_file')
    args = parser.parse_args()

    img_file = args.img_file
    depth_file = args.depth_file

    img = imread(img_file)
    depth = imread(depth_file)
    depth_rgb = gray2rgb(depth)
    plt.figure(figsize=(24, 10))
    plt.subplot(231)
    plt.axis('off')
    plt.imshow(depth_rgb)

    t_start = time.time()
    roi = closed_mask_roi(depth)
    roi_labels = masked_slic(img=depth_rgb[roi], mask=depth[roi],
                             n_segments=20, compactness=30)
    print('slic: takes {} [secs]'.format(time.time() - t_start))

    labels = np.zeros_like(depth)
    labels[roi] = roi_labels
    print('n_labels: {}'.format(len(np.unique(labels))))

    out = label2rgb(labels, img, bg_label=0)
    plt.subplot(232)
    plt.axis('off')
    plt.imshow(out)

    t_start = time.time()
    g = rag_solidity(labels, connectivity=2)
    print('rag_solidity: takes {} [secs]'.format(time.time() - t_start))

    # draw rag: all
    out = graph.draw_rag(labels, g, img)
    plt.subplot(233)
    plt.axis('off')
    plt.imshow(out)
    # draw rag: good edges
    cmap = matplotlib.colors.ListedColormap(['#0000FF', '#FF0000'])
    out = graph.draw_rag(labels, g, img, node_color='#00FF00', colormap=cmap,
                         thresh=1, desaturate=True)
    plt.subplot(234)
    plt.axis('off')
    plt.imshow(out)

    t_start = time.time()
    merged_labels = graph.merge_hierarchical(
        labels, g, thresh=1, rag_copy=False,
        in_place_merge=True,
        merge_func=_solidity_merge_func, weight_func=_solidity_weight_func)
    print('graph.merge_hierarchical: takes {} [secs]'.format(time.time() - t_start))

    out = label2rgb(merged_labels, img, bg_label=0)
    plt.subplot(235)
    plt.axis('off')
    plt.imshow(out)

    merged_g = rag_solidity(merged_labels)
    # draw rag: all
    out = graph.draw_rag(merged_labels, merged_g, img)
    plt.subplot(236)
    plt.axis('off')
    plt.imshow(out)

    # plt.show()
    plt.savefig('{}_solidity_rag_merge.png'.format(time.time()), dpi=100)
Ejemplo n.º 5
0
======================================

This example constructs a Region Adjacency Graph (RAG) and draws it with
the `rag_draw` method.
"""
from skimage import data, segmentation
from skimage.future import graph
from skimage.util.colormap import viridis
from matplotlib import pyplot as plt, colors


img = data.coffee()
labels = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_mean_color(img, labels)

out = graph.draw_rag(labels, g, img)
plt.figure()
plt.title("RAG with all edges shown in green.")
plt.imshow(out)

# The color palette used was taken from
# http://www.colorcombos.com/color-schemes/2/ColorCombo2.html
cmap = colors.ListedColormap(['#6599FF', '#ff9900'])
out = graph.draw_rag(labels, g, img, node_color="#ffde00", colormap=cmap,
                     thresh=30, desaturate=True)
plt.figure()
plt.title("RAG with edge weights less than 30, color "
          "mapped between blue and orange.")
plt.imshow(out)

plt.figure()
Ejemplo n.º 6
0
    S = scene_segment()

    while not rospy.is_shutdown():
        if S.img == []: continue
        print 'start'
        img = S.img.copy()
        labels = segmentation.slic(img, compactness=30, n_segments=200)
        S.set_parameters(labels)
        print labels
        print '1 segment'
        g = graph.rag_mean_color(img, labels)
        #print g.nodes()
        #print g.edges()
        print '2 rag_mean_color'
        out = graph.draw_rag(labels, g, img)

        #seg = color.label2rgb(labels, img, kind='avg')
        #print '5 label2rgb'
        #print '6 mark_boundaries'
        labels2 = {}
        #for i in range(90,100,20):
        i = 80

        seg = segmentation.mark_boundaries(img, labels, (0, 0, 0))
        cmap = colors.ListedColormap(['#6599FF', '#ff9900'])
        out1 = graph.draw_rag(labels,
                              g,
                              seg,
                              node_color="#ffde00",
                              colormap=cmap,
Ejemplo n.º 7
0
Drawing Region Adjacency Graphs (RAGs)
======================================

This example constructs a Region Adjacency Graph (RAG) and draws it with
the `rag_draw` method.
"""
from skimage import data, segmentation
from skimage.future import graph
from skimage.util.colormap import viridis
from matplotlib import pyplot as plt, colors

img = data.coffee()
labels = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_mean_color(img, labels)

out = graph.draw_rag(labels, g, img)
plt.figure()
plt.title("RAG with all edges shown in green.")
plt.imshow(out)

# The color palette used was taken from
# http://www.colorcombos.com/color-schemes/2/ColorCombo2.html
cmap = colors.ListedColormap(['#6599FF', '#ff9900'])
out = graph.draw_rag(labels,
                     g,
                     img,
                     node_color="#ffde00",
                     colormap=cmap,
                     thresh=30,
                     desaturate=True)
plt.figure()
Ejemplo n.º 8
0
    img = imread(img_file)
    depth = imread(depth_file)
    depth_rgb = gray2rgb(depth)
    plt.figure(figsize=(24, 10))
    plt.subplot(131)
    plt.axis('off')
    plt.imshow(depth_rgb)

    roi = closed_mask_roi(depth)
    roi_labels = masked_slic(img=depth_rgb[roi], mask=depth[roi],
                             n_segments=100, compactness=30)

    labels = np.zeros_like(depth)
    labels[roi] = roi_labels

    out = label2rgb(labels, img, bg_label=0)
    plt.subplot(132)
    plt.axis('off')
    plt.imshow(out)

    g = rag_solidity(labels)

    out = draw_rag(labels, g, img, colormap=viridis, desaturate=True)
    plt.subplot(133)
    plt.axis('off')
    plt.imshow(out)

    # plt.show()
    plt.savefig('{}_rag_solidity.png'.format(time.time()))
Ejemplo n.º 9
0
"""
==========================
Region Boundary based RAGs
==========================

This example demonstrates construction of region boundary based RAGs with the
`rag_boundary` function.
"""
from skimage.future import graph
from skimage import data, segmentation, color, filters, io
from skimage.util.colormap import viridis


img = data.coffee()
gimg = color.rgb2gray(img)

labels = segmentation.slic(img, compactness=30, n_segments=400)
edges = filters.sobel(gimg)
edges_rgb = color.gray2rgb(edges)

g = graph.rag_boundary(labels, edges)

out = graph.draw_rag(labels, g, edges_rgb, node_color="#999999",
                     colormap=viridis)

io.imshow(out)
io.show()
Ejemplo n.º 10
0
"""
==========================
Region Boundary based RAGs
==========================

This example demonstrates construction of region boundary based RAGs with the
`rag_boundary` function.
"""
from skimage.future import graph
from skimage import data, segmentation, color, filters, io
from skimage.util.colormap import viridis

img = data.coffee()
gimg = color.rgb2gray(img)

labels = segmentation.slic(img, compactness=30, n_segments=400)
edges = filters.sobel(gimg)
edges_rgb = color.gray2rgb(edges)

g = graph.rag_boundary(labels, edges)

out = graph.draw_rag(labels,
                     g,
                     edges_rgb,
                     node_color="#999999",
                     colormap=viridis)

io.imshow(out)
io.show()