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.

createProject

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{
public:
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;

};

#endif

Now in testApp.cpp following code will be written :

#include “testApp.h”

int circleX, circleY,i, red, green, blue;
//————————————————————–
void testApp::setup(){
ofSetFrameRate(100);
ofBackground(150,50,10);
w = 640;
h = 480;
movie.initGrabber(w, h, true);
//reserve memory for cv images
rgb.allocate(w, h);
filtered.allocate(w, h);
red.allocate(w,h);
green.allocate(w,h);
blue.allocate(w,h);
}

//————————————————————–
void testApp::update(){

movie.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;
}

filtered.flagImageChanged();
//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(){
ofSetColor(255,255,255);

//draw all cv images
red.draw(0,0);
filtered.draw(0,480);
contours.draw(0,480);

ofSetColor(255, 255, 0);
ofFill();

//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

Control your mouse cursor by WIIMOTE

Since the Wii Remote uses bluetooth to communicate with the console, it’s relatively straightforward to use it to control a computer. The most obvious way is similar to the main menu on the Wii console – point the remote where you want the cursor to go, and press A and B to left and right click. There are also options for tilting the Wiimote to move the cursor around, which is useful if you don’t have a sensor bar available.

You can Do all this stuff by using a tool WMGUI which is a utility of CWiiD.

To install and make this work follow the instructions :-

sudo apt-get install libcwiid1

sudo apt-get install lswm


sudo apt-get install wmgui

sudo apt-get install wminput

Now load wmgui in terminal

Now click on Connect button in FILE

Select “connect” from the file menu, press 1+2 when prompted then OK. Lights and rumble can be turned on and off from the controls menu, and which inputs are displayed from the settings menu. Using this, you can test the IR camera, the accelerometers and check the inputs from the Nunchuck or Classic Controller.

Next up is the mouse “emulator” which allows you to control the mouse with the Wiimote. At this point, it can be run with default settings (tilting the Wiimote moves the pointer, A and B work as left and right click).

The uinput module needs to be loaded into the kernal first. This can either be done manually after every reboot:

lswm

This should return the address of the Wiimote (something like 00:1F:32:95:EF:B0). This will be different for each Wiimote. If it doesn’t work, try again, ensuring the lights on the Wiimote are still flashing when you run lswm.

Moving the mouse by tilting the Wiimote

Press the 1+2 buttons on the Wiimote and run:

sudo wminput 00:1F:32:95:EF:B0

replacing 00:1F:32:95:EF:B0 with the address of your Wiimote. This can be run without the address, but will take longer to connect.

Press 1+2 on the Wiimote and from a terminal run this:

wminput -c ir_ptr 00:1F:32:95:EF:B0

Running wminput as user (not root)

Normally wminput has to be run as root as the uinput device requires root priviliges. To allow any user to access it add a rule to /etc/udev/rules.d to change the mode of the device to “0600”, e.g

 sudo sh -c 'echo KERNEL\=\=\"uinput\", MODE\=\"0666\" > /etc/udev/rules.d/71-wiiremote.rules'

 

then reboot or restart the udev service

 

sudo service udev restart 

 

WMGUI as daemon

wminput -d 00:1F:32:95:EF:B0

Will run wminput in daemon mode – quiet output, waits indefinitely and continually retries to reconnect. Press 1+2 on the remote to connect, the remote powerbutton to disconnect.

nohup wminput -d 00:1F:32:95:EF:B0&

Will launch it quietly in the background.

 

By this you can easily Control your mouse cursor using WiiMote. Plus you can find accelerometer’s X-, Y- & Z- axes

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.

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);
size(400,400);
background(0);
frameRate(15);
}
void draw(){
color c = color(200,0,0);
if(mousePressed == true){
fill(c, 150);
if(mouseX % 2 == 0){
fill(255,255,0);
ellipse(mouseX,mouseY,25,25);
arduino.digitalWrite(ledPin1, Arduino.HIGH);
delay(20000);}
arduino.digitalWrite(ledPin1,Arduino.LOW);
if(mouseX % 2 == 1){
ellipse(mouseX,mouseY,40,40);
arduino.digitalWrite(ledPin2, Arduino.HIGH);
delay(10000);}
arduino.digitalWrite(ledPin2, Arduino.LOW);
}
else{
fill(0);}
}

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.

12 reasons to Use UBUNTU 10.10 Maverick Meerkat

As Ubuntu 10.10, or “Maverick Meerkat,” hits the streets this Sunday, it’s a pretty safe bet that legions of existing Ubuntu users will be updating to the new release. After all, it looks to be Canonical’s most user-friendly Ubuntu Linux yet, and many of the new features promise to be must-haves.

1. Speed

Ubuntu 10.10 is fast — darn fast. Even the beta version could boot in as little as 7 seconds, according to reports. Who has time to wait around for Windows when there’s work to be done?

2. Price

There’s no contest on this one, because Ubuntu is free. Pure and simple. No investment whatsoever, unless you want to buy professional support later on.

3. No Commitment

You can try out Ubuntu without changing or affecting anything else on your computer through options like a LiveCD, Live USB, Wubi or virtualization–all of which I’ve already described elsewhere. In other words, you have nothing to lose.

4. Hardware Compatibility

Ubuntu will play well on just about any machine you might have sitting around, so you could also try it out on a spare one to keep it off your Windows machines altogether–until you decide you can’t go back, that is.

5. Ubuntu One

Ubuntu One is the personal cloud service that lets you synchronize your files and notes and then access them from anywhere. You can also consolidate your computer and mobile phone contacts and share documents and pictures with them. On the fun side, you can use Ubuntu One to buy music and get it delivered to the computers of your choice.

6. Windows Compatibility

With Ubuntu 10.10, a beta client for Windows also allows users to integrate their Windows and Ubuntu worlds by accessing files from either platform. You’ll never have to worry about being unable to get at your Windows files.

7. Applications

Unlike Windows, Ubuntu comes with key business productivity software for free, including OpenOffice.org. Firefox is included, but there’s also support for both Flash and Google Chrome. Anything that’s not there already, meanwhile, can be found in Ubuntu’s Software Center. Whereas finding new software on Windows is very much a hunt-and-peck process, with lots of time spent on Google–and your credit card–the Software Center gives you a central place to find and download thousands of open source applications–for free–in a matter of seconds.

8. Security

Ubuntu–and pretty much every distribution of Linux–is extremely secure, particularly compared with Mac OS X and Windows. No wonder experts have recommended using Linux for online banking, in particular–the others just aren’t secure enough.

9. Multitouch

If you try the Netbook Edition of Ubuntu on a supported netbook, you’ll be able to see for yourself the brand-new multitouch features in Maverick’s new Unity interface.

10. Beauty

One key emphasis in the new Ubuntu is making it more beautiful and aesthetically pleasing to use. The Unity interface is part of that in the Netbook Edition, and the Ubuntu Font Family is another part. It’s all just nice to look at.

11. It’s Sociable

Ubuntu’s new “Me Menu” lets you access your Facebook and Twitter accounts straight from the desktop. You can connect to all your favorite chat channels and make updates through a single window.

12. It’s Linux

There are so many reasons for businesses to use Linux today, it’s hard to keep track of them all. Security is one, of course, but there are also many other reasons Ubuntu, in particular, has become such a good business choice–far better than Mac OS X or Windows.

Ubuntu 10.10 will be available for download starting on Sunday from Canonical’s Ubuntu site. Of course, if you can’t wait until then, there’s always the Release Candidate, which is ripe for the picking right now. Either way, my bet is that once you try Ubuntu for your business, you’re going to want to keep it.

(Copied from http://www.pcworld.com/businesscenter/article/207283 12_reasons_to_try_ubuntu_1010_now.html ) .

Ubuntu 10.10 : Wi-Fi reloaded

Wifi problem solved when I tried to install Broadcom’s B43 drivers for my Dell Inspiron 1525. On its automatic Installation for additional drivers I was getting error

SystemError: installarchives() failed

So I got a link http://ubuntuforums.org/showthread.php?t=1593717
Steps I followed are :-

sudo apt-get –purge remove firmware-b43-installer
sudo apt-get –purge remove dkms
sudo apt-get –purge remove bcmwl-kernel-source
sudo apt-get install bcmwl-kernel-source
sudo reboot

This make my laptop’s Wi-fi in working state .

My minor project : Gesto-paint

Hey guys,

blogging after a long time. This time I am blogging about my minor project on my major interest HCI. I have titled it as Gesto-paint. As title itself suggests it is basically a controlling mouse using hand gestures. Paint is just as an application. This project is inspired from Jacob Andreas’s Red Glove project . Libraries and softwares, I am using are openCV, Octave and few more which will be updated later.

Controlling Fading (brightness) of LED from Potentiometer Using ARDUINO

Posting after so long, Obviously expecting a good stuff from my lab. :), but again I am posting my one more small though important experiment. Its about Controlling Intensity of a LED by varying Potentiometer (as input) using arduino board. Here is the program:

int potIn = 0;                                        // Potentiometer analog input at analog pin 0
int ledPin = 12;                                    // LED at digital Pin 12
int potVal = 0;                                     // Initializing Potentiometer’s input as zero

void setup() {
pinMode(12,OUTPUT);                  // Setting pin 12 as OUTPUT

}

void loop() {
potVal = analogRead(potIn);         // analog value read at analog pin 0 assigning to potVal
for (int brightness = potVal; brightness <= 200; brightness+=5) {             // for loop for adjusting brightness
analogWrite(ledPin, brightness);                                                                         // value of brightness is analog write to pin 12

}

}

Setting whole setup:

Potentiometer had 3 legs out of which middle one is input pin connected to pin 0, whereas pin at one end must be grounded and other end pin must be +5V.

A resistor of 220 ohm between pin 12 and LED must be connected.

LED’s positive leg to output pin 12 and negative leg must be grounded.

Inform me if anyone finds its better and shorter way. see ya guys.

Lets start with Wiimote

Wiimote is a Bluetooth enabled remote control for Wii gaming from Nintendo factory. These days it is becoming centre of attraction for hackers. Due to its various features like Bluetooth connectivity, accelerometers etc . I Purchased a wiimote set after struggling in Lajpat Rai Market, Chandni Chowk. Now I started working on and understanding its features. I tried to Hack it and started by downloading libraries (you can say drivers) for wiimote from internet on my ubu-antriksh ( i used to call my laptop by this when ubuntu OS is running lol).

My First Experiment is basically not only mine its been done already by lot of people but was first for me. I tried to make wiimote work as mouse on ubu-antriksh. I google it, and finally found some blogs and forum which could help me. Now explaining Stepwise :

install drivers for wiimote. i.e libwiimote using

wget http://downloads.sourceforge.net/libwiimote/libwiimote-0.4.tgz?modtime=1173542681&big_mirror=0

then Install wmgui, its a gui wiimote emulator using

” $ sudo apt-get install wmgui”

install lswm

“$ sudo apt-get install lswm”

install wminput
“$ sudo apt-get install wminstall”

now using command lswm , we get the address of the wiimote connected to your PC.
“$ lswm”

wiimote as mouse

Here 00:18:00:0E:0F:00 is the address of the device connected to bluetooth device of your PC or laptop. It is different for different devices.

now use wminput 00:18:00:0E:0F:00

wiimote as mouse

The man pages for wminput does not recommend running wminput as root. So run the following command so that you can gain access to /dev/input/uinput without having to use
sudo.

$ sudo sh -c ‘echo “KERNEL==\”uinput\”, GROUP=\”admin\””> /etc/udev/rules.d/50-cwiid-input.rules’ /etc/init.d/udev restart

This would help you in the process of using Wiimote as your laptop or PC’s mouse. So HACK & ENJOY.