Sunday, February 21, 2010

Video Spatial Transformation - Rotation

To rotate an image in Matlab, use the imrotate function. imrotate accepts two primary arguments:
  • The image to be rotated
  • The rotation angle

You specify the rotation angle in degrees. If you specify a positive value, imrotate rotates the image counterclockwise; if you specify a negative value, imrotate rotates the image clockwise.

This example rotates the image I 35 degrees in the counterclockwise direction.
J = imrotate(I,35);

As optional arguments to imrotate, you can also specify
  • The interpolation method
  • The size of the output image
In Video:

fin = 'rawVideo.avi';
fout = 'test.avi';
fileinfo = aviinfo(fin);
nframes = fileinfo.NumFrames;
aviobj = avifile(fout, 'compression', 'none', 'fps',fileinfo.FramesPerSecond);
for i = 1:20
%Read frames from input video
mov_in = aviread(fin,i);
im_in = frame2im(mov_in);
%Do processing on each frame of the video
%----------------------------------------------------------------------
%In this example - Image Rotation
im_out = imrotate(im_in,35);
%----------------------------------------------------------------------
%Write frames to output video
frm = im2frame(im_out);
aviobj = addframe(aviobj,frm);
%i %Just to display frame number
end;
%Don't forget to close output file
aviobj = close(aviobj);

msgbox('DONE');
return;

The Result:

No comments:

Post a Comment