Python OpenCV White and Black Dot Detection Project

FREE Online Courses: Click for Success, Learn for Free - Start Now!

White and black dot detection is a common image processing task that can be accomplished using OpenCV, an open-source computer vision library.

In this project, the goal is to detect the presence and location of small white or black dots within an image. This technique has many practical applications, such as identifying defects in electronic circuits, detecting spots on medical images, or tracking particles in microscopy images.

OpenCV provides a wide range of functions and tools for image processing, including filtering, thresholding, and feature extraction, which can be used to perform dot detection with high accuracy and efficiency. In this tutorial, we will explore the basic principles and techniques of white and black dot detection using OpenCV.

Preprocessing

Preprocessing is an important step in image processing, especially for tasks such as white and black dot detection using OpenCV. In order to accurately detect these dots, the input image needs to be preprocessed to enhance its features and reduce noise. Some common preprocessing techniques include image resizing, color space conversion, filtering, and thresholding.

For white and black dot detection, a common technique is to convert the image to grayscale and apply a binary threshold to obtain a binary image with only black and white pixels. This allows for the dots to be easily detected and segmented from the background.

Additionally, morphological operations such as erosion and dilation can be applied to further enhance the segmentation of the dots. Preprocessing is essential in achieving accurate and reliable results in white and black dot detection using OpenCV.

Prerequisites for White and black dot detection using Python Opencv

It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this you should have the following system requirements.

  • Python 3.7 (64-bit) and above
  • Any python editor (VS code, Pycharm)

Download Python Opencv White and black dot detection Project

Please download the source code of Python Opencv White and black dot detection Project from the following link: Python Opencv White and Black Dot Detection Project Code.

Installation

Open windows cmd as administrator

1. To install the opencv library run the command from the cmd.

pip install opencv-python

Let’s Implement

1. We need to import some libraries that will be used in our implementation.

import cv2
import numpy as np

2. To detect black and white dots in an image you need to load the image using the cv2.imread() function. The function takes the path of the image as input, which is the location from where you want to detect the dots.

img = cv2.imread('C:/Users/yoges/OneDrive/Desktop/dataflair/White and black dot detection using opencv/im.jpg')

3. The next step is to convert the loaded image into grayscale, which is a necessary step for many image processing tasks.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

4. We are converting the grayscale image to a binary image using a thresholding operation. This operation sets the pixel values to either black or white, depending on whether they are above or below a threshold value of 127. This creates a binary image where white dots or bright features in the image are represented by white pixels, and black dots or dark features in the image are represented by black pixels.

ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

5. We’re using the HoughCircles function to detect white dots in the binary image. The function takes in several arguments, including the minimum distance between the centers of detected circles, the Canny edge detector’s threshold values, and the minimum and maximum radius of circles to detect. The function uses a Hough transform algorithm to detect circles in the binary image.

white_circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

6. We are using the HoughCircles function again from OpenCV to detect black dots in the binary image. The arguments are the same as before, except that we are now looking for black dots instead of white dots. We can reuse the same parameters because the size and shape of the dots are assumed to be similar. However, depending on the image and the lighting conditions, we may need to adjust the parameters to get accurate detection of both black and white dots.

black_circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

7. If white circles are detected in the image, we draw circles around them on the original image using the cv2.circle function. We use the center and radius values obtained from the HoughCircles function to draw the circles, and set the color to purple. The circles are filled and drawn with a thickness of 2.

if white_circles is not None:
    white_circles = np.round(white_circles[0, :]).astype("int")
    for (x, y, r) in white_circles:
        cv2.circle(img, (x, y), r, (255, 0, 255), 2)

8. If black circles are detected in the image, we draw circles around them on the original image using the cv2.circle function. We use the center and radius values obtained from the HoughCircles function to draw the circles, and set the color to purple. The circles are filled and drawn with a thickness of 2.

if black_circles is not None:
    black_circles = np.round(black_circles[0, :]).astype("int")
    for (x, y, r) in black_circles:
        cv2.circle(img, (x, y), r, (255, 0, 255), 2)

9. It displays the final image using cv2.imshow() and waits for a key press before closing the window and releasing resources using cv2.waitKey() and cv2.destroyAllWindows(). If the code is not detecting the dots properly, try adjusting the parameters of cv2.HoughCircles() and applying preprocessing steps to the image, such as smoothing or adjusting the thresholding parameters.

cv2.imshow('PythonGeeks', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Python OpenCV White And Black Dot Detection Output

opencv white and black dot detected output

Conclusion

Detecting white and black dots in an image using OpenCV can be done using the Hough Circle Transform. By applying appropriate thresholding and tuning the parameters of the HoughCircles function, it is possible to accurately detect and draw circles around the dots in the image. Preprocessing steps such as smoothing the image with a Gaussian filter or adjusting the threshold parameters can further improve the detection results.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *