The following image will used as an example.
_15# OpenCV library will be needed._15import cv2_15# OPTIONAL display image for Google Colaboratory ONLY_15from google.colab.patches import cv2_imshow_15_15_15# Load the image with opencv_15new_image = cv2.imread("example.png")_15_15# Grayscale image_15gray = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)_15_15# OPTIONAL invert image color_15gray = ~gray_15cv2_imshow(gray) or cv2.imshow('title',gray)
Displayed Image
_10# Using Adaptive Thresholding to recognize the dots and make them true black(0, 0, 0)_10# OpenCV Adaptive Thresholding Documentation: https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html_10thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 50)_10cv2_imshow(thresh) or cv2.imshow('title',thresh)
Displayed Image
_10# OpenCV Contours Documentation: https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html_10Contours, Hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)_10# Draw contours over original image_10Image = cv2.drawContours(new_image, Contours, -1, (0, 255, 0), 1)_10cv2_imshow(Image) or cv2.imshow('title',Image)_10# Contour count = number of dots_10print(len(Contours))
Displayed Image
Total number of dots found: 474
_19import cv2_19from google.colab.patches import cv2_imshow # OPTIONAL display image for Google Colaboratory ONLY_19_19new_image = cv2.imread("example.png")_19gray = cv2.cvtColor(new_image, cv2.COLOR_BGR2GRAY)_19gray = ~gray_19_19cv2_imshow(gray) or cv2.imshow('title',gray) # Show image_19_19thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 50)_19_19cv2_imshow(thresh) or cv2.imshow('title',thresh) # Show image_19_19Contours, Hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)_19Image = cv2.drawContours(new_image, Contours, -1, (0, 255, 0), 1)_19_19cv2_imshow(Image) or cv2.imshow('title',Image) # Show image_19_19print(len(Contours))