 Search

Chromakeying in Matlab The aim of this code example is to extract the green background, also called chroma key, and replace it with the background from a different image. In fact what we’re doing is keying out a black and white mask from the green component of the foreground image and using it to mask any other background image.

```% Read any two images for background (bg) and foreground (fg)

% extract 3 different color channels
% and luminance matrix
fgR = fg(:,:,1);
fgG = fg(:,:,2);
fgB = fg(:,:,3);
fgY = 0.3*fgR+0.59*fgG+0.11*fgB;

% subtract luminance from green
fgG_Y=mat2gray(fgG-fgY);

% Calculate and plot histogram
histo=hist(fgG_Y(:),256);
plot([0:255],histo);

% Define and normalise a reasonable threshold to try to avoid the spills
% too high will trim the image
% too low will let some green through the borders
thres = 80/255;

% fg(G-Y) is lower than the threshold

% finally, save channel by channel the foreground where mask = 1
imshow(mat2gray(final));

```

The result is something like this:

Let’s observe the histogram:

We can clearly distinguish 3 entities from it: the person, the shadow cast and the background.

By placing a threshold at a value of 150 we would be extracting the person only. If we want to keep the shadow we should plan a threshold at about 200. So the answer is to create two different masks this time to be able to distinguish between 3 different areas: foreground, shadow, background.

Foreground

– Mask 1 is active (= 1)
This means it’s getting it’s source from the foreground image.

– Mask 1 is not active (= 0)
This means it’s getting it’s source from the background image.
– Mask 2 is active at 0.5
This means it will only be half as bright as the original image.

Background

– Mask 1 is not active (= 0)
This means it’s getting it’s source from the background image.
– Mask 2 is not active (= 0=
So that it doesn’t interfere with the shadow.

```bg=double(imread('mapa2.tif'));
fgR = fg(:,:,1);
fgG = fg(:,:,2);
fgB = fg(:,:,3);
fgY = 0.3*fgR+0.59*fgG+0.11*fgB;
fgG_Y=mat2gray(fgG-fgY);
histo=hist(fgG_Y(:),256);
plot([0:255],histo);
thres1 = 150/255;
thres2 = 200/255;
imshow(mat2gray(final));

```

For a more realistic effect we would want to soften out the shadow a little bit. This is simply done by averaging the shadow mask.

```H=fspecial(‘average’, [20,20]);
```

This is the result: Author: Bea Cabrera

Freelance Filmmaker with a passion for big cities, snowboard, cinema and a weakness for the smell of freshly ground coffee. Engineer & Graphic Designer in a previous life, loving and living both: art and technology.

One Reply to “Chromakeying in Matlab”

1. 