How to count objects in image using python?

后端 未结 3 2097
無奈伤痛
無奈伤痛 2020-12-06 03:12

I am trying to count the number of drops in this image and the coverage percentage of the area covered by those drops. I tried to convert this image into black and white, bu

3条回答
  •  心在旅途
    2020-12-06 03:33

    You can fill the holes of your binary image using scipy.ndimage.binary_fill_holes. I also recommend using an automatic thresholding method such as Otsu's (avaible in scikit-image).

    from skimage import io, filters
    from scipy import ndimage
    import matplotlib.pyplot as plt
    
    im = io.imread('ba3g0.jpg', as_grey=True)
    val = filters.threshold_otsu(im)
    drops = ndimage.binary_fill_holes(im < val)
    plt.imshow(drops, cmap='gray')
    plt.show()
    

    For the number of drops you can use another function of scikit-image

    from skimage import measure
    labels = measure.label(drops)
    print(labels.max())
    

    And for the coverage

    print('coverage is %f' %(drops.mean()))
    

提交回复
热议问题