I want to count number of dots in an image.
The image looks like

I referred to this SOF link count colored dots in image But this is for colored links , So anyone here can guide me over how to handle this and count black dots out of white back.
thredhold using THRESH_BINARY_INV flag, change to black-background-white-foreground.findContours, filter the contours by area(calculated by contourArea)import cv2
gray = cv2.imread("dots.jpg", 0)
## threshold
th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
## findcontours
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
## filter by area
s1= 3
s2 = 20
xcnts = []
for cnt in cnts:
if s1<cv2.contourArea(cnt) <s2:
xcnts.append(cnt)
print("Dots number: {}".format(len(xcnts)))
#Dots number: 23

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With