def customize_threshold_value(reg_count,thresh_val):
    labels2 = graph.merge_hierarchical(labels, g, thresh=thresh_val, rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=merge_mean_color,
                                   weight_func=_weight_mean_color)
    while not (reg_count <= 15):
        thresh_val = 2 + thresh_val
        print("Thrshold value is",thresh_val)
        labels2 = graph.merge_hierarchical(labels, g, thresh=thresh_val, rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=merge_mean_color,
                                   weight_func=_weight_mean_color)
        reg_count = count_regions(labels2)
    return labels2,reg_count
Example #2
0
def get_patches(img, compactness=30, n_segments=200, rag_thresh=0.08):
    """Get list of patches from image found with SLIC."""
    patches = []
    img_lab = color.rgb2lab(img)
    edges = filters.sobel(color.rgb2gray(img))
    labels = segmentation.slic(img_lab, convert2lab=False,
                               compactness=compactness, n_segments=n_segments)
    g = graph.rag_boundary(labels, edges)
    segmented = graph.merge_hierarchical(labels, g, thresh=rag_thresh,
                                         rag_copy=False,
                                         in_place_merge=True,
                                         merge_func=merge_boundary,
                                         weight_func=weight_boundary)

    idxs_sorted = np.argsort(segmented.reshape((segmented.size)))
    _, idxs_start = np.unique(
        segmented.reshape((segmented.size))[idxs_sorted],
        return_index=True)
    idxs_for_values = np.split(idxs_sorted, idxs_start[1:])
    for idxs in idxs_for_values:
        rows, cols = np.unravel_index(idxs, img.shape[:2])
        data = img[rows, cols]
        lab = np.mean(img_lab[rows, cols], axis=0)
        patches.append((rows, cols, data, lab))
    return patches
def get_groups(original):
    """
    Finds a segmentation of image by taking an oversegmentation produced by the Priority-Flood watershed and
    progressively reducing with a boundary region adjacency graph

    :param original: Original RGB image to segment
    :return: Segmented image. label = 0 represents an edge, label = -1 represents a pruned area
    """
    original = gaussian(original, sigma=1.5, multichannel=True)
    original = downscale_to(original, area_limit=2e5)
    g = original[:, :, 1]

    def weight_boundary(RAG, src, dst, n):
        default = {'weight': 0.0, 'count': 0}
        count_src = RAG[src].get(n, default)['count']
        count_dst = RAG[dst].get(n, default)['count']
        weight_src = RAG[src].get(n, default)['weight']
        weight_dst = RAG[dst].get(n, default)['weight']
        count = count_src + count_dst
        return {
            'count': count,
            'weight': (count_src * weight_src + count_dst * weight_dst) / count
        }

    greyscale = rgb2gray(original)
    gradient = np.hypot(sobel(greyscale, axis=0), sobel(greyscale, axis=1))
    segmentation1 = watershed(gradient, markers=400, mask=greyscale > 0.3)
    RAG = graph.rag_boundary(segmentation1, gradient)
    segmentation2 = graph.merge_hierarchical(segmentation1,
                                             RAG,
                                             thresh=5e-3,
                                             rag_copy=False,
                                             in_place_merge=True,
                                             merge_func=lambda *args: None,
                                             weight_func=weight_boundary)
    segmentation2[greyscale < 0.3] = -1
    segmentation2 = prune(segmentation2,
                          abs_threshold=g.size / 1000,
                          default=-1)
    counts, lo, hi = [], [], []
    for label in set(segmentation2[segmentation2 >= 0]):
        interior = distance_transform_edt(segmentation2 == label) >= 1.5
        if np.sum(interior) >= 0:
            counts.append(np.sum(interior))
            lo.append(np.percentile(gradient[interior], q=70))
            hi.append(np.percentile(gradient[interior], q=90))

    edges = canny(greyscale,
                  low_threshold=np.average(lo, weights=counts),
                  high_threshold=np.average(hi, weights=counts))
    edges = binary_dilation(edges, disk(2))
    edges = binary_closing(edges, disk(5))
    edges = remove_small_objects(edges, g.size / 1000)
    edges = edges[1:-1, 1:-1]
    edges = np.pad(edges, pad_width=1, mode='constant', constant_values=1)
    groups = im_label(edges, background=1, connectivity=1)
    groups = prune(groups, abs_threshold=g.size / 1000)
    groups[greyscale <
           0.15] = -2  # Ignore black areas due to mechanical vignetting
    return groups
Example #4
0
def hierarchical_merging_of_region_boundary_rags_example():
    #img = data.coffee()

    edges = sobel(skimage.color.rgb2gray(img))

    labels = slic(img, compactness=30, n_segments=400)
    g = graph.rag_boundary(labels, edges)

    graph.show_rag(labels, g, img)
    plt.title('Initial RAG')

    labels2 = graph.merge_hierarchical(labels,
                                       g,
                                       thresh=0.08,
                                       rag_copy=False,
                                       in_place_merge=True,
                                       merge_func=merge_boundary,
                                       weight_func=weight_boundary)

    graph.show_rag(labels, g, img)
    plt.title('RAG after hierarchical merging')

    plt.figure()
    out = skimage.color.label2rgb(labels2, img, kind='avg')
    plt.imshow(out)
    plt.title('Final segmentation')

    plt.show()
Example #5
0
def show_segmen_rag(array,
                    pathlibpath,
                    numSegments,
                    threshold,
                    cedges=177,
                    compactness=0.1,
                    sigma=5,
                    convert2lab=False):
    print('Obtaining superstructures')
    segments = slic(array,
                    compactness=compactness,
                    n_segments=numSegments,
                    sigma=sigma,
                    multichannel=False,
                    convert2lab=convert2lab)
    print('Number of SV: ', len(np.unique(segments)))
    segments += 1
    edges = filters.sobel(array)
    print('Obtaining RAG')
    rag = graph.rag_boundary(segments, edges, connectivity=1)
    segments2 = graph.merge_hierarchical(segments,
                                         rag,
                                         threshold=threshold,
                                         in_place_merge=True,
                                         rag_copy=False,
                                         merge_func=merge_boundary,
                                         weight_func=weight_boundary)
    print('Final Number of SV: ', len(np.unique(segments2)))

    graph.show_rag(segments, rag, array, edge_cmap='viridis')
    save_yorno(array, segments, pathlibpath, cedges)
def merge_hierarchical_mean_color(labels,
                                  rag,
                                  thresh,
                                  rag_copy=True,
                                  in_place_merge=False):
    return graph.merge_hierarchical(labels, rag, thresh, rag_copy,
                                    in_place_merge, _pre_merge_mean_color,
                                    _weight_mean_color)
Example #7
0
def segmentation_reduction(img,
                           labels,
                           n_segments,
                           reduction=None,
                           kind='mix',
                           cs=None):

    if reduction == 'selective':
        # selective search
        img_cvtcolor = label2rgb(labels,
                                 img,
                                 kind=kind,
                                 bg_label=-1,
                                 bg_color=(0, 0, 0))

        if cs == 'lab':
            img_cvtcolor = cv2.cvtColor(img_cvtcolor, cv2.COLOR_BGR2LAB)
        elif cs == 'hsv':
            img_cvtcolor = cv2.cvtColor(img_cvtcolor, cv2.COLOR_BGR2HSV)

        merged_labels = selective_search(img_cvtcolor,
                                         labels,
                                         seg_num=n_segments,
                                         sim_strategy='CTSF')
        rgbmap = label2rgb(merged_labels, img, kind=kind)
    elif reduction == 'cluster':
        # aggregate colors in each of the labels and output
        _, rbg_labels = label2rgb(labels,
                                  img,
                                  kind=kind,
                                  bg_label=-1,
                                  bg_color=(0, 0, 0),
                                  ret_rbg_labels=True)
        # apply kmeans clustering to the resulting color labels
        ret, klabels, centroids = apply_kmeans(
            np.array(rbg_labels, dtype=np.float32), n_segments)
        reduced_colors = centroids[klabels.flatten()]
        rgbmap = label2rgb(labels, img, reduced_colors=reduced_colors)
    elif reduction == 'rag':
        # Region Adjacency Graph (RAG)
        g = rag_mean_color(img, labels)
        merged_labels = merge_hierarchical(labels,
                                           g,
                                           thresh=35,
                                           rag_copy=False,
                                           in_place_merge=True,
                                           merge_func=merge_mean_color,
                                           weight_func=_weight_mean_color)
        rgbmap = label2rgb(merged_labels,
                           img,
                           kind=kind,
                           bg_label=-1,
                           bg_color=(0, 0, 0))
    else:
        rgbmap = img

    return rgbmap
Example #8
0
def cluster_merge(volume, labels):
    g = graph.rag_mean_color(volume, labels)
    labels_merged = graph.merge_hierarchical(labels,
                                             g,
                                             thresh=0.03,
                                             rag_copy=False,
                                             in_place_merge=True,
                                             merge_func=merge_mean_color,
                                             weight_func=weight_mean_color)
    return labels_merged
Example #9
0
 def initial_formation(self, labels, g):
     threshold_value = 50
     labels2 = graph.merge_hierarchical(labels,
                                        g,
                                        thresh=threshold_value,
                                        rag_copy=False,
                                        in_place_merge=True,
                                        merge_func=self.merge_mean_color,
                                        weight_func=self._weight_mean_color)
     return labels2
 def graphMergeHierarchical(self,img, labels,thresh=0.15,plot=True,save=0):
      g = graph.rag_mean_color(img,labels)
      new_labels = graph.merge_hierarchical(labels, g, thresh, rag_copy=False,
                                         in_place_merge=True,
                                         merge_func=merge_mean_color,
                                         weight_func=_weight_mean_color)
      if plot:        
          plotLibs().dispImg(color.label2rgb(new_labels, img, kind='avg'),save=save,title='Merge Hierarchical Label rgb Image')
          plotLibs().plotBoundaries(img,new_labels,save=save,title='Merge Hierarchical')
      return new_labels
Example #11
0
 def segment_image(self,img,name):
     labels = segmentation.slic(img, compactness=30, n_segments=200)
     g = graph.rag_mean_color(img, labels)
     labels2 = graph.merge_hierarchical(labels, g, thresh=80, rag_copy=False,
                                    in_place_merge=True,
                                    merge_func=self.merge_mean_color,
                                    weight_func=self._weight_mean_color)
     self.result = color.label2rgb(labels2, img, kind='avg')
     cv2.imshow('segmentation : '+str(name), self.result)
     k = cv2.waitKey(1) & 0xFF
     return self.result
Example #12
0
def partition_image(img, n_segments):
    # lab_img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    superpixels = slic(img, n_segments, sigma=sigma, multichannel=multichannel, convert2lab=convert2lab,
                       slic_zero=slic_zero, enforce_connectivity=False)
    ent_rag = graph.rag_mean_color(img, superpixels)
    regions = graph.merge_hierarchical(superpixels, ent_rag, thresh=hier_thresh, rag_copy=rag_copy,
                                       in_place_merge=in_place_merge,
                                       merge_func=merge_mean_color,
                                       weight_func=weight_mean_color)
    #     out = color.label2rgb(regions, img, kind='avg')
    #     out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
    return regions
 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)
Example #14
0
 def get_segments(self, frame):
     rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
     labels = slic(rgb, self.n_segments)
     edges = filters.sobel(color.rgb2gray(rgb))
     g = graph.rag_boundary(labels, edges)
     labels2 = graph.merge_hierarchical(labels,
                                        g,
                                        thresh=0.02,
                                        rag_copy=False,
                                        in_place_merge=True,
                                        merge_func=merge_boundary,
                                        weight_func=weight_boundary)
     return labels2.astype(np.uint8)
Example #15
0
 def process(self, data):
     with Timer('superpixelnode_2'):
         im_l, im_r, rgb, labels = data
         edges = filters.sobel(color.rgb2gray(rgb))
         g = graph.rag_boundary(labels, edges)
         labels2 = graph.merge_hierarchical(labels,
                                            g,
                                            thresh=0.02,
                                            rag_copy=False,
                                            in_place_merge=True,
                                            merge_func=merge_boundary,
                                            weight_func=weight_boundary)
     return im_l, im_r, labels2.astype(np.uint8)
Example #16
0
 def preview(self, ips, para):
     lab = self.app.get_img(para['lab']).img
     connect = ['4-connected', '8-connected'].index(para['connect']) + 1
     g = graph.rag_mean_color(ips.snap, lab, connect, para['mode'],
                              para['sigma'])
     lab = graph.merge_hierarchical(lab,
                                    g,
                                    thresh=para['thresh'],
                                    rag_copy=False,
                                    in_place_merge=True,
                                    merge_func=merge_mean_color,
                                    weight_func=_weight_mean_color)
     ips.img[:] = color.label2rgb(lab, ips.snap, kind='avg')
    def _merge_superpixels(
        self,
        input_image: np.ndarray,
        initial_superpixels: np.ndarray,
        tissue_mask: np.ndarray = None,
    ) -> np.ndarray:
        """Merge the initial superpixels to return merged superpixels
        Args:
            image (np.array): Input image
            initial_superpixels (np.array): Initial superpixels
            tissue_mask (None, np.array): Tissue mask
        Returns:
            np.array: Output merged superpixel tensor
        """
        if tissue_mask is not None:
            # Remove superpixels belonging to background or having < 10% tissue
            # content
            ids_initial = np.unique(initial_superpixels, return_counts=True)
            ids_masked = np.unique(tissue_mask * initial_superpixels,
                                   return_counts=True)

            ctr = 1
            superpixels = np.zeros_like(initial_superpixels)
            for i in range(len(ids_initial[0])):
                id = ids_initial[0][i]
                if id in ids_masked[0]:
                    idx = np.where(id == ids_masked[0])[0]
                    ratio = ids_masked[1][idx] / ids_initial[1][i]
                    if ratio >= 0.1:
                        superpixels[initial_superpixels == id] = ctr
                        ctr += 1

            initial_superpixels = superpixels

        # Merge superpixels within tissue region
        g = self._generate_graph(input_image, initial_superpixels)
        merged_superpixels = graph.merge_hierarchical(
            initial_superpixels,
            g,
            thresh=self.threshold,
            rag_copy=False,
            in_place_merge=True,
            merge_func=self._merging_function,
            weight_func=self._weighting_function,
        )
        merged_superpixels += 1  # Handle regionprops that ignores all values of 0
        mask = np.zeros_like(initial_superpixels)
        mask[initial_superpixels != 0] = 1
        merged_superpixels = merged_superpixels * mask
        return merged_superpixels
 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)
Example #19
0
def rag_merging_example():
    img = data.coffee()

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

    labels2 = graph.merge_hierarchical(labels,
                                       g,
                                       thresh=35,
                                       rag_copy=False,
                                       in_place_merge=True,
                                       merge_func=merge_mean_color,
                                       weight_func=_weight_mean_color)

    out = skimage.color.label2rgb(labels2, img, kind='avg')
    out = mark_boundaries(out, labels2, (0, 0, 0))
    skimage.io.imshow(out)
    skimage.io.show()
Example #20
0
    def get_segments(self, frame):

        if not self.initialized:
            self.rgb = np.empty_like(frame)
            self.lab = np.empty_like(frame)
            self.initialized = True

        #with Timer('super-labels1'):
        cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, dst=self.rgb)
        labels = slic(self.rgb, self.n_segments)
        #with Timer('super-labels2'):
        edges = filters.sobel(color.rgb2gray(self.rgb))
        g = graph.rag_boundary(labels, edges)
        labels2 = graph.merge_hierarchical(labels,
                                           g,
                                           thresh=0.02,
                                           rag_copy=False,
                                           in_place_merge=True,
                                           merge_func=merge_boundary,
                                           weight_func=weight_boundary)
        return labels2.astype(np.uint8)
Example #21
0
 def run(self, ips, imgs, para=None):
     if not para['stack']:
         imgs, labs = [ips.img], [self.app.get_img(para['lab']).img]
     else:
         labs = self.app.get_img(para['lab']).imgs
         if len(imgs) != len(labs):
             labs = [self.app.get_img(para['lab']).img] * len(imgs)
     for i in range(len(imgs)):
         img, lab = imgs[i], labs[i]
         connect = ['4-connected', '8-connected'].index(para['connect']) + 1
         g = graph.rag_mean_color(img, lab, connect, para['mode'],
                                  para['sigma'])
         lab = graph.merge_hierarchical(lab,
                                        g,
                                        thresh=para['thresh'],
                                        rag_copy=False,
                                        in_place_merge=True,
                                        merge_func=merge_mean_color,
                                        weight_func=_weight_mean_color)
         img[:] = color.label2rgb(lab, img, kind='avg')
         self.progress(i, len(imgs))
Example #22
0
def rag_merge_threshold(edges, labels, threshold, size_pen):
    '''
    Merge adjacent segments using region adjacency graph based on strength of
    edge between them.
    '''

    # create region adjacency graph using the edge info to produce
    # edge weights between the nodes.
    click.echo('creating Region Adjacency Graph')
    rag = graph.rag_boundary(labels, edges)

    # calculate pixel counts for each node
    lab, counts = np.unique(labels.ravel(), return_counts=True)
    click.echo('starting with {} segments'.format(lab.max()))
    counts = (counts / counts.max()) * size_pen
    for i, n in enumerate(lab):
        rag.node[n].update({'pixels': counts[i]})

    # update initial edge weights to weight by mean size of nodes
    for n1, n2 in rag.edges_iter():
        total_pix = rag.node[n1]['pixels'] + rag.node[n2]['pixels']
        rag.edge[n1][n2]['weight'] += total_pix
        rag.edge[n2][n1]['weight'] += total_pix

    # calculate a value from the threshold percentile of edge weights.
    edge_weights = [x[2]['weight'] for x in rag.edges(data=True)]
    t = np.percentile(edge_weights, threshold)

    # merge adjacent labels iteratively if their edge weight is below the
    # required threshold.
    click.echo('merging segments with edge weights below {} percentile'.format(
            threshold))
    refined_labels = graph.merge_hierarchical(
            labels, rag, t, rag_copy=True, in_place_merge=True,
            merge_func=merge_nodes, weight_func=update_edge_weights)
    refined_labels, *_ = segmentation.relabel_sequential(refined_labels + 1,
                                                         offset=1)
    click.echo('merged into {} segments'.format(refined_labels.max()))
    return refined_labels
Example #23
0
def merge_hier_boundary(labels, image, thresh=0.03, show_rag=False):
    """
    Merges the given labels using a RAG based on boundaries.

    Parameters
    ----------
    labels: ndarray
    image: ndarray
    thresh: float
    show_rag: bool
    
    Returns
    -------
    rag: RAG
    labels: ndarray
        Merged labels.
    """
    edges = filters.sobel(color.rgb2gray(image))
    rag = graph.rag_boundary(labels, edges)
    rag_copy = False
    if show_rag:
        rag_copy = True
        fig, ax = plt.subplots(1, 2, figsize=(10, 10))

    labels = graph.merge_hierarchical(labels,
                                      rag,
                                      thresh=thresh,
                                      rag_copy=rag_copy,
                                      in_place_merge=True,
                                      merge_func=merge_boundary,
                                      weight_func=weight_boundary)
    if show_rag:
        graph.show_rag(labels, rag, image, ax=ax[0])
        ax[0].title('Initial RAG')
        graph.show_rag(labels, graph.rag_boundary(labels, edges), ax=ax[1])
        ax[1].title('Final RAG')

    return rag, labels
Example #24
0
def merge_hier_color(labels, image, thresh=0.08, show_rag=False):
    """
    Merges the given labels using a RAG based on the mean color.

    Parameters
    ----------
    labels: ndarray
    image: ndarray
    thresh: float
    show_rag: bool

    Returns
    -------
    rag: RAG
    labels: ndarray
        Merged labels.
    """
    rag = graph.rag_mean_color(image, labels)
    rag_copy = False
    if show_rag:
        rag_copy = True
        fig, ax = plt.subplots(1, 2, figsize=(10, 10))
    labels = graph.merge_hierarchical(labels,
                                      rag,
                                      thresh=thresh,
                                      rag_copy=rag_copy,
                                      in_place_merge=True,
                                      merge_func=merge_mean_color,
                                      weight_func=_weight_mean_color)
    # labels2 = graph.cut_normalized(img_slic, rag, thresh=30)
    # labels2 = graph.cut_threshold(img_slic, rag, 0.2)
    if show_rag:
        graph.show_rag(labels, rag, image, ax=ax[0])
        ax[0].title('Initial RAG')
        graph.show_rag(labels, graph.rag_mean_color(image, labels), ax=ax[1])
        ax[1].title('Final RAG')

    return rag, labels
def rag_merging_segmentation(image):
    # input - PIL Image
    img = np.array(image)
    labels = segmentation.slic(img, compactness=30, n_segments=350)
    g = graph.rag_mean_color(img, labels)

    labels2 = graph.merge_hierarchical(labels,
                                       g,
                                       thresh=45,
                                       rag_copy=False,
                                       in_place_merge=True,
                                       merge_func=merge_mean_color,
                                       weight_func=_weight_mean_color)

    # for x in np.unique(labels2):
    #     vol = sum(sum(labels2 == x))
    #     if vol > 1200 or vol < 100:
    #         labels2[labels2 == x] = 0

    return np.array(drop_invalid_objects(labels2,
                                         lower_threshold=100,
                                         upper_threshold=1200),
                    dtype='int16')
Example #26
0
def merge_RAG_hierarchical(input_img, input_labels, threshold):

    # create Region Adjacency Graph with rag mean color function
    g = graph.rag_mean_color(input_img, input_labels)

    labels2 = graph.merge_hierarchical(input_labels,
                                       g,
                                       thresh=threshold,
                                       rag_copy=False,
                                       in_place_merge=True,
                                       merge_func=merge_mean_color,
                                       weight_func=_weight_mean_color)
    print("labels2 complete")
    # This print statement will print out the array of merged segments
    # print(labels2[0])

    #     out = color.label2rgb(labels2, input_img, kind='avg')
    #     out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
    #     fig = plt.figure(figsize=(12,12))
    #     plt.imshow(out)
    #     plt.show()

    return labels2
Example #27
0
def graph_seg(img, max_dist=200, thresh=80, sigma=255.0):
    """
    segment an image using quickshift and merge_hierarchical
    from scikit-image. In principle, any segmentation method
    can be used, this is just one example.
    """
    img = img.astype("float")
    seg = segmentation.quickshift(
        img,
        max_dist=max_dist,
    )
    g = graph.rag_mean_color(
        img,
        seg,
        sigma=sigma,
    )
    seg = graph.merge_hierarchical(seg,
                                   g,
                                   thresh=thresh,
                                   rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=_merge_mean_color,
                                   weight_func=_weight_mean_color)
    return seg
 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)
Example #29
0
def merge_hierarchical_mean_color(labels, rag, thresh, rag_copy=True,
                                  in_place_merge=False):
    return graph.merge_hierarchical(labels, rag, thresh, rag_copy,
                                    in_place_merge, _pre_merge_mean_color,
                                    _weight_mean_color)
Example #30
0
    This method computes the mean color of `dst`.

    Parameters
    ----------
    graph : RAG
        The graph under consideration.
    src, dst : int
        The vertices in `graph` to be merged.
    """
    graph.node[dst]['total color'] += graph.node[src]['total color']
    graph.node[dst]['pixel count'] += graph.node[src]['pixel count']
    graph.node[dst]['mean color'] = (graph.node[dst]['total color'] /
                                     graph.node[dst]['pixel count'])


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

labels2 = graph.merge_hierarchical(labels, g, thresh=35, rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=merge_mean_color,
                                   weight_func=_weight_mean_color)

g2 = graph.rag_mean_color(img, labels2)

out = color.label2rgb(labels2, img, kind='avg')
out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
io.imshow(out)
io.show()
Example #31
0
    This method computes the mean color of `dst`.

    Parameters
    ----------
    graph : RAG
        The graph under consideration.
    src, dst : int
        The vertices in `graph` to be merged.
    """
    graph.nodes[dst]['total color'] += graph.nodes[src]['total color']
    graph.nodes[dst]['pixel count'] += graph.nodes[src]['pixel count']
    graph.nodes[dst]['mean color'] = (graph.nodes[dst]['total color'] /
                                      graph.nodes[dst]['pixel count'])


img = io.imread("images/sample.jpg")
labels = segmentation.slic(img, compactness=30, n_segments=400, start_label=1)
g = graph.rag_mean_color(img, labels)

labels2 = graph.merge_hierarchical(labels,
                                   g,
                                   thresh=35,
                                   rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=merge_mean_color,
                                   weight_func=_weight_mean_color)

out = color.label2rgb(labels2, img, kind='avg', bg_label=0)
out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
io.imshow(out)
io.show()
def merge_boundary(graph, src, dst):
    """Call back called before merging 2 nodes.

    In this case we don't need to do any computation here.
    """
    pass

img = data.coffee()
edges = filters.sobel(color.rgb2gray(img))
labels = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_boundary(labels, edges)

graph.show_rag(labels, g, img)
plt.title('Initial RAG')

labels2 = graph.merge_hierarchical(labels, g, thresh=0.08, rag_copy=False,
                                   in_place_merge=True,
                                   merge_func=merge_boundary,
                                   weight_func=weight_boundary)

graph.show_rag(labels, g, img)
plt.title('RAG after hierarchical merging')

plt.figure()
out = color.label2rgb(labels2, img, kind='avg')
plt.imshow(out)
plt.title('Final segmentation')

plt.show()
Example #33
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)
    def _merge_labels(self, source_image, labels, **kwargs):
        def weight_boundary(graph, src, dst, n):
            """
            Handle merging of nodes of a region boundary region adjacency graph.

            This function computes the `"weight"` and the count `"count"`
            attributes of the edge between `n` and the node formed after
            merging `src` and `dst`.


            Parameters
            ----------
            graph : RAG
                The graph under consideration.
            src, dst : int
                The vertices in `graph` to be merged.
            n : int
                A neighbor of `src` or `dst` or both.

            Returns
            -------
            data : dict
                A dictionary with the "weight" and "count" attributes to be
                assigned for the merged node.

            """
            default = {"weight": 0.0, "count": 0}

            count_src = graph[src].get(n, default).get("count", 0)
            count_dst = graph[dst].get(n, default).get("count", 0)

            weight_src = graph[src].get(n, default).get("weight", 0)
            weight_dst = graph[dst].get(n, default).get("weight", 0)

            count = count_src + count_dst
            return {
                "count":
                count,
                "weight":
                (count_src * weight_src + count_dst * weight_dst) / count,
            }

        def merge_boundary(graph, src, dst):
            """Call back called before merging 2 nodes.

            In this case we don't need to do any computation here.
            """
            pass

        threshold = self.get_value_of("hierarchy_threshold", 35)
        res = None
        try:
            g = graph.rag_mean_color(source_image, labels)
            merged_labels = graph.merge_hierarchical(
                labels,
                g,
                thresh=threshold,
                rag_copy=False,
                in_place_merge=True,
                merge_func=merge_boundary,
                weight_func=weight_boundary,
            )

            merged_labels[merged_labels == -1] = 0
            labels2 = ((merged_labels - merged_labels.min()) /
                       (merged_labels.max() - merged_labels.min()) *
                       255).astype(np.uint8)
            res = cv2.applyColorMap(255 - labels2, DEFAULT_COLOR_MAP)
            self._wrapper.store_image(res, f"rag_vis", text_overlay=True)
            self.print_segmentation_labels(res, labels2, dbg_suffix="rag")

        except Exception as e:
            logger.exception(f'FAIL label merging, exception: "{repr(e)}"')
        finally:
            return res
Example #35
0
mag = ndi.generic_gradient_magnitude(prueba1, ndi.sobel,
                                     float)  # Float for rag_boundary
#mag *= 255.0 / np.max(mag)  # normalize (Q&D)
ragb = graph.rag_boundary(superp, mag, connectivity=1)
# CLUST "CORTAR"
umbralc = 1000
superp_co = graph.cut_threshold(superp, ragb, umbralc)
print('Intermediate Number of SV: ', len(np.unique(superp_co)))
superp_co += 1
# CLUST "UNIR"
umbralb = 3500
ragb2 = graph.rag_boundary(superp_co, mag, connectivity=1)
superp_un = graph.merge_hierarchical(superp_co,
                                     ragb2,
                                     umbralb,
                                     in_place_merge=True,
                                     rag_copy=False,
                                     merge_func=ji.merge_boundary,
                                     weight_func=ji.weight_boundary)
superp_un += 1
directorio = Path(
    "C:/Users/Juan Ignacio/Documents/Movistar Cloud/TFM/Prueba_Feima_multiframe/"
)
print('Final Number of SV: ', len(np.unique(superp_un)))
print('Parametros:', '\n', '   Num. Segmentos:', numSegments, '\n',
      '   umbral corte:  ', umbralc, '\n', '   umbral union:  ', umbralb)

#nom1 = "ragb_co.tif"
ruta_co = directorio

ji.save_imagen(prueba1, superp_un, ruta_co)