Python skimage.segmentation.felzenszwalb() Examples

The following are 3 code examples of skimage.segmentation.felzenszwalb(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.segmentation , or try the search function .
Example #1
Source File: felzenszwalb.py    From graph-based-image-classification with MIT License 6 votes vote down vote up
def felzenszwalb(image, scale=SCALE, sigma=SIGMA, min_size=MIN_SIZE):
    """Computes Felsenszwalb's efficient graph based image segmentation.

    Args:
        image: The image.
        scale: Float indicating largeness of clusters (optional).
        sigma: Width of Gaussian kernel used in preprocessing (optional).
        min_size: Minimum component size. Enforced using postprocessing
          (optional).

    Returns:
        Integer mask indicating segment labels.
    """

    image = tf.cast(image, tf.uint8)

    def _felzenszwalb(image):
        segmentation = skimage_felzenszwalb(image, scale, sigma, min_size)
        return segmentation.astype(np.int32)

    return tf.py_func(_felzenszwalb, [image], tf.int32, stateful=False,
                      name='felzenszwalb') 
Example #2
Source File: spfunctions.py    From spfeas with MIT License 5 votes vote down vote up
def segment_image(im, parameter_object):

    dims, rows, cols = im.shape

    image2segment = np.dstack((rescale_intensity(im[0],
                                                 in_range=(parameter_object.image_min,
                                                           parameter_object.image_max),
                                                 out_range=(0, 255)),
                               rescale_intensity(im[1],
                                                 in_range=(parameter_object.image_min,
                                                           parameter_object.image_max),
                                                 out_range=(0, 255)),
                               rescale_intensity(im[2],
                                                 in_range=(parameter_object.image_min,
                                                           parameter_object.image_max),
                                                 out_range=(0, 255))))

    felzer = felzenszwalb(np.uint8(image2segment),
                          scale=50,
                          sigma=.01,
                          min_size=5,
                          multichannel=True).reshape(rows, cols)

    props = regionprops(felzer)
    props = np.array([p.area for p in props], dtype='uint64')

    return fill_labels(np.uint64(felzer), props) 
Example #3
Source File: ace.py    From ACE with MIT License 4 votes vote down vote up
def create_patches(self, method='slic', discovery_images=None,
                     param_dict=None):
    """Creates a set of image patches using superpixel methods.

    This method takes in the concept discovery images and transforms it to a
    dataset made of the patches of those images.

    Args:
      method: The superpixel method used for creating image patches. One of
        'slic', 'watershed', 'quickshift', 'felzenszwalb'.
      discovery_images: Images used for creating patches. If None, the images in
        the target class folder are used.

      param_dict: Contains parameters of the superpixel method used in the form
                of {'param1':[a,b,...], 'param2':[z,y,x,...], ...}. For instance
                {'n_segments':[15,50,80], 'compactness':[10,10,10]} for slic
                method.
    """
    if param_dict is None:
      param_dict = {}
    dataset, image_numbers, patches = [], [], []
    if discovery_images is None:
      raw_imgs = self.load_concept_imgs(
          self.target_class, self.num_discovery_imgs)
      self.discovery_images = raw_imgs
    else:
      self.discovery_images = discovery_images
    if self.num_workers:
      pool = multiprocessing.Pool(self.num_workers)
      outputs = pool.map(
          lambda img: self._return_superpixels(img, method, param_dict),
          self.discovery_images)
      for fn, sp_outputs in enumerate(outputs):
        image_superpixels, image_patches = sp_outputs
        for superpixel, patch in zip(image_superpixels, image_patches):
          dataset.append(superpixel)
          patches.append(patch)
          image_numbers.append(fn)
    else:
      for fn, img in enumerate(self.discovery_images):
        image_superpixels, image_patches = self._return_superpixels(
            img, method, param_dict)
        for superpixel, patch in zip(image_superpixels, image_patches):
          dataset.append(superpixel)
          patches.append(patch)
          image_numbers.append(fn)
    self.dataset, self.image_numbers, self.patches =\
    np.array(dataset), np.array(image_numbers), np.array(patches)