Home > Enterprise >  Remove small objects from binary image with skimage
Remove small objects from binary image with skimage

Time:07-18

I have the following binary image and I want to remove the spots with a value 0 inside the area of the pixels with value 1.

I tried following code from the skimage package:

im1 = morphology.remove_small_objects(img_test, 500, connectivity=1)

But the code does not do anything. The picture stays the same, no mater how I change the parameters. Where is my mistake?

enter image description here

CodePudding user response:

import numpy as np
import matplotlib.pyplot as plt

# image posted by OP
URL = "https://i.stack.imgur.com/Pa7Io.png"

# Read image
from skimage import io
from skimage.filters import threshold_otsu
from skimage.color import rgb2gray

image = rgb2gray(io.imread(URL)[21:899, 555:1125, :3]) #index cut the needed part

# Convert to True-False
_2color = image > threshold_otsu(image)

# accumulated true from left to right. Is true if any pixel to the left is True
trueLeft = np.logical_or.accumulate(_2color, axis=1)
# accumulated true from right to left. Is True if any pixel to the right is True
trueRight = np.logical_or.accumulate(_2color[:, ::-1], axis=1)[:, ::-1]

# True if has any true in the left and right
orImage = trueLeft * trueRight

plt.imshow(orImage)
plt.show()

enter image description here

  • Related