Color Tracking in OpenFramework

Hey Guys,

Blog Post after a long time, did some random stuff related to electronics and design but was not able to post anything. These days started working on OpenCV , OpenFramework again. Since there are lots of DIY tutorials on Installing OpenCV and OpenFramework so not posting it. After trying Hello World in OpenFramework, Thought of merging OpenCV and OpenFramework. So here is the 1st outcome, Color Tracking in OpenFramework using OpenCV API for it.

I am working on Ubuntu 12.04, Code:Blocks & I am tracking Red Color.

Using Project Generator of OpenFramework, Created an Empty Project. And after that you will get main.cpp, testApp.cpp & testApp.h files in src folder.


Make Sure you have ofxOpenCv   and ofxCv  addon installed properly in OF folder since it will have some examples so you can check those examples too.

Now In testApp.h , following code will be written.

#ifndef _TEST_APP
#define _TEST_APP
#include “ofMain.h”
#include “ofxOpenCv.h”
#include “ofxCv.h”

class testApp : public ofBaseApp{
void setup(); //to setup a font fo this project
void update();
void draw(); //to draw the font on screen
void keyPressed(int);                         //On KEY Press Events will be defined here
void mouseMoved(int, int);            // On Mouse Move Events will be defined here
void mousePressed(int x, int y, int button); //On Mouse Press Events will be defined here
ofVideoGrabber movie;                          // movie object for capturing Video from Cam
ofxCvColorImage rgb;                           //rgb object for storing color image data
ofxCvGrayscaleImage filtered,red,green,blue;  //objects for storing grayscale image data
ofxCvContourFinder contours;                         //object to store contours of the filtered image
int w,h;



Now in testApp.cpp following code will be written :

#include “testApp.h”

int circleX, circleY,i, red, green, blue;
void testApp::setup(){
w = 640;
h = 480;
movie.initGrabber(w, h, true);
//reserve memory for cv images
rgb.allocate(w, h);
filtered.allocate(w, h);

void testApp::update(){


if (movie.isFrameNew()) {

//copy webcam pixels to rgb image
rgb.setFromPixels(movie.getPixels(), w, h);

//mirror horizontal
rgb.mirror(false, true);

//store the three channels as grayscale images

rgb.convertToGrayscalePlanarImages(red, green, blue);

green+=blue;   // adding green and blue channels then will subtract from red chanel to filter out only red
red -=green;
//filter image based on the hue value were looking for
for (int i=0; i<w*h; i++) {
filtered.getPixels()[i] = ofInRange(red.getPixels()[i],0,40) ? 0 : 255;

//run the contour finder on the filtered image to find blobs with a certain hue
contours.findContours(filtered, 50, w*h/2, 1, false);



void testApp::draw(){

//draw all cv images

ofSetColor(255, 255, 0);

//draw red circles for found blobs
for (int i=0; i<contours.nBlobs; i++) {
ofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 5);

void testApp::keyPressed(int key){
if(key == 115)
i– ;
if(key == 119)
i++ ;

void testApp::mouseMoved(int x, int y){
cout << “mouseMoved: ” << x << “, ” << y << endl;
circleX = x;
circleY = y;

void testApp::mousePressed(int x, int y, int button){
//calculate local mouse x,y in image
int mx = x % w;
int my = y % h;


Now in Last main.cpp this code will be written :

#include “testApp.h”
#include “ofAppGlutWindow.h”

int main(){
//ofAppGlutWindow window; // create a window
// set width, height, mode (OF_WINDOW or OF_FULLSCREEN)
ofSetupOpenGL(1024, 768, OF_WINDOW);
ofRunApp(new testApp()); // start the app

Thats it, Now build and Run the code and you will get red color tracked and a yellow dot over it & start Play with the code

red color detect 2 red color track1

Serial RS232 Communication for Newbies

After a long time again, While working in FARASBEE, First project I got finally compelled me to study about Serial Communication, the term I heard very long time before. 🙂 Its not that terrifying but ya concepts are bit complication ( at least were for me), In this post I am discussing about the Concept of Serial Communication , its interfacing with Computer using serial terminals and few more stuff that I got while working on this project.

Serial Communication:-

Process of sending 1 bit at a time over a communication channel is Serial Communication. RS232 is the most common and one of the oldest type of Serial Communication protocol still widely used.

RS232 Block Diagram

This is the basic block diagram for serial communication.

Line Driver ( important part) :-

RS232 communication uses voltage upto +15V and it uses inverse logic that is

high | true  1  =  negative voltage,

low | false |0 =  positive  voltage.

Now the question arises Why do we need Line Driver?

Answer is pretty clear : Since these voltages are far too high for modern computer logic, So we do have to use Line Driver. It converts computer logic to High Voltage & output voltage is typically continuous short-circuit safe.

Why does Line receiver required here?

It just inverses the operation of line driver i.e. converts incoming RS232 Signal to voltage safe for computer logic.

UART (Universal Asynchronous Receiver Transmitter) :-

  • The Universal Asynchronous Receiver/Transmitter (UART) takes bytes of data and transmits the individual bits in a sequential fashion. At the destination, a second UART re-assembles the bits into complete bytes.
  • The UART usually does not directly generate or receive the external signals used between different items of equipment. Separate interface devices are used to convert the logic level signals of the UART to and from the external signaling levels.
  • It adds necessary start/stop & Parity bits when transmitting & decode this information when recieving.
  • It operates completely on Logic Voltage.
  • UARTs are called asynchronous, because they don’t use a special clock signal to synchronize with the the remote side. Instead, they use the start/stop bits to identify the data bits in the serial stream.

 This is the theory part. Now back to some practical stuff I learned.

In a UART serial Communication, Line Driver/Receiver shifts voltage level for that TTL is normally used in this chip. Now basic pins you have to consider are Transmitter (Tx), Receiver (Rx) & Ground (GND) in that Line Driver/Receiver IC. Normally for RS232 communication Maxim MAX232 IC is used to change the level from 3.3V to your computer level which is usually at 12V.

Technique to find Tx, Rx & GND from Line Driver/Receiver IC (if you don’t know its datasheet its really hard) that I found after lot of searching is as follows:

To find out the serial console pins on the PCB, you’ll be looking for a populated or unpopulated 4-pin header, which can be far from the SoC (signals are relatively slow) and usually with tracks on the top or bottom layer of the PCB, and connected to the TX and RX.

Once found, you can easily check where is the GND, which is connected to the same ground layer than the power connector. The VCC should be fixed at 3.3V and connected to the supply layer ; the TX is also at 3.3V level, but using a multimeter as an ohm-meter, if you find an infinite resistance between the TX and VCC pins, it means they’re distinct signals (else, you’ll need to find the correct TX or VCC pin). The RX and GND are by default at 0V, so you can check them using the same technique.

If you don’t have a multimeter, a simple trick that usually works is using a speaker or a LED to determine the 3.3V signals.

Now here comes an important term BAUD RATE , Many of us gets confused with Baud Rate and Bit rate, so here is the best place to find its difference BAUD RATE Vs BIT RATE . Now Software part and its tracing will be discussed in my next post.

WOP : 3-D box rotating on its own axis using processing

As a part of project, today I tried to make a 3-D box rotating on its own axis using processing.
this is how it looked like

import processing.opengl.*;
int depth = 200;
//Cube cubes = new Cube;
Cube cubes = new Cube(150, 75, depth/2);
void setup() {


void draw() {
pointLight(51, 102, 255, 65, 60, 100);
pointLight(200, 40, 60, -65, -60, -150);

// Raise overall light in scene
ambientLight(70, 70, 10);
rotateY(frameCount * 0.01);

class Cube{
int w,h,d;

Cube(int w, int h, int d){

void drawCube(){

vertex(-w/2 , -h/2 , d);
vertex(w, -h/2 , d );
vertex(w , h , d );
vertex(-w/2 , h , d );

// Left face
vertex(-w/2 , -h/2 , -d/2 );
vertex(-w/2 , -h/2, d );
vertex(-w/2, h , d );
vertex(-w/2 , h , -d/2 );

// Right face
vertex(w , -h/2 , -d/2 );
vertex(w , -h/2 , d );
vertex(w , h , d );
vertex(w , h , -d/2 );

// Top face
vertex(-w/2 , -h/2 , -d/2 );
vertex(w , -h/2 , -d/2 );
vertex(w , -h/2 , d );
vertex(-w/2 , -h/2 , d );

// Bottom face
vertex(-w/2 , h , -d/2 );
vertex(w , h , -d/2 );
vertex(w , h , d );
vertex(-w/2 , h , d );


LED’s blinking according to coordinates of mouse

This is the experimental set-up I tried using Processing inheriting Arduino Libraries. Objective I wanted to do was to make red and blue coloured LED’s to blink according to the location of mouse’s X- coordinate is whether even or odd when clicked. i.e. When Mouse’s X-coordinate is even Red coloured LED blinks and when it is odd Blue Coloured LED will blink while clicking it including making different sized ellipse on the screen according to clicks.
hardware setting was nothing that hard, just two LEDs connected to pin 13 and pin 12 of arduino. Thats it.

Rest programming in Processing was as following:-

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin1 = 13;
int ledPin2 = 12;
void setup(){
arduino = new Arduino(this, Arduino.list()[0],57600);
arduino.pinMode(ledPin1, Arduino.OUTPUT);
arduino.pinMode(ledPin2, Arduino.OUTPUT);
void draw(){
color c = color(200,0,0);
if(mousePressed == true){
fill(c, 150);
if(mouseX % 2 == 0){
arduino.digitalWrite(ledPin1, Arduino.HIGH);
if(mouseX % 2 == 1){
arduino.digitalWrite(ledPin2, Arduino.HIGH);
arduino.digitalWrite(ledPin2, Arduino.LOW);

Processing using Arduino

Processing is an awesome library for interactive designers. I was reading stuff about it from last 1 month and after trying its example got an idea to implement after synchronising Arduino and processing and outcome was more awesome.
I synchronised arduino with processing library by following this link.