Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Shape Based Object Tracking with OpenCV on Raspberry Pi
Raspberry Pi Raspberry Pi Projects

Shape Based Object Tracking with OpenCV on Raspberry Pi

Mamtaz AlamBy Mamtaz Alam4 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Shape Based Object Tracking with OpenCV on Raspberry Pi
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview:

This project is about Shape Based Object Detection & Tracking with OpenCV on Raspberry Pi 4 Computer.

In the world of computer vision, object tracking is a crucial task that involves monitoring the movement of specific objects over time using a camera. Shape-based object tracking is a technique that recognizes and tracks objects based on their geometric shapes. OpenCV, an open-source computer vision library, provides a wide range of functions that make this form of tracking both feasible and efficient.

Pairing this with the Raspberry Pi, a compact and affordable mini-computer known for its adaptability in DIY projects, creates a potent combination. By running OpenCV on Raspberry Pi, both hobbyists and professionals can craft real-time tracking systems without heavy investments in hardware. From surveillance and robotics to interactive art installations, shape-based object tracking with OpenCV on Raspberry Pi offers innovative solutions across various fields.




Components Required

We need the following components for this project. You can purchase all items from given links:

S.N.ComponentsQuantityPurchase Link
1Raspberry Pi 41Amazon | SunFounder
2Raspberry Pi Camera1Amazon | SunFounder
2SD Card 16/32 GB1Amazon | SunFounder
35V, 3A DC Adapter for RPi1Amazon | SunFounder
4LCD Display (Optional)1Amazon | SunFounder
5Mouse & Keyboard (Optional)1Amazon | SunFounder

Raspberry Pi Camera Connection

The Raspberry Pi Camera is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add video/photo capabilities to Raspberry Pi projects.

For this project, we can use a 5 mega-pixel Raspberry Pi Camera.

Raspberry Pi Camera Connection

Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.

Image

To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing sudo raspi-config in the terminal. Navigate to Interfacing Options > Camera and enable it.




Raspberry Pi Setup, Libraries & Dependencies Installation

OpenCV is required for Object Detection and Tracking, and other image processing tasks present in the code. Hence you need to install the OpenCV first. Follow the following guide to install OpenCV in your system:

How to Install & Setup OpenCV on Raspberry Pi 4

The next step is to install picamera. Therefore install it using pip.

1
2
pip3 install picamera
 

The setup part is complete now. We can move to the Shape Based Object Detection & Tracking Project with Raspberry Pi and OpenCV.


Raspberry Pi Python Code for Object Tracking with OpenCV

Now let’s develop a Python Code that helps in the Shape Based Object Tracking with OpenCV library and Raspberry Pi 4 Camera.


Python Code

Open Thonny IDE and paste the following code to the Thonny Editor. Save this file with any name such as “Object_Tracking.py” to any location.

Here is a complete Python Code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import cv2
import numpy as np
import time
 
CAMERA_DEVICE_ID = 0
IMAGE_WIDTH = 320
IMAGE_HEIGHT = 240
fps = 0
 
def set_camera_properties(cap, width, height):
    """ Set resolution properties for the camera """
    cap.set(3, width)
    cap.set(4, height)
 
def capture_frame(cap):
    """ Capture a frame from the video source """
    ret, frame = cap.read()
    if not ret:
        raise ValueError("Failed to read a frame from the camera")
    return frame
 
def detect_circles(gray_frame):
    """ Detect circles using Hough transform and return the circles found """
    return cv2.HoughCircles(gray_frame, cv2.HOUGH_GRADIENT, 1.2, 100)
 
def process_frame(frame):
    """ Blur, convert to grayscale and detect circles """
    frame = cv2.blur(frame, (3, 3))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    circles = detect_circles(gray)
    return frame, gray, circles
 
def draw_circles_on_frame(frame, circles):
    """ Draw circles and center rectangles on the given frame """
    output = frame.copy()
    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        for (x, y, r) in circles:
            cv2.circle(output, (x, y), r, (0, 255, 0), 4)
            cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    return output
 
def visualize_fps(image, fps: float) -> np.ndarray:
    """Overlay the FPS value onto the given image."""
    if len(np.shape(image)) < 3:
        text_color = (255, 255, 255)  # white
    else:
        text_color = (0, 255, 0)  # green
    
    row_size = 20  # pixels
    left_margin = 24  # pixels
    font_size = 1
    font_thickness = 1
    
    fps_text = 'FPS: {:.1f}'.format(fps)
    text_location = (left_margin, row_size)
    cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
                font_size, text_color, font_thickness)
 
    return image
 
def main():
    try:
        cap = cv2.VideoCapture(CAMERA_DEVICE_ID)
        if not cap.isOpened():
            raise ValueError("Could not open the camera")
        set_camera_properties(cap, IMAGE_WIDTH, IMAGE_HEIGHT)
 
        print("Press 'Esc' to exit...")
        
        fps = 0  # Initialize the fps variable
 
        while True:
            start_time = time.time()
            
            frame = capture_frame(cap)
            frame, _, circles = process_frame(frame)
            output = draw_circles_on_frame(frame, circles)
 
            end_time = time.time()
            seconds = end_time - start_time
            fps = 1.0 / seconds
 
            # Overlay FPS and display frames
            cv2.imshow("Frame", np.hstack([visualize_fps(frame, fps), visualize_fps(output, fps)]))
 
            if cv2.waitKey(33) == 27:  # Escape key
                break
 
    except Exception as e:
        print(e)
 
    finally:
        cv2.destroyAllWindows()
        cap.release()
 
if __name__ == "__main__":
    main()
 
 



Code Explanation

Let’s break down the code into sections and explain each:

1
2
3
4
import cv2
import numpy as np
import time
 

These lines import necessary libraries. cv2 is for OpenCV functions, numpy provides support for arrays and matrices, and time is used for timing operations.

1
2
3
4
5
CAMERA_DEVICE_ID = 0
IMAGE_WIDTH = 320
IMAGE_HEIGHT = 240
fps = 0
 

Here, constants and variables are defined. CAMERA_DEVICE_ID specifies the camera to use. IMAGE_WIDTH and IMAGE_HEIGHT set the video resolution, and fps initializes the frames per second variable.

1
2
3
4
5
def set_camera_properties(cap, width, height):
    """ Set resolution properties for the camera """
    cap.set(3, width)
    cap.set(4, height)
 

This function sets the resolution of the camera. The cap.set() method is used to modify camera properties, with 3 and 4 being the property IDs for width and height, respectively.

1
2
3
4
5
6
7
def capture_frame(cap):
    """ Capture a frame from the video source """
    ret, frame = cap.read()
    if not ret:
        raise ValueError("Failed to read a frame from the camera")
    return frame
 

This function captures a frame from the camera. If the frame isn’t captured successfully, it raises an error.

1
2
3
4
def detect_circles(gray_frame):
    """ Detect circles using Hough transform and return the circles found """
    return cv2.HoughCircles(gray_frame, cv2.HOUGH_GRADIENT, 1.2, 100)
 

This function detects circles in a grayscale frame using the Hough transform.

1
2
3
4
5
6
7
def process_frame(frame):
    """ Blur, convert to grayscale and detect circles """
    frame = cv2.blur(frame, (3, 3))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    circles = detect_circles(gray)
    return frame, gray, circles
 

This function processes the frame by blurring it, converting it to grayscale, and detecting circles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def main():
    try:
        cap = cv2.VideoCapture(CAMERA_DEVICE_ID)
        ...
        while True:
            ...
            frame = capture_frame(cap)
            frame, _, circles = process_frame(frame)
            ...
            if cv2.waitKey(33) == 27:  # Escape key
                break
    except Exception as e:
        print(e)
    finally:
        cv2.destroyAllWindows()
        cap.release()
 

The main() function is the primary execution function. It initializes the camera, sets its properties, and enters a loop to capture, process, and display frames. The loop continues until the ‘Esc’ key is pressed. If any exceptions occur, they’re printed, and the program proceeds to the cleanup phase, where all OpenCV windows are destroyed, and the camera is released.

1
2
3
if __name__ == "__main__":
    main()
 

This conditional checks if the script is being run as a standalone program (not imported as a module). If true, the main() function is executed.




Testing & Results of Shape Based Object Tracking

Now we need to run the code for Shape Based Object Tracking with OpenCV on Raspberry Pi.

After running the code, 2 windows will appear. The first window will show the image fed from Camera. The 2nd window wil show the tracked object.

Shape Based Object Tracking with OpenCV on Raspberry Pi

On the 2nd window, the round object is detected and tracked using HoughCircles().

Image

You may bring any round object in front of camera. A green circle will appear on the outer surface of the circle.


Conclusion

To sum up, the project combines OpenCV’s capabilities in shape-based object tracking with the Raspberry Pi 4’s compact and cost-effective nature. This blend allows for efficient monitoring of objects based on their geometric shapes in real-time. It’s a practical solution for various applications, from surveillance to art installations, making advanced computer vision accessible to both enthusiasts and professionals.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticlePulse Rate (BPM) Monitor using Arduino & Pulse Sensor
Next Article Measure Wind Speed & Direction with Ultrasonic Anemometer & Arduino

Related Posts

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

Updated:July 24, 2025
Interface BMI160 with Raspberry Pi Pico & MicroPython

Interface BMI160 with Raspberry Pi Pico & MicroPython

Updated:February 2, 20252K
Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Updated:February 2, 202512K
Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Updated:February 2, 20252K
Modbus RTU with Raspberry Pi Pico & Micropython

Modbus RTU with Raspberry Pi Pico & MicroPython

Updated:February 2, 20256K
Fever Detector with MLX90640 & OpenCV Raspberry Pi

Thermal Fever Detector with MLX90640 & OpenCV Raspberry Pi

Updated:February 2, 20255K
Add A Comment

CommentsCancel reply

Image
Image
Latest Posts
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

December 7, 2025
Flight Black-Box Motion Recorder using ESP32 & BMI160

Flight Black-Box Motion Recorder System using ESP32 & BMI160

December 7, 2025
Humidity & Temperature Monitoring using DHT11 & NodeMCU on ThingSpeak

ESP8266 & DHT11 Humidity Temperature Monitor on ThingSpeak

October 19, 2025
IoT based Battery SoC (%) Monitoring System with ESP32

IoT based Battery SoC (%) Monitoring System with ESP32

September 28, 2025
Image

Speed-Run Translations: Making Fast-Moving Meme Videos Accessible Worldwide

September 22, 2025
DIY ESP32 Board for Battery Powered IoT Applications

DIY ESP32 Board for Battery Powered IoT Applications

September 28, 2025
Build IoT DC Energy Meter with ESP32 Web Dashboard

Build IoT DC Energy Meter with ESP32 Web Dashboard

September 5, 2025
Image

The Future of Video Production: Adding Emotion with AI Voice Generators

August 26, 2025
Top Posts & Pages
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
  • Designing of MPPT Solar Charge Controller using Arduino
    Designing of MPPT Solar Charge Controller using Arduino
  • ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
    ECG Graph Monitoring with AD8232 ECG Sensor & Arduino
  • How to use INA226 DC Current Sensor with Arduino
    How to use INA226 DC Current Sensor with Arduino
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
Categories
  • Arduino Projects (197)
  • Articles (59)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (27)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (199)
    • ESP32 MicroPython (7)
    • ESP32 Projects (76)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (37)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (17)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.
Advertisement