Sunday, July 6, 2008

Image Processing using MATLAB

Wanna make ibot to feel your emotions and respond to your commands ? then …… Let’s add an eye to our ibot …

Let’s make a ball follower robot to understand the basics of image processing… So now its time to process the data from the eye and act accordingly…

Checklist before we start off…

  • iBot controller board
  • Serial Cable
  • Power Supply
  • Matlab
  • Working Web cam mounted on a bot

Well, i guess we are now ready to get on with image processing…

It’s quite simple from one point of view that we got only 3 actions to do in image processing…

  1. Image Acquisition
  1. Image Processing

2.1 Pre processing
2.2 Feature extraction

  1. Data Communication

Step 1:

Acquiring an image in real time from a web cam or any camera source is quite simple… it can be done with few lines of simple code…

It’s interesting to know about you’re camera before you use it and it’s required too…

Hey!! U learnt how to use matlab from previous article right…

So let’s work on with these commands…

>> imaqhwinfo

InstalledAdaptors: {’winvideo’}
MATLABVersion: ‘7.0 (R14)’
ToolboxName: ‘Image Acquisition Toolbox’
ToolboxVersion: ‘1.5 (R14)’

Note: Get to know about the adaptor installed in your device… In my case its ‘winvideo’… so what’s up with you??

winvideo should be replaced with your adaptor name… anyways most of us will be having winvideo as the installed adaptor…

For few more details about your cam, check out with this command

>> info = imaqhwinfo (’winvideo’, 1);

What is the info you got???
……. You guessed i suppose, if not refer to previous article…

You are still there… that’s cool, so you are ready to capture a video from web cam and follow a ball right…then try out with this code

vid = videoinput (’winvideo’); % this initializes or creates a video object.
Preview (vid) % wait for few seconds for the frame to be displayed

You are getting a continuous video from your web cam right.

Note: Now its time to adjust the lens at proper angle and it depends on cameras vertical and horizontal view angles…

Let’s say this is a frame at an instant and we wanna grab this ….

I = getsnapshot (vid); % get snap shot of a video object

Hey!! that’s all with Image Acquisition… Now its time to process this image

Step 2:

Processing this acquired image is the crucial and important phase in developing a vision system… The processing method varies from person to person, its like how you are looking at image and what is that you are looking for in the image… (i.e. it all depends on how the frame is …whether you are looking for some colors in the frame or wanna find out obstacles in the frame …) and how to go about with lot of variations in lighting???

Let’s start off this phase with few important functions …

What if you wanna view image I you have grabbed before… it’s simple we got two functions for that…

  • imview(’I')
  • imshow(’I')

If u wanna display two or more image using imshow then command syntax would be

  • figure, imshow(’I2′)

We can read an image already stored in computer with

  • I=imread(’imgname.ext’);

Note: You must be understood the difference between imview & imshow right….

  1. With imview you can see the value of each pixel in the frame and it’s a powerful tool for image analysis which helps us to decide our algorithm…
  2. Processing time for imview is few milliseconds more than imshow… so better to use imshow if you wanna display some image in the process of execution.

Run time or processing time is a very important factor to be considered in image processing. Even a fraction of millisecond reduction in runtime would be an add-on to your code… You can check your codes runtime by using tic and toc commands at the beginning and end of your infinite loop…

In task based processing proper analysis of image gets you an effective code… This analysis also depends on type of operation we are gonna adopt… i.e.

  • Point
  • Local
  • Global

Point- The output value at a specific coordinate is dependent only on the input value at that same coordinate.

Local-The output value at a specific coordinate is dependent on the input values in the neighborhood of that same coordinate.

Global-The output value at a specific coordinate is dependent on all the values in the input image.

We will stick on with local operation for now…

So our region of interest (ROI) in a frame (i.e. red ball), should be properly analyzed for RGB values and tabulate the values for 10 different frames.… This RGB values can be noted from pixel info at bottom left corner of the frame…

Ex: In the above frame, pixel at point (165,151) has [R G B] values [188 60 137] respectively

Ball Follower Algorithm:

(x,y)

Note: This is the virtual frame in which (x, y) is the center of the ball. We consider entire blue box as origin and ‘upper blue U’ is positive Y axis and ‘lower blue U’ as negative Y axis… Now the logic is, we will find the quadrant in which the center of the ball is there and act accordingly…

flowchat

Its quite simple right…. Till now we have discussed all the important topics to get on with coding… Now it’s all with implementation of what we have learnt till now…

Hey!!! Try coding this Algorithm… lets see how far you can get on with it……

Step 3:

Huhh… last few slides…

Communicating with bot, this is interesting to get on with …

Matlab provides good support and ease to access any of these ports for communicating with bot

  1. Parallel port
  2. Serial port
  3. USB

Personally if like to use parallel port because of its good support with Matlab and its access time is less compared with other ports…

But serial communication is quite useful in many projects… it’s just with couple of lines we can access this Serial port… some times it happens that that you need to close to your matlab and then reopen or even restart your system sometimes when you are using serial port in continuous loop…

Parallel Port:

Wanna access your parallel port… here is a simple code

parport=digitalio(’parallel’,'LPT1′);

Note: This command creates a digital I/O object… once you are done with this command you can access your parallel port.

Line1= addline(parport,0:3,’out’);

Note: Now you need to tell Matlab which lines of parallel port should be used as output or input pins. This command configures first 4 data pins of parallel port as output pins.

What if you wanna configure next four pins to input pins???

You guessed it… one more line in the code

Line2= addline(parport,4:7, ‘in’);

Suppose you wanna send 4 bits to parallel port according to requirement, store the values in an array…

output=[0 0 0 0;0 1 0 1;1 0 0 1;0 1 1 0;1 0 1 0];
pval=output(1,:);

Note: This output (1,:) lists all the values in the 1st row of output array.

putvalue(parport,pval);

Note: It’s the last step, to send the output values to parallel port.

Serial Port:

How about working with serial portL ??? It’s quite simple as such; it’s much similar to the concept of files in C language.

ser=serial(’com1′);
Note: This creates a serial object…

fopen(ser)
Note: This open the object in both read and write mode…

fprintf(ser,’data’);
Note: writes the data to the object… i.e. sends data to serial port

fclose(ser)
Note: It’s important to close the object at the end of communication.

Hey!!! We are done with making a ball follower… Let your ibot play with a ballJ

Hmm… here is the code….

Wanna develop your own Algorithm… here are few more important concepts of image processing…

  1. Image adjustment
  2. Image conversions
  3. Edge detection
  • Image adjustment:

You can adjust the value of pixels in a frame to some fixed range if light conditions are varying ….

imadjust is the command to be used for adjusting the values of the pixel to fixed range… you can find more details at this link…

http://www-ccs.ucsd.edu/matlab/toolbox/images/imadjust.html

  • Image conversions:
  • RGB to Gray Scale

This can be done in 2 ways…..

    • Gray_img = .2989*rgb_img(:,:,1)…

+.5870*rgb_img(:,:,2)…
+.1140*rgb_img(:,:,3);

    • Gray_img = rgb2gray(rgb_img)

Converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. This is sometimes very useful in processing an image with lots of variations in neighborhood pixel values…

  • RGB to binary image

BW = im2bw(I,level)

Note: im2bw converts the input image to grayscale format, and then uses thresholding to convert this grayscale image to binary. The output binary image BW has values of 0 (black) for all pixels in the input image with luminance less than level and 1 (white) for all other pixels

level = graythresh(I)

Note: This computes a global threshold (level) value that is used with im2bw to convert into binary image. Value of this level lies in the range [0, 1].

It has got its advantage in modeling potential fields…

  • Edge Detection:

Edge detection the most important and power tool in image processing helps us in solving some interesting problems in computer vision… It’s a part of process called segmentation. it takes an intensity image I as its input, and returns a binary image BW of the same size as I, with 1’s representing edges in I and 0’s elsewhere

We got around six predefined edge detectors in matlab. And each one has got its own pros and cons. So our ability of choosing the right detector gets you appropriate results…

Hmm!! Atlast we are all set to develop some basic vision system… how about you developing one vision system….

How would it be if we could add an ear to ibot???

That’s interesting right….. Let’s work on some very basics of voice recognition…

As like in image processing we have created video object, we have to create an audio object in voice processing.

ai = analoginput(’winsound’);
So you have created an audio object… now we need to add an audio channel to operate in mono or stereo mode… so here is how we add a channel

addchannel(ai,1);
Note: 1 is for mono and 2 is for stereo mode

To acquire audio data we need to set sample rate and define how often we need to trigger…

set(ai,’SampleRate’,8000);
set(ai,’SamplesPerTrigger’,4000)

Note: Trigger duration = (sample rate/samples per trigger)

Now it’s all with starting the audio object and getting the data…
start(ai)
data = getdata(ai);

After every cycle we need to delete and clear audio object
delete(ai)
clear ai

Wouldn’t be interesting to see analog data of voice right….
Plot(data)

Hint: Proper analysis of the data for different words coupling with basic matlab programming concepts helps you to develop a simple speaker dependent voice recognition system for couples of words….

Huhh!! We are now set with developing a real ibot with eyes and ears right….

19 comments:

I3rahim Kh.AL-taie said...

**************
hi ROBOMAN >>>u are such a great roboman....
i will use ur info in my graduation project ..."fire fighting robot"
i will be very pleased if u help me in my project
>>>>>>>>>>>>>>>>>>>
by the way i am ibrahim
and this is my email :ibrahim_khalil34@yahoo.com

I3rahim Kh.AL-taie said...

peace.....

have a nice day*****

Rikil K Shah said...

good job man!!!

/(am /\/\aheshwari said...

fantabulous... yaar i need ur help in a robotics comp.. can u??? ma e mail id is rampasari@gmail.com

Swapnil said...

very good informatn,really helpful for my seminar.
my email id swapnil.vyavahare@gmail.com

Swapnil said...

Hi,very good job.But how to connect matlab with motor via controller board?Which controller to use?
Mail me at swapnil.vyavahare@gmail.com

Unknown said...

u've copied everything from TRI resourses

Unknown said...

i haven't used it anywhere.

Unknown said...

Hi,
U have done an awesome work
Ca u please show me how to send data using usb??
I need it immediately
Srinivasan

Saurabh said...

thankyou very very much.your information is really very usefull to me.i am engineering student and want to make something like rajanikant's robot in movie ROBOT.i wish to get your help in that.my email id is saurabh.lanje@yahoo.com

ashish choudhary said...

Hello Guys

Can any body explain me the code
Its very urgent as I am working on it but not able to understand this code,mainly the color detection part.U can mail me at aag07251@yahoo.com.

ashish choudhary said...

Hello,
I am new to MATLAB and want to make an obstacle detector.I got your code but I dont understand the colour detection part.Also for calculating the centroid Why U r using the reference 30,I am not able to get.So plz help me........

Sheldon said...

please explain me the code

aman said...

thanx a lot sir for ur valuable guidance........
aman vikas abhinav
amankapil213@gmail.com
vikassaini40702@gmail.com

akash said...

hey,
i dont have a desktop, i use my laptop. so i guess i have to buy a usb to parallel port converter. but i found out that there are 2 types:
db25 and db36.. which 1 is recommended?? will any of them work??

hemchandra said...

sir the images given inn the tutorial are not being shown

Kiran N said...

Hi, I am Kiran

when i upload the data to the microcontroller board (Arduino/Atmega8).
It is Showing an error as below

??? Error using ==> serial.fopen at 71
Port: COM7 is not available. Available ports: COM8, COM14, COM18, COM20, COM28.
Use INSTRFIND to determine if other instrument objects are connected to the
requested device.

Error in ==> lordmuruganfinal at 19
fopen(ser);

I am trying from the last 2 days..
please help me to solve this problem..

Unknown said...

i wana your help for my project "hand gesture recognition using image processing in matlab for wheel chair automation.my email id is :ayyazmayo@gmail.com


raghav said...

can u please send me this code in my mail id: its kesarraghav@gmail.com
i am working on the same project,
thanks anyways