Example Image

The following image will used as an example.

Process

# OpenCV library will be needed.
import cv2
# OPTIONAL display image for Google Colaboratory ONLY
from google.colab.patches import cv2_imshow

# Load the image with opencv
new_image = cv2.imread("example.png")

# Grayscale image
gray = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)

# OPTIONAL invert image color
gray = ~gray
cv2_imshow(gray) or cv2.imshow('title',gray)

Displayed Image

# Using Adaptive Thresholding to recognize the dots and make them true black(0, 0, 0)
# OpenCV Adaptive Thresholding Documentation: https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 50)
cv2_imshow(thresh) or cv2.imshow('title',thresh)

Displayed Image

# OpenCV Contours Documentation: https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html
Contours, Hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# Draw contours over original image
Image = cv2.drawContours(new_image, Contours, -1, (0, 255, 0), 1)
cv2_imshow(Image) or cv2.imshow('title',Image)
# Contour count = number of dots
print(len(Contours))

Displayed Image

Total number of dots found: 474

Full Code

import cv2
from google.colab.patches import cv2_imshow # OPTIONAL display image for Google Colaboratory ONLY

new_image = cv2.imread("example.png")
gray = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)
gray = ~gray

cv2_imshow(gray) or cv2.imshow('title',gray) # Show image

thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 50)

cv2_imshow(thresh) or cv2.imshow('title',thresh) # Show image

Contours, Hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
Image = cv2.drawContours(new_image, Contours, -1, (0, 255, 0), 1)

cv2_imshow(Image) or cv2.imshow('title',Image) # Show image

print(len(Contours))

qwq~