0

I have image segmentation results that look like this:

enter image description here

As you can see there are small gaps in the segmentation map. These gap pixels have been assigned the value 0; all other, non-gap pixels have been assigned a non-0 class value.

Is there a method, perhaps somewhere in skimage, which can perform something like k-nearest interpolation on just the null pixels, in order to assign them a value coherent with their neighborhood? I tried writing this function myself, but it too slow for my tastes.

Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • Can you please provide the image itself rather than a screencapture of a plot of the image which is kind of like a photocopy of a photocopy.... – Mark Setchell Dec 04 '19 at 23:17
  • 1
    You can do connected components processing in Python Wand (which uses Imagemagick) to merge small regions into their surrounding regions. See area-threshold under connected components at http://docs.wand-py.org/en/0.5.7/wand/image.html – fmw42 Dec 04 '19 at 23:17
  • Apologies. I started using JupyterLab recently, and their way of downloading output images (https://github.com/jupyterlab/jupyterlab/issues/2685) is rather nonintuitive. – Aleksey Bilogur Dec 06 '19 at 18:45

2 Answers2

1

You can use opencv's morphological closing operation.(reference: Link)

I tried to perform the same operation on your image:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = plt.imread('image path')
plt.imshow(img)

orignal image

kernel = np.ones((5,5),dtype='uint8')
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel,iterations=1)
plt.imshow(closing)

output

You can play with kernel size and number of iterations. For small images kernel size of 3 or 5 would be find. You can increase the number of iterations to close bigger holes.

Digant Patel
  • 56
  • 1
  • 5
1

the morphological closing is quite good but can modify the shape of your object, it add some artefact in the edge. for your problem, i think you should use findContour and then use fillpoly to remove holes.

gpcbitnik
  • 106
  • 4