Python skimage.morphology.diamond() Examples

The following are 8 code examples of skimage.morphology.diamond(). 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.morphology , or try the search function .
Example #1
Source File: Util.py    From PReMVOS with MIT License 6 votes vote down vote up
def get_pos_dst_transform(label_unmodified, img, instance, old_label=None, dt_method="edt"):
  label = np.where(label_unmodified == instance, 1, 0)

  # If an old label is available, then sample positive clicks on the difference between the two.
  if old_label is not None:
    # The difference should be taken only if there is atleast one object pixel in the difference.
    label = np.max(0, label - old_label) if np.any((label - old_label) == 1) else label

  # Leave a margin around the object boundary
  img_area = morphology.binary_erosion(label, morphology.diamond(D_MARGIN))
  img_area = img_area if len(np.where(img_area == 1)[0]) > 0 else np.copy(label)

  # Set of ground truth pixels.
  O = np.where(img_area == 1)
  # Randomly sample the number of positive clicks and negative clicks to use.
  num_clicks_pos = 0 if len(O) == 0 else random.sample(list(range(1, Npos + 1)), 1)
  # num_clicks_pos = random.sample(range(1, Npos + 1), 1)
  pts = get_sampled_locations(O, img_area, num_clicks_pos)
  u1 = get_distance_transform(pts, img_area, img=img, dt_method=dt_method)

  return u1, pts 
Example #2
Source File: Util.py    From PReMVOS with MIT License 6 votes vote down vote up
def get_image_area_to_sample(img):
  """
  calculate set g_c, which has two properties
  1) They represent background pixels 
  2) They are within a certain distance to the object
  :param img: Image that represents the object instance
  """

  #TODO: In the paper 'Deep Interactive Object Selection', they calculate g_c first based on the original object instead
  # of the dilated one.

  # Dilate the object by d_margin pixels to extend the object boundary
  img_area = np.copy(img)
  img_area = morphology.binary_dilation(img_area, morphology.diamond(D_MARGIN)).astype(np.uint8)

  g_c = np.logical_not(img_area).astype(int)
  g_c[np.where(distance_transform_edt(g_c) > D)] = 0

  return g_c 
Example #3
Source File: segmentation_test.py    From DRFNS with MIT License 5 votes vote down vote up
def overlay(self, img, imbin, contour=False):
        colim = color.gray2rgb(img)
        colorvalue = (0, 100, 200)
        if contour:
            se = morphology.diamond(2)
            ero = morphology.erosion(imbin, se)
            grad = imbin - ero
            colim[grad > 0] = colorvalue
        else:
            colim[imbin>0] = colorvalue
            
        return colim 
Example #4
Source File: segmentation_test.py    From DRFNS with MIT License 5 votes vote down vote up
def morpho_rec(self, img, size=10):
        # internal gradient of the cells: 
        se = morphology.diamond(size)
        ero = morphology.erosion(img, se)
        rec = morphology.reconstruction(ero, img, method='dilation').astype(np.dtype('uint8'))
                
        return rec 
Example #5
Source File: segmentation_test.py    From DRFNS with MIT License 5 votes vote down vote up
def morpho_rec2(self, img, size=10):
        # internal gradient of the cells: 
        se = morphology.diamond(size)
        dil = morphology.dilation(img, se)
        rec = morphology.reconstruction(dil, img, method='erosion').astype(np.dtype('uint8'))
                
        return rec 
Example #6
Source File: segmentation_test.py    From DRFNS with MIT License 5 votes vote down vote up
def test_morpho2(self, bigsize=20.0, smallsize=3.0, threshold=5.0):
    
        img = self.read_H_image()

        pref = self.morpho_rec(img, 10)
        filename = os.path.join(test_out_folder, 'morpho_00_rec_%s.png' % self.image_name)
        skimage.io.imsave(filename, pref)

        res = self.difference_of_gaussian(pref, bigsize, smallsize)
        filename = os.path.join(test_out_folder, 'morpho_01_diff_%s_%i_%i.png' % (self.image_name, int(bigsize), int(smallsize)))
        skimage.io.imsave(filename, res)
        
        #res = self.morpho_rec2(diff, 15)
        #filename = os.path.join(test_out_folder, 'morpho_02_rec_%s.png' % self.image_name)
        #skimage.io.imsave(filename, res)

        res[res>threshold] = 255
        filename = os.path.join(test_out_folder, 'morpho_03_res_%s_%i.png' % (self.image_name, threshold))
        skimage.io.imsave(filename, res)
        
        se = morphology.diamond(3)
        ero = morphology.erosion(res, se)
        filename = os.path.join(test_out_folder, 'morpho_03_ero_%s_%i.png' % (self.image_name, threshold))
        skimage.io.imsave(filename, ero)
        res[ero>0] = 0
        
        overlay_img = self.overlay(img, res)
        filename = os.path.join(test_out_folder, 'morpho_04_overlay_%s_%i.png' % (self.image_name, int(threshold)))
        skimage.io.imsave(filename, overlay_img)
        
        return 
Example #7
Source File: Utils.py    From BEAL with MIT License 5 votes vote down vote up
def postprocessing(prediction, threshold=0.75, dataset='G'):
    if dataset[0] == 'D':
        prediction = prediction.numpy()
        prediction_copy = np.copy(prediction)
        disc_mask = prediction[1]
        cup_mask = prediction[0]
        disc_mask = (disc_mask > 0.5)  # return binary mask
        cup_mask = (cup_mask > 0.1)  # return binary mask
        disc_mask = disc_mask.astype(np.uint8)
        cup_mask = cup_mask.astype(np.uint8)
        for i in range(5):
            disc_mask = scipy.signal.medfilt2d(disc_mask, 7)
            cup_mask = scipy.signal.medfilt2d(cup_mask, 7)
        disc_mask = morphology.binary_erosion(disc_mask, morphology.diamond(7)).astype(np.uint8)  # return 0,1
        cup_mask = morphology.binary_erosion(cup_mask, morphology.diamond(7)).astype(np.uint8)  # return 0,1
        disc_mask = get_largest_fillhole(disc_mask).astype(np.uint8)  # return 0,1
        cup_mask = get_largest_fillhole(cup_mask).astype(np.uint8)
        prediction_copy[0] = cup_mask
        prediction_copy[1] = disc_mask
        return prediction_copy
    else:
        prediction = prediction.numpy()
        prediction = (prediction > threshold)  # return binary mask
        prediction = prediction.astype(np.uint8)
        prediction_copy = np.copy(prediction)
        disc_mask = prediction[1]
        cup_mask = prediction[0]
        for i in range(5):
            disc_mask = scipy.signal.medfilt2d(disc_mask, 7)
            cup_mask = scipy.signal.medfilt2d(cup_mask, 7)
        disc_mask = morphology.binary_erosion(disc_mask, morphology.diamond(7)).astype(np.uint8)  # return 0,1
        cup_mask = morphology.binary_erosion(cup_mask, morphology.diamond(7)).astype(np.uint8)  # return 0,1
        disc_mask = get_largest_fillhole(disc_mask).astype(np.uint8)  # return 0,1
        cup_mask = get_largest_fillhole(cup_mask).astype(np.uint8)
        prediction_copy[0] = cup_mask
        prediction_copy[1] = disc_mask
        return prediction_copy 
Example #8
Source File: __funcs__.py    From porespy with MIT License 5 votes vote down vote up
def _get_axial_shifts(ndim=2, include_diagonals=False):
    r'''
    Helper function to generate the axial shifts that will be performed on
    the image to identify bordering pixels/voxels
    '''
    if ndim == 2:
        if include_diagonals:
            neighbors = square(3)
        else:
            neighbors = diamond(1)
        neighbors[1, 1] = 0
        x, y = np.where(neighbors)
        x -= 1
        y -= 1
        return np.vstack((x, y)).T
    else:
        if include_diagonals:
            neighbors = cube(3)
        else:
            neighbors = octahedron(1)
        neighbors[1, 1, 1] = 0
        x, y, z = np.where(neighbors)
        x -= 1
        y -= 1
        z -= 1
        return np.vstack((x, y, z)).T