OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Detecting white and black dots in an image can be achieved using OpenCV by using a combination of image thresholding, contour detection, and filtering based on area. In this tutorial, we'll cover how to detect both white and black dots in a grayscale image.
First, make sure you've installed OpenCV:
pip install opencv-python
Then, import the required libraries:
import cv2 import numpy as np
Load the image in grayscale format:
image = cv2.imread('image_path.jpg', cv2.IMREAD_GRAYSCALE)
Define a function that detects dots:
def detect_dots(image, color='white', min_area=5, max_area=100): if color == 'white': _, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) else: _, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) filtered_contours = [cnt for cnt in contours if min_area < cv2.contourArea(cnt) < max_area] return filtered_contours
This function works by thresholding the image to separate the dots from the background. It then uses contour detection to find the dots, and filters the detected contours based on area to refine the detection.
Detect white and black dots and draw them on the image:
white_dots = detect_dots(image, color='white') black_dots = detect_dots(image, color='black') output = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) # Convert grayscale to BGR for colored annotations for cnt in white_dots: cv2.drawContours(output, [cnt], -1, (0, 255, 0), 2) # Draw white dots in green for cnt in black_dots: cv2.drawContours(output, [cnt], -1, (0, 0, 255), 2) # Draw black dots in red
cv2.imshow('Detected Dots', output) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 import numpy as np def detect_dots(image, color='white', min_area=5, max_area=100): if color == 'white': _, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) else: _, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) filtered_contours = [cnt for cnt in contours if min_area < cv2.contourArea(cnt) < max_area] return filtered_contours image = cv2.imread('image_path.jpg', cv2.IMREAD_GRAYSCALE) white_dots = detect_dots(image, color='white') black_dots = detect_dots(image, color='black') output = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) for cnt in white_dots: cv2.drawContours(output, [cnt], -1, (0, 255, 0), 2) for cnt in black_dots: cv2.drawContours(output, [cnt], -1, (0, 0, 255), 2) cv2.imshow('Detected Dots', output) cv2.waitKey(0) cv2.destroyAllWindows()
Replace 'image_path.jpg'
with the path to your image. This code will detect white and black dots and highlight them in green and red respectively on the image. Adjust the min_area
and max_area
in the detect_dots
function based on the size of the dots you want to detect.
White and black dot detection with OpenCV in Python: Detecting white and black dots involves thresholding and contour detection to identify the circular shapes in an image.
import cv2 import numpy as np # Read the image image = cv2.imread('dots_image.jpg') # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Thresholding to separate dots from the background _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) # Find contours of the dots contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Draw circles around detected dots for contour in contours: if cv2.contourArea(contour) > 10: # Filter out small contours (x, y), radius = cv2.minEnclosingCircle(contour) center = (int(x), int(y)) radius = int(radius) cv2.circle(image, center, radius, (0, 255, 0), 2) # Display the result cv2.imshow('Dot Detection', image) cv2.waitKey(0) cv2.destroyAllWindows()