Introduction: Vision Based Object Tracking and Following on a DIY Drone.
Vision based object tracking and following uses the technique of visual servoing using a camera mounted on a 3-axis Gimbal. Unlike using a fixed camera, using a 3-axis gimbal adds better solution for object tracking and following as the camera can always focus on the target keeping it within the frame. This project involves the technique where the target is always kept at the centre of the frame of reference and the camera mounted on the Gimbal tracks the target with an attempt to keep it at the centre and then follows the target keeping it within its proximity.
The projects aims to encourage drone enthusiasts to build similar applications using simple algorithm and develop better applications taking the drone industry to a greater heights.
Step 1: Components Used
1. A F450 quadrotor
2. Odroid XU4 - Image processing and sending commands to Gimbal
3. Pixhawk PX4 - autopilot
4. 3-axis Gimbal (Storm32 BGC)
5. Sj4000 Camera
Step 2: Object Detection
Object detection is a the first step in this project. The main idea behind this project is that, the user has the ability to select the object of interest of his choice. This is done by Opencv methods such as 'mouse events', where the user draws a bounding box around the target and based on visual features like color and shape, the target is selected.
The following is the algorithm to implement it.
1. Take the video feed from your camera
2. Create a mouse callback function based on mouse click events like left mouse button down, mouse drag and left mouse button up.
3. store these coordinates in variables and create a rectangular box around the object you want to track.
4. extract the pixels of these coordinates and define the respective HSV values such that the target is masked.
5. Apply shape defining algorithms to the selected object, I had used a circular target in my project so I had applied Circular Hough transform (https://en.wikipedia.org/wiki/Hough_transform)
6. Keep track of only these pixel values based on your color during the entire frame capture process.
7. Using contours, calculate the centroid of the target. This centroid will the the main focus point in the entire project.
8. Calculate the distance from the current centroid (in pixel) to the centre of the frame. Do it seperatey for rows and columns. Store this error distance (in pixels ) in 2 seperate variables.
Step 3: Object Tracking Using Camera Mounted on Gimbal
Based on the error distance (in pixels ) obtained from the previous step, we command the Gimbal to move across 2 directions - pitch and yaw. But the value was obtained in pixels and need to be converted in angular radians. The following algorithm is used for the tracking part.
1. Convert the pixels value in angular radians both along x (rows) and y(columns) directions. This is your yaw and pitch value respectively.
2. Since the value still is erroneous, we give it to a PID controller to stabilize, improve the response and decrease the error in the command given to the gimbal. Kp, Ki and Kd values can be set according to your convenience.
3. You will see that when you start you execution, the Gimbal will move with respect to the target with an attempt to keep it at the centre of the frame of reference and in the video feed you will be able to see that the target remains at the centre.
If you are doing this for a fast moving target, then you need to adjust your gimbal's motor speed and also improve your PID gains.
Step 4: Following
Once the gimbal tracks the target successfully, now we want the drone to remain within the proximity of the target. This involves a bit of patience and mathematics! Based on the pitch and yaw values obtained as the output of the PID controller, we use 3D trigonometry to find the position setpoints of the target and send these sentpoints to the autopilot which will then be used to command the drone to follow the target accordingly.
You can assume that the height of the target is 0 and can calculate only the x and y coordinate of the target based on the current pitch and yaw which makes the gimbal face the target.
Step 5: Conclusion
Vision Servoing has numerous applications in the fields of entertainment, to disaster management operations like search and rescue etc. It also plays a major role in traffic management or tracking a stolen vehicle without the notice of the thief.
My application is following the target selected by the user's choice which can be used in the field of entertainment or search and rescue but mainly focuses on tracking stolen vehicles which can be useful by police department where the operator specifies the vehicle to be tracked in the presence of numerous other vehicles where OCR (optical character recognition) is added to it.
Hoping that you all build many more such useful applications thereby solving many real life problems!
Cheers!

Runner Up in the
Drones Contest 2016
34 Comments
5 years ago
plzz sir ...atleast guide me about what hardware we should use.
Reply 5 years ago
1. A F450 quadrotor
2. Odroid XU4 - Image processing and sending commands to Gimbal
3. Pixhawk PX4 - autopilot
4. 3-axis Gimbal (Storm32 BGC)
5. Sj4000 Camera
6. Connecting wires
7. GPS module
8. LiDAR
9. Barometer
Reply 6 months ago
Pixhawk ain't necessary, APM is cheaper and has the same firmware
alternately, using GPS modules, data storage units and Raspberry Pi with DRehmFlight may work out too...
2 years ago on Introduction
I own a UAV manufacturing company. May I speak with you regarding camera object tracking ? We have a standard digital camera on a a Gremsy gimbal and a pixhawk flight controller at the moment using and ethernet video transmission system and need the gimbal to track objects. This is a large fixed wing UAV operating at over 300 meters. I cpuld use your assistance if interested. Craig @ USAsianAeroservices. Com
2 years ago
Hi , very nice work. But i have seen it now on search of my requirement. I need source code of this , yu guidance in implementation in tracking algorithm with PTZ camera.
Thank you.
Question 4 years ago
Can you guys help me find the best camera drones that are capable of following movable objects (RC Car, Hotwheels cars, animals) that are big, small, or mini?
Thanks!
5 years ago
This is my final year project, I need your full guidance from hardware to software. Can you help me out?
Reply 5 years ago
Thank you for your interest.
Honestly speaking I cannot dedicate my time completely to guide you on this. However can assist you partly. You can find the code on my Github page for reference.
Reply 4 years ago
Hey, nice project! Where is the link to the github? I cant seem to find it.
Reply 4 years ago
Thanks for your interest. The code is available at:
https://github.com/shashankvkt/Object_tracking
If you find the code useful, kindly consider starring it. Also, feel free to ask me if you have any queries.
5 years ago
Thanks a lot for this tutorial !
I'm going to have a similar project, using also Xu4, Pixhawk, SJ4000 Camera.
But I'm still looking for a gimbal which can be directly connected to the Odroid Xu4, and can support the SJ4000 (according to its size and free the USB port, in order to use it like a webcam from the Odroid).
Can you explained me which gimbal are you using ? And which protocol are you using (Mavlink ?) ?
I appreciate your work, congratulation for your results !
Reply 5 years ago
Thank you for your interest!
In fact you could use any 3-axis gimbal of your choice connect it to the pixhawk and send commands to it via mavLink
Reply 5 years ago
Thank you for your quick answer, and this explaination ! :)
Do you remember which gimbal you used (or which brand) ? In order to avoid any mistake after purchasing it :p
Reply 5 years ago
It was along time back. Actually i know that you can buy any gimbal which supports Storm32 BGC which is essentially a gimbal controller board hooked up with your gimbal to send and receive signals from your pixhawk. You can google about storm32 BGC. Hope this helps!
Feel free to inquire if your doubts are still not cleared.
Reply 5 years ago
Ok thank you ! I think I'm going to buy this one, for the SJ4000 camera :
https://fr.gearbest.com/-/pp_244970.html?lkid=120...
Hope it's going to be enough !
Reply 5 years ago
Yes definitely!!
Good Luck with your project! In case of any doubts please feel free to contact.
5 years ago
Sir we are using raspberry pi 3B for image processing purpose.
Sir could be send commands from rapsberry pi to gimbal??
What is the price of Gimbal?
Reply 5 years ago
Hello!
unfortunately, Rpi3B would slow down the image processing process. Ive used Odroid XU4 with 2GB ram which actually could handle fast image processing in the drone.
Reply 5 years ago
We are actually making a drone using arduino uno board.Can we interface arduino uno board and Rpi3B combination for object tracking and following drone
Reply 5 years ago
Without pixhawk?