# Thilina's Blog

## Chroma Keying MATLAB Implementation 1.0

In my previous article I discussed the major concepts on chroma keying. In this article I will discuss a method for use chroma keying principles to key two images in to a single image. For the implementation we need two images which can use as background and foreground images. I select ‘background.jpg’ and ‘foreground.jpg’ as my background and foreground images for the keying operation. Note that the foreground is the image which you stay in front of that special reflecting screen.

We can add those two images in to MATLAB workspace by using following commands.

As we can see the foreground major colour is blue, therefore we have to mask the corresponding blue colour from the image. In order to perform this operation, we need to create a mask with colour thresholds. The ‘key’ variable contains the corresponding RGB colours for the above colour filtering operation.

R = key(1); G = key(2); B = key(3);

imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)<=G)&(foreground(:,:,3)>B);

Filtered mask will be as below. By using this logical mask we can perform the keying operation, which is to replace the white pixels with background image and replace the black pixels with foreground image.

Since we are using colour images, we have to perform the mask to all 3 RGB matrices in-order to obtain the keyed colour image. A casting operation must apply for the logical image since the data operations in MATLAB can only perform in same data type.

imFu = cast(~imF,’uint8′);

imFm = cast(imF,’uint8′);

imCk(:,:,1) = background(:,:,1).*imFm + imFu.*foreground(:,:,1);

imCk(:,:,2) = background(:,:,2).*imFm + imFu.*foreground(:,:,2);

imCk(:,:,3) = background(:,:,3).*imFm + imFu.*foreground(:,:,3);

With these operations finalised result will be as below.

Complete code for the operation will be as below. This code can directly use as a MATLAB function.

_____________________

function [image mask] = chromaKey(background, foreground,key,major)

R = key(1);                   G = key(2);               B = key(3);

if(major==’b’)

imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)<=G)&(foreground(:,:,3)>B);

else

imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)>G)&(foreground(:,:,3)<=B);

end

imFu = cast(~imF,’uint8′);

imFm = cast(imF,’uint8′);

imCk(:,:,1) = background(:,:,1).*imFm + imFu.*foreground(:,:,1);

imCk(:,:,2) = background(:,:,2).*imFm + imFu.*foreground(:,:,2);

imCk(:,:,3) = background(:,:,3).*imFm + imFu.*foreground(:,:,3);

image = imCk;

_____________________

This function can be easily implement as a simple application using MATLAB as below.

2010 December 3 -

1. Nice work malli…

Comment by Chakshika | 2010 December 4 | Reply

2. Hey malli nice work. There aren’t that much of good blogs for Image Processing. Keep it up

Comment by Dinesh | 2010 December 5 | Reply

3. What is “key”?

Comment by Sang Nguyen Thanh | 2011 May 24 | Reply

• Hi,
The ‘key’ is the background colour of foreground image or the average colour of the image segment which we needed to mask out. Such as key = [0,255,0]; (RGB 8bit)
This colour values can be generated using simple colour picker program on MATLAB,

Comment by Thilina S. | 2011 May 24 | Reply

• Because the background color of “foreground.jpg” is blue, I try to use the key = [0,0,255] to mask out. But the result don’t expect.

key = [0,0,255];
R = key(1);
G = key(2);
B = key(3);
imF = (imFg(:,:,1) <= R) & (imFg(:,:,2) B);
imFu = cast(~imF,’uint8′);
imFm = cast(imF,’uint8′);
imCk(:,:,1) = imBg(:,:,1).*imFm + imFu.*imFg(:,:,1);
imCk(:,:,2) = imBg(:,:,2).*imFm + imFu.*imFg(:,:,2);
imCk(:,:,3) = imBg(:,:,3).*imFm + imFu.*imFg(:,:,3);

Comment by Sang Nguyen Thanh | 2011 July 1 | Reply

• Try key as
key = [0,0,254];
Since the masking is used as

imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)B);
B = 255 will do nothing.
Thank you very much for the comment..!

Comment by Thilina S. | 2011 July 1 | Reply

4. Hi Thilina,
imF = (imFg(:,:,1) <= R) & (imFg(:,:,2) B);
I have already tried with key = [0,0,254] but the result is still unchange.
Please fix it again. Thanks so much!

Comment by Sang Nguyen Thanh | 2011 July 1 | Reply

5. I don’t know why the imF function is cut a segment.
The full of imF is:
imF = (imFg(:,:,1) <= R) & (imFg(:,:,2) B);

Comment by Sang Nguyen Thanh | 2011 July 1 | Reply

Comment by Sang Nguyen Thanh | 2011 July 2 | Reply

• It must work then, since I just paste my test code..!!

```function [image mask] = chromaKey(background, foreground,key,major)
R = key(1);
G = key(2);
B = key(3);
if(major=='b')
imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)<=G)&(foreground(:,:,3)>B);
else
imF = (foreground(:,:,1)<=R)&(foreground(:,:,2)>G)&(foreground(:,:,3)<=B);
end
imFu = cast(~imF,'uint8');
imFm = cast(imF,'uint8');
imCk(:,:,1) = background(:,:,1).*imFm + imFu.*foreground(:,:,1);
imCk(:,:,2) = background(:,:,2).*imFm + imFu.*foreground(:,:,2);
imCk(:,:,3) = background(:,:,3).*imFm + imFu.*foreground(:,:,3);

image = imCk;

```

Comment by Thilina S. | 2011 July 2 | Reply

7. What is the “key” function that you used in your test code?

Comment by Sang Nguyen Thanh | 2011 July 3 | Reply

• I can’t remember exactly, but you can pick the background point using getpts() command and see its colour. Then try it with some little variations.

Comment by Thilina S. | 2011 July 3 | Reply

8. I use getpts() command and get coordinate of a point on image. How to see its color of this point?

Comment by Sang Nguyen Thanh | 2011 July 6 | Reply

9. I only begin with Processing Image on Matlab.
For above example, I use getrect (rect2=getrect) command to get a color region of background in foreground.jpg image and S = imFg(rect2(2): rect2(2)+rect2(4), rect2(1):rect2(1)+rect2(3),:) to get color value of R:G:B of this region.
The key = [254 254 175] is chose.
The result, however, is not good as your result.

Comment by Sang Nguyen Thanh | 2011 July 7 | Reply

• Hi,
Try it using key = [128 172 128], you can have much better results.

Comment by Thilina S. | 2011 July 7 | Reply

10. Thanks Thilina,
I don’t know how do you find the above key?
I use three “for” commands and use “imwrite” command to save image file. I observe these images by your eyes and I also find key value [130 172 120].

Comment by Sang Nguyen Thanh | 2011 July 7 | Reply

• Hi,
As you can see in the image at the bottom, i designed a GUI to run my chroma key function. By using that GUI i found above key. By the way what exactly are you trying to implement ?

Comment by Thilina S. | 2011 July 7 | Reply

11. Oh, I see.
Have a nice day!

Comment by Sang Nguyen Thanh | 2011 July 8 | Reply

12. Hi, Thilna.
I’m new about using MATLAB and I’m learning and trying to run your’s ChromaKey code but it keeps error.
I’m copy your’s code as chromaKey,m then try to run it.
and this is what’s it showed to me.

??? Input argument “key” is undefined.

Error in ==> chromaKey at 3
R = key(1); G = key(2); B =
key(3);

Error in ==> run at 57
evalin(‘caller’, [s ‘;’]);

Not sure you still around here or not.
Help would be great, Thanks.

Comment by Frightz | 2012 March 5 | Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.