White and black dot detection using OpenCV Python

OpenCV Python is a powerful image processing library that uses NumPy arrays to store image data. This allows us to efficiently detect and count objects like white and black dots in images.

The cv2.findContours() method is used for detecting objects in binary images. We'll use this method along with thresholding techniques to detect white and black dots ?

Syntax

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

Parameters:

  • image ? 8-bit single-channel binary image

  • mode ? Contour retrieval mode

  • method ? Contour approximation method

Approach

Follow these steps to detect white and black dots ?

  1. Load the image
  2. Convert to grayscale
  3. Apply median blur for noise reduction
  4. Apply appropriate thresholding
  5. Find contours
  6. Filter contours by area

Detecting White Dots

For white dots on dark backgrounds, we use THRESH_BINARY with a high threshold value ?

import cv2
import numpy as np

# Create sample image with white dots
image = np.zeros((300, 300, 3), dtype=np.uint8)
cv2.circle(image, (50, 50), 15, (255, 255, 255), -1)
cv2.circle(image, (150, 100), 20, (255, 255, 255), -1)
cv2.circle(image, (200, 200), 12, (255, 255, 255), -1)

# Apply median blur
blur = cv2.medianBlur(image, 5)

# Convert to grayscale
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

# Threshold for white dots
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Filter by area and count
min_area = 50
white_dots = []
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        white_dots.append(c)

print("White dots detected:", len(white_dots))
White dots detected: 3

Detecting Black Dots

For black dots on light backgrounds, use THRESH_BINARY_INV with a lower threshold ?

import cv2
import numpy as np

# Create sample image with black dots on white background
image = np.ones((300, 300, 3), dtype=np.uint8) * 255
cv2.circle(image, (75, 75), 18, (0, 0, 0), -1)
cv2.circle(image, (175, 125), 25, (0, 0, 0), -1)
cv2.circle(image, (225, 225), 15, (0, 0, 0), -1)

# Apply median blur
blur = cv2.medianBlur(image, 5)

# Convert to grayscale
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

# Threshold for black dots (inverted)
thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Filter by area and count
min_area = 50
black_dots = []
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        black_dots.append(c)

print("Black dots detected:", len(black_dots))
Black dots detected: 3

Using SimpleBlobDetector

OpenCV's SimpleBlobDetector provides an alternative approach for blob detection ?

import cv2
import numpy as np

# Create sample image with black dots
image = np.ones((200, 200), dtype=np.uint8) * 255
cv2.circle(image, (50, 50), 10, 0, -1)
cv2.circle(image, (120, 80), 15, 0, -1)
cv2.circle(image, (150, 150), 12, 0, -1)

# Create detector with default parameters
detector = cv2.SimpleBlobDetector_create()

# Detect keypoints
keypoints = detector.detect(image)

print("Dots detected with SimpleBlobDetector:", len(keypoints))

# Draw detected blobs
result = cv2.drawKeypoints(image, keypoints, np.array([]), (0, 0, 255), 
                          cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
Dots detected with SimpleBlobDetector: 3

Key Parameters

Method Best For Threshold Type
findContours() with THRESH_BINARY White dots on dark background High threshold (200+)
findContours() with THRESH_BINARY_INV Black dots on light background Low threshold (100-)
SimpleBlobDetector General blob detection Automatic

Conclusion

Use cv2.findContours() with appropriate thresholding for precise dot detection. SimpleBlobDetector offers automatic blob detection with less parameter tuning. Choose the method based on your specific image characteristics and accuracy requirements.

Updated on: 2026-03-27T06:51:08+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements