Thilina's Blog

I might be wrong, but…

Chroma Keying MATLAB Implementation 2.0

In my previous article on chroma key implementation on MATLAB (Chroma Keying MATLAB Implementation 1.0), I explained how to key two images using a simple image segmentation method called ‘Image thresholding’. In this article I’ll try to extend above result to key a foreground video with a background image. Please note that this implementation can be use to process a video after saving it or post processing manner.

During this implementation I used an *.avi video file ‘movie.avi’, which has the chroma keying properties; a video taken in front of a blue screen as my foreground video and ‘background.jpg’ as my background image.

clip_image002 clip_image004

Opening a movie to MATLAB workspace can done by using the command ‘mmreader ()’. The command creates a multimedia reader object on MATLAB workspace. You can read more on this command by typing ‘doc mmreader’ on MATLAB command prompt.


xyloObj = mmreader(‘in_clip.avi’);

im = imread(‘background.jpg’);

Properties of the movie object such as number of frames, video size (height and width) can be obtained by following commands.

nFrames = xyloObj.NumberOfFrames;

vidHeight = xyloObj.Height;

vidWidth = xyloObj.Width;

For applying chroma keying we need to convert the movie object in to a series of images, so that we can directly apply the chroma key method we obtained in my previous article. In-order to perform this action we have to first allocate and movie structure which is a series of images and then we can allocate each frame of the movie to this data structure. To saving the computational power we can perform the chroma key procedure at this stage because the chroma keying and conversion of movie object both are frame based procedure.

mov(1:nFrames) = struct(‘cdata’, zeros(vidHeight, vidWidth, 3, ‘uint8’), ‘colormap’, []);

for k = 1 : nFrames

im_frame = read(xyloObj, k);

         mov(k).cdata = chromaKey(im, im_frame, key,’b’);

end

Playing the movie can do by using the command below.

 hf = figure;

set(hf, ‘position’, [150 150 vidWidth vidHeight]);

movie(hf, mov, 1, xyloObj.FrameRate);

Saving the movie is also a very easy operation in MATLAB.

movie2avi(mov,file_name);

Complete test code for the procedure described above is as below.

___________________________________________________________________________________________________

xyloObj = mmreader(‘in_clip.avi’);

nFrames = xyloObj.NumberOfFrames;

vidHeight = xyloObj.Height;

vidWidth = xyloObj.Width;

im = imread(‘background.jpg’);

mov(1:nFrames) = struct(‘cdata’,

zeros(vidHeight,vidWidth,3,’uint8′),‘colormap’,[]);

for k = 1 : nFrames

      im_frame = read(xyloObj, k);

      mov(k).cdata = chromaKey(im, im_frame,key,’b’);

end

hf = figure;

set(hf, ‘position’, [150 150 vidWidth vidHeight]);

movie(hf, mov, 1, xyloObj.FrameRate);

___________________________________________________________________________________________________

Result obtained from this method and a small application program which can use for this kind of operation is as below.

clip_image006

Resulting video is as below.

2010 December 16 - Posted by | Chroma Keying, Image Processing, MATLAB, Technology

9 Comments »

  1. Ela ela😀

    Comment by Rukmal Fernando | 2010 December 16 | Reply

  2. wow. maxxa machn.
    If you can implement this on an FPGA, you can extend this to real time videos too.

    Comment by Shamika | 2010 December 16 | Reply

  3. Keep rolling. Nice work meeya😀

    Comment by Harindu | 2010 December 16 | Reply

  4. nice work machaaann, proud of u….:D😀❤

    Comment by Upul | 2010 December 16 | Reply

  5. nice work aiya…but man nam mewa danne nathooooo….

    Comment by sameera dayanthini | 2010 December 16 | Reply

  6. Nice Work Malli..
    Keep it up!!!🙂

    Comment by Chakshika | 2010 December 16 | Reply

  7. sira machan

    Comment by Gosala | 2010 December 16 | Reply

  8. nice work.. keep it up !

    Comment by කණිෂ්ක | 2010 December 16 | Reply

  9. ela ela maxa, nice work !!

    Comment by Buddhi | 2010 December 16 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: