Introduction: Object Tracking Based on Shape Using OpenCV on BrainyPI
Geometric shape detection plays a crucial role in computer vision and image processing. It involves identifying and locating specific shapes within an image, enabling applications such as object recognition, robotics, and industrial automation. In this tutorial, we will focus on one of the most common geometric shapes: circles. We will explore how to detect circles in images using OpenCV's Hough Circles algorithm.
The Hough Circles algorithm leverages a variant of the Hough Transform, which is a popular technique for shape detection. It relies on edge detection and gradient information to identify circles within an image. By applying this algorithm, we can extract valuable information about circular objects, such as their positions and radii.
Through this tutorial, we will guide you step-by-step in setting up the environment, capturing or loading an image, preprocessing the image to enhance detection accuracy, and utilizing the Hough Circles algorithm to identify circles. We will also showcase how to visualize the detected circles and handle user input for a seamless user experience.
By the end of this tutorial, you will have a solid understanding of the process involved in circle detection and the ability to apply this knowledge to various computer vision tasks. So, let's dive in and explore the fascinating world of geometric shape detection using OpenCV's Hough Circles algorithm.
Supplies
Libraries: OpenCv
Hardware: BrainyPI
Step 1: Setting Up the Environment
- To begin, make sure you have Python 3 installed on your system.
- Install the necessary libraries by running the command pip install opencv-python in your terminal or command prompt.
- Import the required libraries in your Python script:
import cv2
import numpy as np
import time
Step 2: Capturing or Loading an Image
Decide whether you want to capture frames from a camera or load an existing image from disk.
- Camera Capture: Specify the camera device ID to be used (CAMERA_DEVICE_ID). Usually, the default value is 0, but if you have multiple cameras, you may need to adjust this value accordingly.
- Set the desired image resolution (IMAGE_WIDTH and IMAGE_HEIGHT). Smaller resolutions help reduce latency.
- Image Loading: If you want to use a specific image, provide the path to the image file using cv2.imread(). For example:
image_path = "path_to_image.jpg"
frame = cv2.imread(image_path)
Step 3: Preprocessing the Image and Detecting Circles
Convert the captured or loaded image to grayscale using cv2.cvtColor():
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Apply any necessary preprocessing steps to enhance the image quality or reduce noise. For example, you can use blurring to smooth the image:
blurred = cv2.blur(gray, (3, 3))
Use OpenCV's HoughCircles() function to detect circles in the preprocessed image:
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1.2, 100)
Adjust the parameters based on your specific image and requirements. The parameters include the image, method (cv2.HOUGH_GRADIENT), circle resolution (inverse ratio of the accumulator resolution), and minimum distance between the centers of detected circles.
Ensure that circles are not None before proceeding:
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(frame, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(frame, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
Step 4: Implementing on BrainyPI :
To implement the motion detection code on BrainyPI using GitLab, you can follow these steps:
a. Ensure that you have Git installed on your local machine. You can download Git from the official website and follow the installation instructions.
b. Create a Git repository on a GitLab server to host your text recognition code. You can create a new repository or use an existing one.
c. Clone the Git repository to your local machine using the following command:
git clone git@gitlab.com:your-username/your-repository.git
Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.
d. Copy the Object Tracking Python file to the local Git repository directory.
e. Commit the changes and push them to the remote GitLab repository using the following commands:
git add colour_detection.py
git commit -m "Added Object Tracking code"
git push origin main
Replace main with the branch name of your GitLab repository if it's different.
f. Open a terminal or command prompt on your device. Use the following command to establish an ssh connection with your BrainyPI device.
ssh -X pi@auth.iotiot.in -p 65530
g. Now, on your BrainyPI device, make sure you have Git installed. If not, you can install it using the package manager of your operating system.
h. Open a terminal or command prompt on your BrainyPI device.
i. Clone the Git repository from your GitLab server to your BrainyPI device using the following command:
git clone git@gitlab.com:your-username/your-repository.git
Replace your-username with your GitLab username and your-repository with the name of your GitLab repository.
j. Navigate to the cloned repository directory on your BrainyPI device.
k. Run the Object Tracking code using the following command:
python3 Object_Tracking.py
The Object Tracking program will start running, and you will see the output on the terminal of your BrainyPI device.
Step 5: Conclusion
n this tutorial, we explored the process of detecting circular shapes in images using OpenCV's Hough Circles algorithm. By following the step-by-step guide, you learned how to set up the environment, capture or load an image, preprocess the image, detect circles using the Hough Circles algorithm, and display the output.
Detecting geometric shapes, such as circles, in images is a fundamental task in computer vision and has various practical applications. It can be used for object recognition, robotics, industrial automation, and much more. OpenCV provides powerful tools and algorithms that simplify the process of shape detection.