Tuesday, 1 July 2014

Colour Spaces and their conversions in opencv

In this blog we will be dealing with various color spaces and their conversions using opencv functions.

Introduction to Color spaces

According to definitions given in wikipedia the colorspace of an image is defined as: 
"Color space, also known as the color model (or color system), is an abstract mathematical model which simply describes the range of colors as tuples of numbers, typically as 3 or 4 values or color components "

For example in a RGB image every color is defined as a combination of three basic colors: red,green and blue.The color of a point changes as the value of red,green and blue component changes.(0,0,0) represents perfect black (255,255,255) represents white.One thing to be taken into account always is that in opencv the images are represented in the BGR format.So (0,0,255) represents perfect red and not blue as in a ordinary RGB image.

Another important color space used in image processing projects is HSV color space.Here the color value of each point is represented by three parameters Hue,Saturation and value.You can find more details here.Hue is the color component.In opencv it ranges from 0-180 unlike the normal image handling softwares such as Gimp or photoshop were the hsv value of hue varies from 0-360.So dont forget to divide the value by to before applying to opencv.

There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two which are most widely used ones, BGR \leftrightarrow Gray and BGR \leftrightarrow HSV.
The syntax of the function used for color space conversion is :
cv2.cvtColor(input_image, flag) where flag determines the type of conversion.
For BGR \rightarrow Gray conversion we use the flags cv2.COLOR_BGR2GRAY. Similarly for BGR \rightarrow HSV, we use the flag cv2.COLOR_BGR2HSV. 
To get other flags, just run following commands in your Python terminal :
>>> import cv2
>>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags

Example Code for BGR-> HSV conversion:

import cv2

img=cv2.imread('image.jpg')  # A BGR image is taken
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) #image is converted to hsv color space

Example Code for BGR-> GRAY conversion:

import cv2

img=cv2.imread('image.jpg')    # A BGR image is taken
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  #image is converted to gray sacle image

Similarly by changing the flags the image can be converted to various color spaces.Different color spaces have their own advantages and disadvantages.Each of them is selected according to their suitability to the application.




Friday, 4 April 2014

Madras Tiger Returns : Viswanathan Anand's Come Back

Ever since i started watching sachins play i have always heard this sentence " Form is temporary but class is permanent" . But a few days back i updated my fb post with the same sentence but this time it was meant for someone else ' Viswanathan anand ' the madras tiger , 5 time world champion .His come back in candidates tournament 2014 will be recorded as one of the greatest comebacks in the history of world sports itself.

Till few days back nobody believed he could do this.Defeated by young Magnus Carlsen in 3 matches in Chess WC 2013 anand was going through the worst times of his career.The chess legend Garry Kasparov himself asked anand to retire.Many thought he wont be playing chess again.At last even when he entered the candidates tournament he had the 2nd worst ranking among all the players participating.

Anand in press conference after winning the candidates
tournament 2014
 Vladimir Kramnik and Levon Aronian were the tournament favourites. But anand was in no mood to give up.The tournament saw anand gain back his mojo.He started the tournament with a "bang" .He defeated the world No:2 levon aronian in style.There after he won 3 matches and ended up in a draw in  all other matches.Even when his competitors lost key matches under pressure  Anand stayed cool and never let his lead drop.Through out the championship he maintained his supremacy leaving his opponents no chance to defeat him.He won the tournament in the 13th round when he had a draw with Sergey Karjakin with still one round to spare.

The press conference after the match showed how much he had recovered from the defeat in WC2013.We were able to see the young anand with a charming face and it was hard to believe he was 44 year old.Now it is time for preparations. The chess world cup 2014 is expected to take place somewhere in mid november.And this time we can surely see a new Anand.No matter what ever be the output of  that championship Anand's comeback in candidates tournament 2014 will be remembered for a very long time :-)





Current Time:

Intel Edison: Computer in size of a SD card


Once there was a time when the computers used to be massive. There were computers so huge so that it could easily fill a 2 storied building. But it was long time back. Computers started becoming smaller and smaller. And now we have the latest addition to that small family of computers the Intel “Edison”.

Edison is a computer in the size of a SD card .It is complete in itself with wifi , Bluetooth and even its own app store. It is designed as a proof-of-concept computer more portable than virtually anything previously built. Running one of Intel's new Quark processors (naturally), the device is "aimed at developers" to help them build new connected products which can talk to each other, and perform complex functions in a network.

Edison is designed to boost up the revolution of wearable computing. According to the experts the wearable the reason for the slow progress in the field was due to the lack small but powerful computing devices. But now Intel Edison available the wearable computing is expected to leap forward much faster and smarter.

The importance of Edison in wearable technology can be explained with an example. You can have a wearable gadget “a baby monitor” which can track every vital sign using the Edison, and send the data back to a base station which in turn uploads the stats to the internet.


Tail tip: Intel is now offering developers $1.3 million in prizes, including $500,000 for the first prize winner, as part of a "Make It Wearable" project. You wanna try???

Thursday, 27 February 2014

How to send emails from Matlab

Hey Everyone !

In this blog i'm going to teach you how to send emails from MATLAB.

Its very Easy..Just one command will do the trick for you..
sendmail('meetleopauly@yahoo.com', 'Gmail Test', 'This is a test message.');

The syntax is as follows :
sendmail('your address', 'title', 'your message.');

But you need to do a lil bit of preference changes before you use this command.

myaddress = 'mail address';    % give the address from which you need to send the mail from
mypassword = 'password';    % give your password

setpref('Internet','E_mail',myaddress);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',myaddress);
setpref('Internet','SMTP_Password',mypassword);

props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', ...
                  'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');

Just copy the above code.Run it and use the send mail command.

Here what we have done is we have used one of the google's silent features the  portable SMTP server.You can read more about Google's SMTP server Here .

So friends try this out...Have fun :-)



Saturday, 29 September 2012

BLINKING LED WITH ARDUINO USING MATLAB



               

Arduino is a commonly used open source single-board microcontroller.And matlab is the  language of technical computing. Here i illustrate how to control a led using matlab through arduino...

STEPS:
     1.  Connect your arduino board to the PC.
     2.   Attach an Led to the PIN 2 of the arduino. 
     3. Use the following codes in arduino and matlab.

PROGRAM CODE:

Code For arduino:

//this is a simple progam that blinks the LED in pin 2 if the transmitted  char is ‘a’ else just turns on the LED.
int led = 2;
int s;
void setup()
{   Serial.begin(9600);            
    pinMode(led, OUTPUT);    
}

void loop()
{  if(Serial.available()>0)           
   { s=Serial.read();
   }
   if(s=='a')
      {while(1)
{
     digitalWrite(led, HIGH);  
     delay(1000);
      digitalWrite(led, LOW);   
      delay(1000);}}      
  else
     {
     digitalWrite(led, HIGH);  
     delay(1000);}
        
}


Code For matlab:

 >>instrhwinfo('serial')                     //check which  port is available for serial communication
ans =
     AvailableSerialPorts: {0x1 cell}
       JarFileVersion: 'Version 2.8.0'
    ObjectConstructorName: {'serial('COM6');'}
         SerialPorts: {'COM6'}

obj=serial('com6');                       //creates a serial object
>> fopen(obj)                             //opens serial object
>> fprintf(obj,'a')                       // writes char ‘a’ to the  serial port
>> fclose(obj)                              //closes the obj

OUTPUT
The led will first glow continuously,but once you run the code in matlab it will start blinking.....

REMARK:
this is a simple pgm.just try it out friends...........:-)