1

Is there any advance technique or a library that can post-process the images for segmentation to give smooth boundaries on segmented part using a mask?

This website here provides very good boundaries

Here is the code which I have adapted the combined_display method in this notebook from the ModNet Paper which is as follows:

def overlay_mask(image:np.ndarray, mask:np.ndarray, return_comparison: bool = False)-> np.ndarray:
  '''
  Overlay Mask over Image. Smoothes the image boundry
  image: RGB Numpy array
  mask: Binary B/W mask
  return_comparison: whether to return side by side images for original and new
  '''

  # obtain predicted foreground
  if len(image.shape) == 2: # If grayscale, add empty dimension
    image = image[:, :, None]
    
  if image.shape[2] == 1: # If grayscale with empty dimension, repeat to give the image a RGB look
    image = np.repeat(image, 3, axis=2)
    
  elif image.shape[2] == 4: # if RGBA, Pick first 3 Dimensions
    image = image[:, :, 0:3]
    
  # mask = np.repeat(mask[:, :, None], 3, axis=2) / 255
  foreground = image * mask + np.full(image.shape, 255) * (1 - mask) # From the paper ModNet, Colab Demo
  
  if return_comparison: # combine image, foreground, and alpha into one line
    combined = np.concatenate((image, foreground), axis=1)
    return Image.fromarray(np.uint8(combined))

  return foreground.astype(np.uint8)


def superimpose_background(foreground, background, alpha, overlay:bool = False):
    '''
    Change the background of any image given it's binary mask
    args:
        foreground: RGB image which has to be superimposed
        backgroud: Background of same shape
        alpha: B&W Mask image 
        overlay: Overlay background first
    '''
    if overlay:
        foreground = overlay_mask(foreground, alpha)
          
    foreground = (alpha * foreground) 
    
    background = (1.0 - alpha) * background 
    return (foreground + background).astype(np.uint8)

Below are my Original Image as Mask ans as you can see, there's blurriness in the image final segmented image.

enter image description here enter image description here enter image description here

Deshwal
  • 3,436
  • 4
  • 35
  • 94
  • Hope you get some idea from this: https://stackoverflow.com/questions/63001988/how-to-remove-background-of-images-in-python/63003020#63003020 – Jeru Luke May 20 '22 at 16:39
  • @JeruLuke Morphology followed by Blur and stretch helped to some extent. Thank you. – Deshwal May 21 '22 at 07:46
  • 1
    In the answer linked above the author mentions morphological operations as an option. – Jeru Luke May 26 '22 at 17:53

1 Answers1

0

If mask is numpy array, you can set the mask threshold for removing low-confidence pixels like:

mask[mask<= 0.1] = 0

You can choose the threshold by yourself, based on your preferences and on your target data. But in this case line has no effect:

image * mask + np.full(image.shape, 255) * (1 - mask)

just try to use:

image * mask

If it doesn't work, for better visual effect you can just blur your mask with Gaussian/Bilateral/averaging filters

Maxim Lyuzin
  • 377
  • 3
  • 8