Sunday 11 August 2013

Music Player with Hand Gesture Control using Neural Network

Hello guys, I have already posted an article on hand gesture recognition using neural network,if you have not see it then please go through it first.Well,here is the Link 

So,using the previous implementation I am now going to show you how you can built a Music Player that will be controlled by hand gestures.That means there will be a separate predefined gesture for performing every individual task that will be performed on the player.

That means there will be an unique gesture for Play,another for pause and so on.


So here are the steps using which you can built it easily :

1. I hope you are all familiar with Matlab GUI,if not then please see this Link 

2. Now here I am not going to briefly explain how to play and pause music in matlab,here my main focus is to tell you how can you control this using hand gestures.

Still,if any one has any problem in building the music player he or she can tell his or her problem through comment,then I will post the code for that later.

3. For example lets assume we use the following gesture for play action.





Then we have to get at least 5-10 pic like this and save it into a folder.

4.  Now train the network with the sample picture,so that when ever any similar gesture is detected during testing,then system would perform some task.

For code reference,please visit this link below :

Gesture Recognition using Neural Network

5.Now the point is where to insert the code. Well for every recognition system we go for a threshold value to determine whether it is the desired result.In the previous Hand gesture recognition system post also I have done the same in the end.

So insert the desired music player action code within that MATCH_FOUND code block.
like this,

if(PLAY_IMG_MATCH_FOUND==TRUE)
{
  //Music player play code
}

Similarly if you want to perform any hard ware related task then just go to this link  and insert the code in the same block as I mentioned before.

Just for example,I have shown this one example PLAY action with this sample images and one gesture.

You can use different gestures for performing different task like PAUSE,STOP etc.

There are lot more things to do with this project...try it yourself, and please let me know about your new works...best of luck...thanks for visiting,have lots of fun :)

Sunday 4 August 2013

Gesture Recognition using Neural Network in MATLAB Code Part 3

preprocess_gesture.m 

If you have not seen previous post then please go through it first then come back   go to Previous Part


Create a Menu

clc
F = MENU('Choose a database set','Test Set','Train Set'); 
if F==1
K = MENU('Choose a file','Test O');

FOR TESTING A DATASET

if K == 1
    loop=5 
    for i=1:loop 
       string = ['test\O\' num2str(i) '.tif']; 
       Rimages{i} = imread(string); 
    end 

 end 
 end 
end;

FOR TRAINING


if F==2 
    loop=3    %Set loop to 3. All train sets have 3 images 
    L = MENU('Choose a file','Train O'); 
if L == 1 

    for i=1:loop 
    string = ['train\O\' num2str(i) '.tif']; 
    Rimages{i} = imread(string); 
    end
end 
end 

Now do what I am doing here


T{i}=imresize(Timages{i},[150,140]); 

    x = [0 -1 1];                               
    y = [0 1 -1]';

dx{i} = convn(T{i},x,'same');
dy{i} = convn(T{i},y,'same');
gradient{i} = dy{i} ./dx{i};
theta{i} = atan(gradient{i});
cl{i}= im2col(theta{i},[1 1],'distinct');
 N{i} = (cl{i}*180)/3.14159265359; .
c1{i}=(N{i}>0)&(N{i}<10 div="" nbsp="">
    s1{i}=sum(c1{i}); 

    c2{i}=(N{i}>10.0001)&(N{i}<20 div="" nbsp="">
    s2{i}=sum(c2{i}); 

    c3{i}=(N{i}>20.0001)&(N{i}<30 div="" nbsp="">
    sum(c3{i}); 
    s3{i}=sum(c3{i}); 

    c4{i}=(N{i}>30.0001)&(N{i}<40 div="" nbsp="">
    sum(c4{i}); 
    s4{i}=sum(c4{i}); 

    c5{i}=(N{i}>40.0001)&(N{i}<50 div="" nbsp="">
    sum(c5{i}); 
    s5{i}=sum(c5{i}); 

    c6{i}=(N{i}>50.0001)&(N{i}<60 div="" nbsp="">
    sum(c6{i}); 
    s6{i}=sum(c6{i}); 

    c7{i}=(N{i}>60.0001)&(N{i}<70 div="" nbsp="">
    sum(c7{i}); 
    s7{i}=sum(c7{i}); 

    c8{i}=(N{i}>70.0001)&(N{i}<80 div="" nbsp="">
    sum(c8{i}); 
    s8{i}=sum(c8{i}); 

    c9{i}=(N{i}>80.0001)&(N{i}<90 div="" nbsp="">
    sum(c9{i}); 
    s9{i}=sum(c9{i}); 

    c10{i}=(N{i}>90.0001)&(N{i}<100 div="" nbsp="">
    sum(c10{i}); 
    s10{i}=sum(c10{i}); 

    c11{i}=(N{i}>-89.9)&(N{i}<-80 div="" nbsp="">
    sum(c11{i}); 
    s11{i}=sum(c11{i}); 
    c12{i}=(N{i}>-80.0001)&(N{i}<-70 div="" nbsp="">
    sum(c12{i}); 
    s12{i}=sum(c12{i}); 

    c13{i}=(N{i}>-70.0001)&(N{i}<-60 div="" nbsp="">
    sum(c13{i}); 
    s13{i}=sum(c13{i}); 

    c14{i}=(N{i}>-60.0001)&(N{i}<-50 div="" nbsp="">
    sum(c14{i}); 
    s14{i}=sum(c14{i}); 

    c15{i}=(N{i}>-50.0001)&(N{i}<-40 div="" nbsp="">
    sum(c15{i}); 
    s15{i}=sum(c15{i}); 

    c16{i}=(N{i}>-40.0001)&(N{i}<-30 div="" nbsp="">
    sum(c16{i}); 
    s16{i}=sum(c16{i}); 

    c17{i}=(N{i}>-30.0001)&(N{i}<-20 div="" nbsp="">
    sum(c17{i}); 
    s17{i}=sum(c17{i}); 

    c18{i}=(N{i}>-20.0001)&(N{i}<-10 div="" nbsp="">
    sum(c18{i}); 
    s18{i}=sum(c18{i}); 

    c19{i}=(N{i}>-10.0001)&(N{i}<-0 .0001="" div="" nbsp="">
    sum(c19{i}); 
    s19{i}=sum(c19{i}); 

    D{i}= [s1{i} s2{i} s3{i} s4{i} s5{i} s6{i} s7{i} s8{i} s9{i} s10{i} s11{i} s12{i} s13{i} s14{i} s15{i} s16{i} s17{i} s18{i} s19{i}]; 

close(w);

Guys thanks for visiting my blog,Have fun :)


This is the end of gesture recognition


Please LIKE and COMMENT...


Gesture Recognition using Neural Network in MATLAB Code Part 2

neuralnetwork_gesture.m

Go to First Part                             Go to Next Part



echo on
clc
pause
clc

Store the training informations in a test file

fid = fopen('train.txt','rt');
P1 = fscanf(fid,'%f',[19,inf]);
P=P1;

Open some text file using code to write and fetch the required information about image.


fid = fopen('testO.txt','rt');
TS1 = fscanf(fid,'%f',[19,inf]);

%(As here we are only testing alphabet 'O')

fid = fopen('target8.txt','rt');
T = fscanf(fid,'%f',[8,inf]);

It has been found that the optimal number of neurons for  the hidden layer is 85

S1 = 85;
S2 = 5;

Now we have to initialize pre-processing layer

[W1,b1] = initp(P,S1);

We also have to initialize learning layer

 [W2,b2] = initp(S1,T);

pause 

NOW TRAIN THE NETWORK


A1 = simup(P,W1,b1);
TP = [1 500]; 
pause
clf reset
figure(gcf)

% resize the frame size
setfsize(600,300);

[W2,b2,epochs,errors] = trainp(W2,b2,A1,T,TP);

pause
clc 

ploterr(errors); 


pause

M = MENU('Choose a file resolution','Test O');    %as we are only showing 'O',you can add more
if M == 1 
 TS = TS1;
else 
 disp('Wrong input');

a1 = simup(TS,W1,b1);        

a2 = simup(a1,W2,b2)      

echo off 

Next part will be posted in next Post,Thanks for visiting have fun

Gesture Recognition using Neural Network in MATLAB 


Gesture Recognition using Neural Network in MATLAB Code

Hi guys,many people were requesting for gesture recognition code so here it is ...
Well for any recognition system there are some simple common states what we also followed here they are:


1. First you have to have data set.Which you have to prepare or download from any website according to your demand.Like you want to want to recognize an alphabet 'O'.Then you have to provide particular data set for 'O'.


Like this :



Okey this is only one picture but I recommend at least 5 pictures you have to give to recognize a gesture.


2. Now give matlab access to the data sets providing particular path of the folder on your system,it is better to make different folder for different alphabet sample.


3. Now you have to train the network.I will show you how to do it in later part.It is the most important part,here you are making your network understand that " ANY IMAGE LIKE-THIS IS ALPHABET 'O' "


4.Here you need to provide an algorithm that will set such a threshold value that will decide whether the test image is 'O' or NOT.


5.Now you need some more sample of 'O' to test the network whether it is coming fine.


6.Well that is it. I will give you the sample code in my next post so that it will be easy for you to implement.


Proceed to Next Part


Thanks a lot for visiting have fun.









Sunday 14 April 2013

People Counting System using face detection Code





close all;
clear all;
clc;
rgbInputImage = imread('photo.jpg');
labInputImage = applycform(rgbInputImage,makecform('srgb2lab'));
Lbpdfhe = fcnBPDFHE(labInputImage(:,:,1));
labOutputImage = cat(3,Lbpdfhe,labInputImage(:,:,2),labInputImage(:,:,3));
rgbOutputImage = applycform(labOutputImage,makecform('lab2srgb'));
figure, imshow(rgbInputImage);
figure, imshow(rgbOutputImage);
img=rgbOutputImage;
final_image = zeros(size(img,1), size(img,2));
if(size(img, 3) > 1)
for i = 1:size(img,1)
for j = 1:size(img,2)
R = img(i,j,1);
G = img(i,j,2);
B = img(i,j,3);
if(R > 92 && G > 40 && B > 20)
v = [R,G,B];
if((max(v) - min(v)) > 15)
if(abs(R-G) > 15 && R > G && R > B)
final_image(i,j) = 1;
end
end
end
end
end
end
binaryImage=im2bw(final_image,0.6);
figure, imshow(binaryImage);
binaryImage = imfill(binaryImage, 'holes');
figure, imshow(binaryImage);
binaryImage = bwareaopen(binaryImage,1890);
figure,imshow(binaryImage);
labeledImage = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, final_image, 'all');
numberOfPeople = size(blobMeasurements, 1)
imagesc(rgbInputImage); title('Outlines, from bwboundaries()');
hold on;
boundaries = bwboundaries(binaryImage);
for k = 1 : numberOfPeople
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
imagesc(rgbInputImage);
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople
thisBlobsBox = blobMeasurements(k).BoundingBox;
x1 = thisBlobsBox(1);
y1 = thisBlobsBox(2);
x2 = x1 + thisBlobsBox(3);
y2 = y1 + thisBlobsBox(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'LineWidth', 2);
end                          


fcnBPDFHE() function should be added

avialable in mathworks website link is here :



Used image URL : 








Sunday 7 April 2013

Silent Password Creation using matlab

Here we are going to create silent password.We can do this using both from a video file or from live streaming.The main thing is that he camera should focus on the lip.It should track the lip movement.The key idea is that we are going to take a threshold value,using that particular value we are going to check whether the password is wrong or right.We will track the lip movement,if the movement is beyond threshold then wrong password otherwise right.



Well here are the steps we need to do to create a silent password with matlab :

1.Take video of lip movement or record using webcam.

2.Now use a detection algorithm algorithm from here
(Note : if you are using live detection you need to track the motion using another method,that code is also available in this blog,here is the link.

3.Now what you need to do is that you need to set the threshold value.

Threshold value :

Now the threshold value can be set or generated in many ways.I have done it in one way what i am going to tell you but you can follow different approach. 

i.I have calculated the no of frames between one lip touching frame to the next one.In live streaming case the threshold should be given a bit large for less errors.

ii..For my case the calculated frame no is the threshold.If you find any better approach please let me know through comment.

4.So once the threshold is calculated your prog is okk.

5.Now simple calculation if detected result is beyond threshold then rejected otherwise accepted.





Wednesday 27 February 2013

Play Computer games with hand gesture recognition using Matlab

To do this experiment,just you need an webcam and matlab2012a or 2012b

Now we are gonna do live motion detection,the below live detection code is not written by me,i got it from this link http://arindambose.webs.com/htmls/Motion%20Detection%20in%20LIVE%20Video.html

vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ...
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
optical = vision.OpticalFlow('OutputValue', 'Horizontal and vertical components in complex form');

maxWidth = imaqhwinfo(vidDevice,'MaxWidth');
maxHeight = imaqhwinfo(vidDevice,'MaxHeight');
shapes = vision.ShapeInserter;
shapes.Shape = 'Lines';
shapes.BorderColor = 'Custom';
shapes.CustomBorderColor = [255 0 0];
r = 1:5:maxHeight;
c = 1:5:maxWidth;
[Y, X] = meshgrid(c,r);

hVideoIn = vision.VideoPlayer;
hVideoIn.Name = 'Original Video';
hVideoIn.Position = [30 100 640 480];
hVideoOut = vision.VideoPlayer;
hVideoOut.Name = 'Motion Detected Video';
hVideoOut.Position = [700 100 640 480];

nFrames = 0;
while (nFrames < Inf)
rgbData = step(vidDevice);
optFlow = step(optical,rgb2gray(rgbData));
optFlow_DS = optFlow(r, c);
H = imag(optFlow_DS)*50;
V = real(optFlow_DS)*50;
lines = [Y(:)'; X(:)'; Y(:)'+V(:)'; X(:)'+H(:)'];
rgb_Out = step(shapes, rgbData, lines');
step(hVideoIn, rgbData);
step(hVideoOut, rgb_Out);
nFrames = nFrames + 1;
end
release(hVideoOut);
release(hVideoIn);
release(vidDevice);

But after this Ive changed this a little bit and  made a different thing,hope you like it.
So the code i got from the above link,well im pasting it here,

Now again use the redbox around the motion

rectangle('Position',cen,'EdgeColor','r','LineWidth',5);

Now perform a mouse click operation whenever a redbox appears in the video,get the code here

So,this is how your computer player will fire or do some other stuff as per game settings

And move the mouse pointer as per box location,code is Here

By this your player will switch it's view,
Now after doing all this,

1.Run your matlab code
2.Minimize the video stream
3.Run the game you want to play


I've tried this with some popular games,like counter strike,IGI 



Well this is a very basic idea,you can implement it further....you can also add some keyboard operation for performing other tasks,Keyboard Code here

try it it will definitely work... Thank u



Press like and leave comment

Monday 25 February 2013

Vehicle Counting System in Matlab


1. Input a video of vehicles ex. test.avi
2. Now extract the frames and save in a specific folder
    Get the code here, one of my previous post
3. Detect the motion of the vehicle in the video
    Get the code here

Now if everything has gone alright,we can begin.

We just make very simple changes in the previous code to make the thing work perfectly.

Step 1 : Watch the redbox around the moving object.Now insert this code in there

             rectangle('Position',cen,'EdgeColor','r','LineWidth',5);
     
             Here 'cen' is the centroid

Step 2: Now the most important step,insert this code after that

        txt=text(no, strcat('car: ', num2str(round(no))));
        set(txt, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'black');

It will display the number of cars. 'no' is a variable which is set to '0' at first.But when ever a motion is detected in the video 'no' will be incremented by 1,that is how we get the vehicle count.

But the basic problem what you will face in this method that is,here we are considering frame after frame,so the 'no' variable gets incremented each time,but thats not what we want,we want the actual count,so we are gonna need a FLAG variable which indicates if 'no' is incremented by 1 for a particular vehicle then it wont increment for the specific one again.

like this,
FLAG=0;
if FLAG==0
   no=no+1;
   flag=1;
   frame=frame+1;
end
else
   frame=frame+1;
end

So now it is done,Thank You :)
Press like and leave comment

Screen Capture With Matlab

screencapture.m

clc;
r=java.awt.Robot;
tool=java.awt.Toolkit.getDefaultToolkit();
rectangle=java.awt.Rectangle(tool.getScreenSize());
image=r.createScreenCapture(rectangle);
fileh=java.io.File('scrcap.jpg');
a=javax.imageio.ImageIO.write(image,'jpg',fileh);
disp('done');

Saturday 23 February 2013

Bestfitness matlab code using neural network


    clc;
    a= [39.8878 39.9947 39.9544 40.0285 39.9263 40.0013 40.9078 39.7623 40.9278];
    b=[0.6016 0.6016 0.6045 0.6074 0.6067 0.6074 0.6052 0.5998 0.6056] ;
    c=[100 100 100 100 100 100 100 100 100];
    y=a+ b.*c;
    I=[a; b; c];
    T=y;
    R=[0 1000; 0 1000 ; 0 1000];
    S=[5 1];
    net = newff([0 1000;0 1000 ;0 1000],[4 1],{'tansig','purelin'});
    O=[100.0439 100.5907 100.4028 100.7729 100.5963 100.7457 100.5964 99.7375 100.6877];
    net=train(net,I,O);
    O1=sim(net,I);
    plot(1:9,O,1:9,O1);
     net.IW{1}
     net.LW{2,1}
     BestFitness=sim(net,[39.8595 0.6023 100]');

Wednesday 13 February 2013

Making a Photoviewer with GUI using Matlab

Code :


clc;
imr=input('Enter the full path of the image frames:','s');
somefolder=imr;
filelist=dir(somefolder);
for i=1:size(filelist,1)
    if filelist(i).isdir ~=true
        fname=filelist(i).name;
        if strcmp(fname(size(fname,2)-3:size(fname,2)),'.jpg')==1
            tmp=imread([somefolder fname]);
            if size(tmp,3)==3;
                tmp=imresize(tmp,[400,400],'bilinear');
                imshow(tmp);
                pause(1);
                disp([fname,':loaded']);
            end
        end
    end
end

Building GUI :

If you dont know how create basic matlab GUI then you can see my previous post :

Building GUI with Matlab

Step 1 : I have made a very simple interface,drawn just a button  and an axes


Step 2 : Now open the GUI .m file by clicking view and then m-file editor



Step 3: Now paste the above code in the pushbutton_callback function and save

Step 4: Now run the code,press play button,then it should ask for the image folder path,write the path press          enter it will show the images

Building GUI with Matlab

1. Type 'guide' on the console and press enter



2. Now select a guide template and mark 'save on start up as' option then change the name,ex. test.fig
then press ok.

3. A screen will appear like this one...



4. Now you can draw button,use axis for showing images or videos,and make your own GUI.

5. To edit the matlab code do this,



6. Copy the matlab code what you want to execute after pressing a button within that particular button function

7. Then run your code pressing 'f5' it should show the GUI you built.



Monday 4 February 2013

Virtual Mouse using Hand gestures Matlab Code

1. Take a sample Video

2. Use any motion tracking algorithm like camshift or kalman to detect and track the motion of your hand movements.

Get code here : Motion detection and tracking matlab code


3. Now track the centroids

 like this ----
        bb = stats(object).BoundingBox;
        bc = stats(object).Centroid;

4. Now move the cursor as per centroids last recorded location

use this piece of code to move the cursor


         import java.awt.Robot;
         cursor = Robot;
         cursor.mouseMove(bc(1), bc(2));


5. Clicking mechanism :

Now where you want a clicking operation during motion detection use this code

import java.awt.Robot;
import java.awt.event.*;

cursor = Robot;

cursor.mousePress(InputEvent.BUTTON1_MASK);

For more hardware access help : Click here


Saturday 2 February 2013

Real time Video Recording and Save as AVI matlab code

Here is an easy way to do this using my older Posts :

Just first Run this file  :

Capture and save Frames using Webcam matlab Code


Now run this :

Frames to AVI using Matlab Code

*just combine them both and save as a single file

Guess what... its done... :D

So,what I want to say is that always look at the smaller modules,combine them,Make your own project...



Frames to AVI using Matlab Code


avi = avifile('output1.avi');
avi.COMPRESSION ='None';
for i =1:numOfFrames
   %%%read image
   %%% call imread or related something
   %%%%%%%%%%%%%
   avi = addframe(avi,img);
end
aviobj = close(avi);


Reverse thing,you may also see Video to frames


Capture and save Frames using Webcam matlab Code


clc ; % clearing the command window
n = input(' Enter the number of photos to be taken: ');
intervel = input(' Enter the time(seconds) gap between succeessive photos: ');
photosave = input(' Do you want to save the files(y/n): ','s');
disp('Please wait...');
outputFolder = fullfile(cd, 'frames');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
obj = videoinput('winvideo',1);
preview(obj);
disp('Press Enter to start after webcam initialization.');
pause;
disp('First shot will taken after 1 second');
pause(1);
for i=1:n
img=getsnapshot(obj);
image(img);
if(photosave == 'y')
outputBaseFileName = sprintf('fr%d.png',i);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(img,outputFullFileName,'jpg');
end
pause(intervel);
end
closepreview;

disp('The program successfully taken the photos');
disp('Done.');

Friday 1 February 2013

Access Webcam using Matlab

webcam.m

clc;
vid = videoinput('winvideo', 1, 'RGB24_320x240');
preview(vid);


Wednesday 30 January 2013

Client server program Matlab Code


server.m


import java.net.*;
import java.io.*;

%% Create Socket
port = 20;
    %input('Enter port number: ');
Server = ServerSocket (port);
disp('Waiting for a connection...')
connected = Server.accept;

iStream = connected.getInputStream;
oStream = connected.getOutputStream;

% Greets the client
oStream.sendS('Welcome client!')

% Waiting for messages from client
while ~(iStream.available)
end
readS(iStream);

%% Communication
msg = '';
while isempty(strfind(msg,'!q'))
% Waits for messages from client
while ~(iStream.available)
end
msg = readS(iStream);

if isempty(strfind(msg,'!q'))
 % Sends message to client
 disp 'Server''s turn!'
 %cmd = input('Toclient>> ', 's');
      cmd = 'hey im feeling okey!'
 oStream.sendS(cmd);

else
 oStream.sendS('!q');
end
end

pause(1)
connected.close;
disp (['Connection ended: ' datestr(now)]);

client.m


import java.net.*;
import java.io.*;

%% Create Socket and Connect to server
% Note that I am connecting to localhost, but you may change that to the corresponding machine IP that is running the server.
port = 20;
    %input('Enter port number: ');
disp('Connecting to server...')
clientSocket = Socket('localhost', port);

iStream = clientSocket.getInputStream;
oStream = clientSocket.getOutputStream;

% Greets the server on connection is accepted
oStream.sendS('Hi Server!')

% Waiting for messages from Server
while ~(iStream.available)
end
readS(iStream);
msg = '';
while isempty(strfind(msg,'!q'))
% Sends message to server
disp 'Client''s turn!'
cmd = input('ToServer>> ', 's');
oStream.sendS(cmd);

% Waits for replies from server
while ~(iStream.available)
end
msg = readS(iStream);
end
clientSocket.close
disp (['Connection ended: ' datestr(now)]);

readS.m


function b = readS(iStream)

disp 'Reply:'

% Number of messages
n = iStream.available;

% Buffer size = 500 characters
b = zeros(1,500);
for i = 1:n
   b(i) = iStream.read();
end

if (b(1) ~= 0)
   disp (char(b))
end
disp ('')

sendS.m


    function sendS(oStream,str)
oStream.write(int8([str 10]))





Sunday 27 January 2013

Access serial port using Matlab code


AccessSerialPort.m


clear all;
close all;

s = serial('COM1');  %assigns the object s to serial port

set(s, 'InputBufferSize', 256);  %number of bytes in inout buffer
set(s, 'FlowControl', 'hardware');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',10);
disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));

disp(['Port Setup Done!!',num2str(prop)]);

fopen(s);           %opens the serial port
t=1;
disp('Running');
x=0;
while(t<200 p="">   a =fread(s);
   a=max(a);
   x =[x a];
   plot(x);
   axis auto;
   grid on;
   disp([num2str(t),'th iteration max= ',num2str(a)]);
   hold on;
   t=t+1;
   a=0;
   drawnow;
end

fclose(s); %close the serial port

Saturday 26 January 2013

Face Detection Matlab Code


face_detect.m


function []=detect_face(I)

close all;
Faces=[];
numFaceFound=0;

I=double(I);

H=size(I,1);
W=size(I,2);
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
YCbCr=rgb2ycbcr(I);
Y=YCbCr(:,:,1);
minY=min(min(Y));
maxY=max(max(Y));
Y=255.0*(Y-minY)./(maxY-minY);
YEye=Y;
Yavg=sum(sum(Y))/(W*H);

T=1;
if (Yavg<64)
    T=1.4;
elseif (Yavg>192)
    T=0.6;
end

if (T~=1)
    RI=R.^T;
    GI=G.^T;
else
    RI=R;
    GI=G;
end

C=zeros(H,W,3);
C(:,:,1)=RI;
C(:,:,2)=GI;
C(:,:,3)=B;

figure,imshow(C/255);
title('Lighting compensation');
YCbCr=rgb2ycbcr(C);
Cr=YCbCr(:,:,3);

S=zeros(H,W);
[SkinIndexRow,SkinIndexCol] =find(10<Cr & Cr<45);
for i=1:length(SkinIndexRow)
    S(SkinIndexRow(i),SkinIndexCol(i))=1;
end

figure,imshow(S);
title('skin');
SN=zeros(H,W);
for i=1:H-5
    for j=1:W-5
        localSum=sum(sum(S(i:i+4, j:j+4)));
        SN(i:i+5, j:j+5)=(localSum>12);
    end
end

figure,imshow(SN);
title('skin with noise removal');
L = bwlabel(SN,8);
BB  = regionprops(L, 'BoundingBox');
bboxes= cat(1, BB.BoundingBox);
widths=bboxes(:,3);
heights=bboxes(:,4);
hByW=heights./widths;

lenRegions=size(bboxes,1);
foundFaces=zeros(1,lenRegions);

rgb=label2rgb(L);
figure,imshow(rgb);
title('face candidates');
for i=1:lenRegions
 
    if (hByW(i)>1.75 || hByW(i)<0.75)
        continue;
    end
    if (heights(i)<20 && widths(i)<20)
        continue;
    end
    CurBB=bboxes(i,:);
    XStart=CurBB(1);
    YStart=CurBB(2);
    WCur=CurBB(3);
    HCur=CurBB(4);
    rangeY=int32(YStart):int32(YStart+HCur-1);
    rangeX= int32(XStart):int32(XStart+WCur-1);
    RIC=RI(rangeY, rangeX);
    GIC=GI(rangeY, rangeX);
    BC=B(rangeY, rangeX);
 
    figure, imshow(RIC/255);
    title('Possible face R channel');
    M=zeros(HCur, WCur);
    theta=acos( 0.5.*(2.*RIC-GIC-BC) ./ sqrt( (RIC-GIC).*(RIC-GIC) + (RIC-BC).*(GIC-BC) ) );
    theta(isnan(theta))=0;
    thetaMean=mean2(theta);
    [MouthIndexRow,MouthIndexCol] =find(theta<thetaMean/4);
    for j=1:length(MouthIndexRow)
        M(MouthIndexRow(j),MouthIndexCol(j))=1;
    end
    Hist=zeros(1, HCur);
 
    for j=1:HCur
        Hist(j)=length(find(M(j,:)==1));
    end
 
    wMax=find(Hist==max(Hist));
    wMax=wMax(1);
 
    if (wMax < WCur/6)
        continue;
    end
 
    figure, imshow(M);
    title('Mouth map');
    eyeH=HCur-wMax;
    eyeW=WCur;
 
    YC=YEye(YStart:YStart+eyeH-1, XStart:XStart+eyeW-1);
 
    E=zeros(eyeH,eyeW);
    [EyeIndexRow,EyeIndexCol] =find(65<YC & YC<80);
    for j=1:length(EyeIndexRow)
        E(EyeIndexRow(j),EyeIndexCol(j))=1;
    end
    EyeExist=find(Hist>0.3*wMax);
    if (~(length(EyeExist)>0))
        continue;
    end
 
    foundFaces(i)=1;
    numFaceFound=numFaceFound+1;
 
end
disp('Number of faces found');
numFaceFound;

if (numFaceFound>0)
    disp('Indices of faces found: ');
    ind=find(foundFaces==1);
    CurBB=bboxes(ind,:);
    CurBB
else
    close all;
end

end

run the file using

main.m


I=double(imread('sample.jpg'));
detect_face(I);

You might also like : Skin Detection



Controlling Keyboard switches with Matlab code


import java.awt.Robot;
import java.awt.event.*;

robot = Robot();

robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

I have shown with an example of "CAPS LOCK",but it can be replaced with the other buttons also


Friday 25 January 2013

Skin Detection Matlab Code

skin_detect.m

function [out bin] = generate_skinmap(filename)

 
    if nargin > 1 | nargin < 1
        error('usage: generate_skinmap(filename)');
    end;
    img_orig = imread(filename);
    height = size(img_orig,1);
    width = size(img_orig,2);
    out = img_orig;
    bin = zeros(height,width);
    img = grayworld(img_orig);  
    img_ycbcr = rgb2ycbcr(img);
    Cb = img_ycbcr(:,:,2);
    Cr = img_ycbcr(:,:,3);
    [r,c,v] = find(Cb>=77 & Cb<=127 & Cr>=133 & Cr<=173);
    numind = size(r,1);
    for i=1:numind
        out(r(i),c(i),:) = [0 0 255];
        bin(r(i),c(i)) = 1;
    end
    imshow(img_orig);
    figure; imshow(out);
    figure; imshow(bin);
end

run.m


clc;
skin_detect('sample.jpg');


You might also like : Face Detection



RSA algorithm implementation using Matlab code

crypt.m

function mc = crypt(M,N,e)
e=dec2bin(e);
k = 65535;
c  = M;
cf = 1;
cf=mod(c*cf,N);
for i=k-1:-1:1
    c = mod(c*c,N);
    j=k-i+1;
     if e(j)==1
         cf=mod(c*cf,N);
     end
end
mc=cf;

dec2bin.m

function a = dec2bin(d)
i=1;
a=zeros(1,65535);
while d >= 2
    r=rem(d,2);
    if r==1
        a(i)=1;
    else
        a(i)=0;
    end
    i=i+1;
    d=floor(d/2);
end
if d == 2
    a(i) = 0;
else
    a(i) = 1;
end
x=[a(16) a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) 

initialize.m


function [Pk,Phi,d,e] = intialize(p,q)
clc;
disp('Intaializing:');
Pk=p*q;
Phi=(p-1)*(q-1);
%Calculate the value of e
x=2;e=1;
while x > 1
    e=e+1;
    x=gcd(Phi,e);
end
%Calculate the value of d
i=1;
r=1;
while r > 0
    k=(Phi*i)+1;
    r=rem(k,e);
    i=i+1;
end
d=k/e;
clc;
disp(['The value of (N) is: ' num2str(Pk)]);
disp(['The public key (e) is: ' num2str(e)]);
disp(['The value of (Phi) is: ' num2str(Phi)]);
disp(['The private key (d)is: ' num2str(d)]);

rsa.m

clc;
disp('Implementation of RSA Algorithm');
clear all; close all;
p = input('\nEnter the value of p: ');
q = input('\nEnter the value of q: ');
[Pk,Phi,d,e] = intialize(p,q);
M = input('\nEnter the message: ','s');
x=length(M);
c=0;
for j= 1:x
    for i=0:122
        if strcmp(M(j),char(i))
            c(j)=i;
        end
    end
end
disp('ASCII Code of the entered Message:');
disp(c); 
% % %Encryption
for j= 1:x
   cipher(j)= crypt(c(j),Pk,e); 
end
disp('Cipher Text of the entered Message:');
disp(cipher);
% % %Decryption
for j= 1:x
   message(j)= crypt(cipher(j),Pk,d); 
end
disp('Decrypted ASCII of Message:');
disp(message);
disp(['Decrypted Message is: ' message]);




Motion Detection using Camshift algorithm Matlab code


There are two files included.

1.convert video frames using this .m file


clc;
cd frames;

for i = 2:107,
    filename1 = sprintf('%3.3i.png', i);
    filename2 = sprintf('%3.3i.png', i-1);
    A = imread(filename1, 'png');
    A = RGB2GRAY(A);
 
    B = imread(filename2, 'png');
    B = RGB2GRAY(B);
 
    C = imabsdiff(A,B);
    C = medfilt2(C, [5 5]);
    imwrite(C, sprintf('Frame %4.4d.png', i), 'png');
end

cd ..

disp('Done.');


2. Camshift.m



clc;
disp('Running...');
close all;
clear;
cd frames;

    WHITE = 255;

    % Threshold of convergence (in pixels for each deg. of freedom)
T = 1;
 
    % Number of pixels to expand search window by.
    P = 5;

avi = avifile('output1.avi');

% Initial search window size.
%W = [10 10];
W = [99 99];

% Initial location of search window.
%L = [95 193];
L = [71 141];

% For plotting motion
Xpoints=[];
Ypoints=[];

disp('Frame: Coordinates');

for frame = 1:44,
filename = sprintf('%3.3i.png', frame);
R = imread(filename, 'png');

% Convert image from RGB space to HSV space
I = rgb2hsv(R);

% Extract the hue information
I = I(:,:,1);
    I = roicolor(I, 0.83, 1.0);
 
    % Initialization
    oldCamL = [0 0];
    MeanConverging = 1;

    for i = L(1) : L(1)+W(1),
        x = i;
        y = L(2);
        if x > size(I,1) | y > size(I,2) | x < 1 | y < 1
            continue;
        else
            R(x, y,:) = 0;
        end
    end
 
    for i = L(1) : L(1)+W(1),
        x = i;
        y = L(2) + W(2);
        if x > size(I,1) | y > size(I,2) | x < 1 | y < 1
            continue;
        else
            R(x, y, :) = 0;
        end
    end  
 
    for i = L(2) : L(2)+W(2),
        x = L(1);
        y = i;
        if x > size(I,1) | y > size(I,2) | x < 1 | y < 1
            continue;
        else
            R(x, y, :) = 0;
        end
    end  

    for i = L(2) : L(2)+W(2),
        x = L(1)+W(1);
        y = i;
        if x > size(I,1) | y > size(I,2) | x < 1 | y < 1
            continue;
        else
            R(x, y, :) = 0;
        end
    end  

M00 = 0.0;
for i = L(1)-P : (L(1)+W(1)+P),
            for j = L(2)-P : (L(2)+W(2)+P),
                if i > size(I,1) | j > size(I,2) | i < 1 | j < 1
                    continue;
                end
                M00 = M00 + double(I(i,j));
            end
end

M10 = 0.0;
for i = L(1)-P : (L(1)+W(1)+P),
            for j = L(2)-P : (L(2)+W(2)+P),
                if i > size(I,1) | j > size(I,2) | i < 1 | j < 1
                    continue;
                end
                M10 = M10 + i * double(I(i,j));
            end
end

M01 = 0.0;
for i = L(1)-P : (L(1)+W(1)+P),
            for j = L(2)-P : (L(2)+W(2)+P),
                if i > size(I,1) | j > size(I,2)| i < 1 | j < 1
                    continue;
                end              
                M01 = M01 + j * double(I(i,j));
            end
end

xc = round(M10 / M00);
yc = round(M01 / M00);

oldL = L;
L = [floor(xc - (W(1)/2)) floor(yc - (W(2)/2))];
     
        % Check threshold
        if abs(oldL(1)-L(1)) < T | abs(oldL(2)-L(2)) < T
            MeanConverging = 0;
        end
    end
 
    s = round(1.1 * sqrt(M00));
    W = [ s      floor(1.2*s) ];
    L = [floor(xc - (W(1)/2)) floor(yc - (W(2)/2))];
 
    % Output the centroid's coordinates
    disp(sprintf('%3i:   %3i, %3i', frame, xc, yc));
    Xpoints = [Xpoints xc];
    Ypoints = [Ypoints yc];
 
    % Superimpose plus sign on to centroid of hand.
    plus_sign_mask = [0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     1 1 1 1 1 1 1 1 1 1 1 1 1;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0;
                     0 0 0 0 0 0 1 0 0 0 0 0 0];
sizeM = size(plus_sign_mask);
for i = -floor(sizeM(1) / 2):floor(sizeM(1) / 2),
        for j = -floor(sizeM(2) / 2):floor(sizeM(2) / 2),
            if plus_sign_mask(i+1+floor(sizeM(1) / 2), j+1+floor(sizeM(2) / 2)) == 1
                R(i+xc, j+yc, :) = WHITE;
            end
        end
end
% ----------------------------------------------------------------------
    % Display the probability image.
    I = rgb2hsv(R);
    S = [];
    S(:,:,1) = I(:,:,1);
    S(:,:,2) = I(:,:,1);  
    S(:,:,3) = I(:,:,1);
% Extract the hue information
    avi = addframe(avi, S);
 
end
disp('AVI move parameters:');
avi = close(avi)
plot(Ypoints,Xpoints, 'go' , Ypoints, Xpoints);
axis([0 320 0 240]);
cd ..
disp('Done.');








Thursday 24 January 2013

Motion Tracking using Kalman Filter Matlab Code



clear all; close all; clc

%% Read video into MATLAB using aviread
video = aviread('Roof1.AVI');
nframes = length(video);

% Calculate the background image by averaging the first 5 images
temp = zeros(size(video(1).cdata));
[M,N] = size(temp(:,:,1));
for i = 1:10
    temp = double(video(i).cdata) + temp;
end
imbkg = temp/10;

% Initialization for Kalman Filtering
centroidx = zeros(nframes,1);
centroidy = zeros(nframes,1);
predicted = zeros(nframes,4);
actual = zeros(nframes,4);

% % Initialize the Kalman filter parameters
% R - measurement noise,
% H - transform from measure to state
% Q - system noise,
% P - the status covarince matrix
% A - state transform matrix

R=[[0.2845,0.0045]',[0.0045,0.0455]'];
H=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.01*eye(4);
P = 100*eye(4);
dt=1;
A=[[1,0,0,0]',[0,1,0,0]',[dt,0,1,0]',[0,dt,0,1]'];

% loop over all image frames in the video
kfinit = 0;
th = 38;
for i=1:nframes
  imshow(video(i).cdata);
  hold on
  imcurrent = double(video(i).cdata);
 
  % Calculate the difference image to extract pixels with more than 40(threshold) change
  diffimg = zeros(M,N);
  diffimg = (abs(imcurrent(:,:,1)-imbkg(:,:,1))>th) ...
      | (abs(imcurrent(:,:,2)-imbkg(:,:,2))>th) ...
      | (abs(imcurrent(:,:,3)-imbkg(:,:,3))>th);

  % Label the image and mark
  labelimg = bwlabel(diffimg,4);
  markimg = regionprops(labelimg,['basic']);
  [MM,NN] = size(markimg);

  % Do bubble sort (large to small) on regions in case there are more than 1
  % The largest region is the object (1st one)
  for nn = 1:MM
      if markimg(nn).Area > markimg(1).Area
          tmp = markimg(1);
          markimg(1)= markimg(nn);
          markimg(nn)= tmp;
      end
  end

  % Get the upper-left corner, the measurement centroid and bounding window size
  bb = markimg(1).BoundingBox;
  xcorner = bb(1);
  ycorner = bb(2);
  xwidth = bb(3);
  ywidth = bb(4);
  cc = markimg(1).Centroid;
  centroidx(i)= cc(1);
  centroidy(i)= cc(2);

  % Plot the rectangle of background subtraction algorithm -- blue
  hold on
  rectangle('Position',[xcorner ycorner xwidth ywidth],'EdgeColor','b');
  hold on
  plot(centroidx(i),centroidy(i), 'bx');

  % Kalman window size
  kalmanx = centroidx(i)- xcorner;
  kalmany = centroidy(i)- ycorner;

  if kfinit == 0
      % Initialize the predicted centroid and volocity
      predicted =[centroidx(i),centroidy(i),0,0]' ;
  else
      % Use the former state to predict the new centroid and volocity
      predicted = A*actual(i-1,:)';
  end
  kfinit = 1;

  Ppre = A*P*A' + Q;
  K = Ppre*H'/(H*Ppre*H'+R);
  actual(i,:) = (predicted + K*([centroidx(i),centroidy(i)]' - H*predicted))';
  P = (eye(4)-K*H)*Ppre;

  % Plot the tracking rectangle after Kalman filtering -- red
  hold on
  rectangle('Position',[(actual(i,1)-kalmanx) (actual(i,2)-kalmany) xwidth ywidth],'EdgeColor','r','LineWidth',1.5);
  hold on
  plot(actual(i,1),actual(i,2), 'rx','LineWidth',1.5);
  drawnow;
end


Extracting frames from a Video file Matlab Code


%%%%%%              Extracting frames from a Video file Matlab Code           %%%%%%%


clc;
close all;

% Open an sample avi file

filename = '.\003.AVI';
mov = MMREADER(filename);

% Output folder

outputFolder = fullfile(cd, 'frames');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end

%getting no of frames

numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1 : numberOfFrames

thisFrame = read(mov, frame);
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);
disp(progressIndication);



You might also like : Frames to AVI using Matlab Code

Moving Mouse Pointer using Matlab code


clc;
import java.awt.Robot;
cursor = Robot;

cursor.mouseMove(0, 0);
screensize = get(0, 'screensize');
aspect_ratio = screensize(3)/screensize(4);
for i = 1:screensize(4)
cursor.mouseMove(aspect_ratio*i, i);
pause(0.005);
end

Controlling Mouse event using Matlab code


import java.awt.Robot;
import java.awt.event.*;

cursor = Robot;

cursor.mousePress(InputEvent.BUTTON3_MASK);
cursor.mouseRelease(InputEvent.BUTTON3_MASK);

%%%%%%%%%%%%%   Note %%%%%%%%%%%

BUTTON1 for left click
BUTTON2 for both
BUTTON3 for right click