Monday, April 12, 2010

Mirror your Video (Inverting video using Matlab)

Have you ever seen Saurav Ganguly batting right handed or Sachin Tendulkar batting left handed.
Well just watch this:-



You might be wondering when did sachin play such innings.
Its all mirrored.
The big question remains how to do this.
Ans.
1)Using Matlab
2)Using Java
3)Many other software's are available like photoshop
But i'll rather prefer Matlab because it is taught in my home subject(ECE)

First let me tell you what all you can do with it:-
1)Mirror a Video(or image) either horizontally or vertically or both ways.
2)Face warping(you might have seen funny mirrors).
3)Creating edges leaving behind the background.
4)Creating black and white image from coloured ones and vice versa.
and much more.

Starting with mirroring of a video(actually it is mirroring of each frame so if it canb aslo be done for an image ).

First thing i expect from you is to have a basic knowledge of matlab + you have matlab installed on your PC
(rest dont worry i'll be posting for java soon).

For reading files of every format you need to download a reader package (because matlab provides default reader of avi videos only) .

Download it from here:-
http://www.mathworks.com/matlabcentral/fileexchange/8028-mmread?controller=file_infos&download=true

Extract it to a folder.
Now create a M-file inside the same folder(change directory if need be).
Copy the video in same folder.
Copy video name along with its extension(ex:-video.avi)
note:-prefer avi video
Now write the following code:-
%For Horizontal mirror
%change video.avi with name of video
s=mmread('video.avi');   
fo=avifile('Vid.avi','fps',25);

for x1=1:50
%put no of frames in place of 50  
y1=s.frames(x1).cdata;

fr_size = size(y1);
width = fr_size(2);
height = fr_size(1);

for i1=1:height
    for j1=1:width
        xx=i1;
        yy=width-j1+1;
                    z1(i1,j1,1)=y1(xx,yy,1);
                     z1(i1,j1,2)=y1(xx,yy,2);
                     z1(i1,j1,3)=y1(xx,yy,3);
      
    end;
end;
sfo=addframe(sfo,z1);

end;
sfo=close(sfo);


---------------------------------------------
%For Vertical mirror
%change video.avi with name of video
s=mmread('video.avi');   
fo=avifile('Vid.avi','fps',25);

for x1=1:50
%put no of frames in place of 50  
y1=s.frames(x1).cdata;

fr_size = size(y1);
width = fr_size(2);
height = fr_size(1);

for i1=1:height
    for j1=1:width
        xx=height-i1+1;
        yy=j1;
                    z1(i1,j1,1)=y1(xx,yy,1);
                     z1(i1,j1,2)=y1(xx,yy,2);
                     z1(i1,j1,3)=y1(xx,yy,3);
      
    end;
end;
sfo=addframe(sfo,z1);

end;
sfo=close(sfo);

No comments:

Post a Comment

customised by Vaibhav