Get rid of a large blob in a binary image but keep smaller blobs in python with PIL or skimage

1k Views Asked by At

I have two images:

enter image description here enter image description here

Exuse the different resolution but that's not the point. On the left I have a "large" blob due to a camera reflection. I want to get rid of that blob, so closing the blob. But on the right I have smaller blobs that are valuable information that I need to keep. Both of these image need to undergo the same algorithm. If I use a simple opening the smaller blobs will be gone, too. Is there an easy way to implement this in Python with skimage or/and PIL?

In a perfect world the left image should just create a white circle, where the right image should have the black dots within the white circle. It is okay to change the size of the black dots on the right image.

Here is an image which should describe the problem at the image directly enter image description here

1

There are 1 best solutions below

0
On

Ok. So before I answer. I have to tell you that this is a hackish way and has no scientific background.

from skimage import io, measure
import numpy as np
img = io.imread('img.png', as_grey=True)
img = np.invert(img>0)
labeled_img = measure.label(img)
labels = np.unique(labeled_img)
newimg = np.zeros((img.shape[0],img.shape[1]))
for label in labels:
    if np.sum(labeled_img==label) < 250:
        newimg = newimg + (labeled_img==label)
io.imshow(newimg)
io.show()

Since this is a hackish way, I know I should have commented rather answered, but I don't have enough points to comment.