Jeru’s answer is correct for this case. If you have a case with bigger noise where morpholigocal operations won’t take them out, you can make a cutoff with the contour size, something like
for contour in contours
if cv2.contourArea(contour) > minimal_length
before counting
1
solved How to calculate the number of shapes detected after thresholding