How to count objects in image using python?

后端 未结 3 2090
無奈伤痛
無奈伤痛 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:40

    The idea is to isolate the background form the inside of the drops that look like the background. Therefore i found the connected components for the background and the inside drops took the largest connected component and change its value to be like the foreground value which left me with an image which he inside drops as a different value than the background. Than i used this image to fill in the original threshold image. In the end using the filled image i calculated the relevant values

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    # Read image
    I = cv2.imread('drops.jpg',0);
    
    # Threshold
    IThresh = (I>=118).astype(np.uint8)*255
    
    # Remove from the image the biggest conneced componnet
    
    # Find the area of each connected component
    connectedComponentProps = cv2.connectedComponentsWithStats(IThresh, 8, cv2.CV_32S)
    
    IThreshOnlyInsideDrops = np.zeros_like(connectedComponentProps[1])
    IThreshOnlyInsideDrops = connectedComponentProps[1]
    stat = connectedComponentProps[2]
    maxArea = 0
    for label in range(connectedComponentProps[0]):
        cc = stat[label,:]
        if cc[cv2.CC_STAT_AREA] > maxArea:
            maxArea = cc[cv2.CC_STAT_AREA]
            maxIndex = label
    
    
    # Convert the background value to the foreground value
    for label in range(connectedComponentProps[0]):
        cc = stat[label,:]
        if cc[cv2.CC_STAT_AREA] == maxArea:
            IThreshOnlyInsideDrops[IThreshOnlyInsideDrops==label] = 0
        else:
            IThreshOnlyInsideDrops[IThreshOnlyInsideDrops == label] = 255
    
    # Fill in all the IThreshOnlyInsideDrops as 0 in original IThresh
    IThreshFill = IThresh
    IThreshFill[IThreshOnlyInsideDrops==255] = 0
    IThreshFill = np.logical_not(IThreshFill/255).astype(np.uint8)*255
    plt.imshow(IThreshFill)
    
    # Get numberof drops and cover precntage
    connectedComponentPropsFinal = cv2.connectedComponentsWithStats(IThreshFill, 8, cv2.CV_32S)
    NumberOfDrops = connectedComponentPropsFinal[0]
    CoverPresntage = float(np.count_nonzero(IThreshFill==0)/float(IThreshFill.size))
    
    # Print
    print "Number of drops = " + str(NumberOfDrops)
    print "Cover precntage = " + str(CoverPresntage)
    

提交回复
热议问题