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.