OpenCV Tracking Object Part II

In the First Part I created a library for tracking object, using HSV,binary and original image by applying dilate and erode filters

In this part I will continue this project by moving it to Raspberry Pi, adding code to use wiringPi and sending coordinates of the couture's center via UART to Arduino board, which will control servos, that way we will keep the tracking object within the planer center.

Boards And Software


  • Raspberry Pi - I'm using Model B, but really you can use any model (ideal will be Model B version 2 because of all the image processing we are doing)
  • Arduino Uno - that way we can control servo's
  • Motor Shield - I'm using Adafruit motor shield V2
  • SD Card Shield - Optional, but it is good for debugging


If you followed Part I, there are couple of changes in the code that you need to be aware of when moving the code to Linux flavor as well as extra addition for communicating between the Pi and Arduino.

Code Addition

In order for us to communicate between the Pi and Arduino, we use the wiring Pi framework as Serial communication. This framework makes it easy to open the serial communicator and start sending data via the Rx, Tx pins NOTE: Your Serial port on the pi is used by default, so you will have to follow part I in order to free it We achieve this by including references to the wiringPi and opening the port.

#include <wiringPi.h>
#include <wiringSerial.h>

Than, within the main function we are going to initialize the wiringPi and open the UART port.

int handle;
if((handle = serialOpen("/dev/ttyAMA0",9600))<0)
    cout<<"Couldn't open ttyAMA0 to communicate over UART"<<endl;
    return -1;
if(wiringPiSetup() == -1)
    cout<<"Couldn't Initialize Wiring Pi"<<endl;
    return -1;
// To Send the communication we use serialPuts from wiring Pi
string message = "Your Message Here";

Code Update for Linux

To use string with Linux and to convert integer values to string, you will need to change the code to use std:ostringstream value. See example below:

std:ostringstream x;
int iValue = 10;
x << iValue;
// To send the ostringstream as a string object we use the str() function
string s = x.str();

That is it, or at least in a nutshell. If you have any questions please leave me a comment on the video page