How to count objects in image using python?

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

    I used the following code to detect the number of contours in the image using OpenCV and python.

    import cv2
    import numpy as np
    img = cv2.imread('ba3g0.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(gray,127,255,1)
    contours,h = cv2.findContours(thresh,1,2)
    for cnt in contours:
        cv2.drawContours(img,[cnt],0,(0,0,255),1)
    

    For further removing the contours inside another contour, you need to iterate over the entire list and compare and remove the internal contours. After that, the size of "contours" will give you the count

提交回复
热议问题