Skip to content

Target Tracking

Target tracking is an important application of drone intelligence. In this project, you will learn how to track a colored target in real time using the OpenCV library.

  • Using the OpenCV library
  • Color space conversion
  • Target detection algorithms
  • Real-time tracking control
ItemQuantityNotes
ESP32-S3 Drone1-
OV2640 Camera Module1-
Red Ball110 cm diameter

Unzip esp32_opencv.zip and copy the library files into the project’s components folder.

Unzip color_tracking.zip and open it with VS Code.

Step 3: Implement the Color Tracking Function

Section titled “Step 3: Implement the Color Tracking Function”

Open color_tracking.c and implement color tracking:

void color_tracking(uint8_t* frame, int width, int height, int* target_x, int* target_y) {
// 1. Convert the RGB image to HSV color space
cvt_color_rgb2hsv(frame, width, height);
// 2. Apply color threshold filtering (red)
uint8_t mask[width * height];
hsv_threshold(frame, width, height, mask, 0, 10, 100, 255, 100, 255);
// 3. Find contours and compute the target center
find_contours(mask, width, height);
if (has_contours()) {
*target_x = get_largest_contour_center_x();
*target_y = get_largest_contour_center_y();
} else {
*target_x = -1;
*target_y = -1;
}
}

Open flight_control.c and add the following to the main loop:

int target_x, target_y;
color_tracking(frame, width, height, &target_x, &target_y);
if (target_x != -1) {
// Target on the left → drone turns left
if (target_x < width / 2 - 20) {
motorsSetRatio(MOTOR_M1, current_ratio * 0.8);
motorsSetRatio(MOTOR_M2, current_ratio * 1.2);
motorsSetRatio(MOTOR_M3, current_ratio * 0.8);
motorsSetRatio(MOTOR_M4, current_ratio * 1.2);
}
// Target on the right → drone turns right
else if (target_x > width / 2 + 20) {
motorsSetRatio(MOTOR_M1, current_ratio * 1.2);
motorsSetRatio(MOTOR_M2, current_ratio * 0.8);
motorsSetRatio(MOTOR_M3, current_ratio * 1.2);
motorsSetRatio(MOTOR_M4, current_ratio * 0.8);
}
// Target in the center → drone moves forward
else {
motorsSetRatio(MOTOR_M1, current_ratio * 1.1);
motorsSetRatio(MOTOR_M2, current_ratio * 1.1);
motorsSetRatio(MOTOR_M3, current_ratio * 1.1);
motorsSetRatio(MOTOR_M4, current_ratio * 1.1);
}
}
  1. Move a red ball around and check whether the drone can follow it steadily
  2. Challenge: lead the drone all the way around a table with the red ball
  • Adjust the color threshold
  • Improve the lighting conditions
  • Tune the control parameters
  • Add a prediction algorithm

Congratulations! You have implemented a target tracking system — an important application of drone intelligence!

In the next project, you will learn how to deploy an AI model on the ESP32-S3.