Remove spurious small islands of noise in an image - Python OpenCV

后端 未结 2 1418
执念已碎
执念已碎 2020-12-07 10:22

I am trying to get rid of background noise from some of my images. This is the unfiltered image.

\"\"

T

2条回答
  •  盖世英雄少女心
    2020-12-07 10:48

    One can also remove small pixel clusters using the remove_small_objects function in skimage:

    import matplotlib.pyplot as plt
    from skimage import morphology
    import numpy as np
    import skimage
    
    # read the image, grayscale it, binarize it, then remove small pixel clusters
    im = plt.imread('spots.png')
    grayscale = skimage.color.rgb2gray(im)
    binarized = np.where(grayscale>0.1, 1, 0)
    processed = morphology.remove_small_objects(binarized.astype(bool), min_size=2, connectivity=2).astype(int)
    
    # black out pixels
    mask_x, mask_y = np.where(processed == 0)
    im[mask_x, mask_y, :3] = 0
    
    # plot the result
    plt.figure(figsize=(10,10))
    plt.imshow(im)
    

    This displays:

    To retain only larger clusters, try increasing min_size (smallest size of retained clusters) and decreasing connectivity (size of pixel neighborhood when forming clusters). Using just those two parameters, one can retain only pixel clusters of an appropriate size.

提交回复
热议问题