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.
imBg = imread(‘background.jpg’);
imFg = imread(‘foreground.jpg’);
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;
mask = imF;
_____________________
This function can be easily implement as a simple application using MATLAB as below.
2010 December 3 - Posted by Thilina S. | Chroma Keying, Image Processing, MATLAB, Technology
21 Comments »
Leave a reply to Sang Nguyen Thanh Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
About
I am Thilina Sameera from Sri Lanka. I completed my undergraduate studies on Electronic and Telecommunication Engineering at the University of Moratuwa. Currently, I am working as a Lecturer at the Department of Electronic and Telecommunication Engineering, University of Moratuwa.
Categories
- වනජීවී – WildLife (3)
- Biomedical (2)
- C++ (2)
- Electronics (11)
- Circuit Design (1)
- FPGA (8)
- MIC1 IJVM (1)
- Image Processing (20)
- Chroma Keying (5)
- ITK (1)
- Java (2)
- MATLAB (27)
- SolidWorks (1)
- Technology (15)
- Ubuntu (1)
- VTK (1)
Ask from Thilina
Top Posts & Pages
Websites I follow
My pals
Online
-
Join 106 other subscribers
No Instagram images were found.
Blog Hits
- 353,807 hits
Visitors
Top Of Blogs
Twitter
My Networked Blogs
Goodreads
Nice work malli…
Appreciate your efforts…
Comment by Chakshika | 2010 December 4 |
Hey malli nice work. There aren’t that much of good blogs for Image Processing. Keep it up
Comment by Dinesh | 2010 December 5 |
What is “key”?
Comment by Sang Nguyen Thanh | 2011 May 24 |
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,
check the link
Comment by Thilina S. | 2011 May 24 |
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.
imBg = imread (‘background.jpg’);
imFg = imread (‘foreground.jpg’);
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 |
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 |
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 |
can you send me the code?
Comment by Ngoc Dinh Le | 2014 May 10 |
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 |
Just check on https://thilinasameera.wordpress.com/2011/01/10/chroma-keying-matlab-implementation-4-0/
I modified all possible errors which comes with this model. How ever are you sure about the blue back ground is [0,0 255] ??
Comment by Thilina S. | 2011 July 1 |
I download the foreground.jpg and background.jpg images from your blog.
Comment by Sang Nguyen Thanh | 2011 July 2 |
It must work then, since I just paste my test code..!!
Comment by Thilina S. | 2011 July 2 |
What is the “key” function that you used in your test code?
Comment by Sang Nguyen Thanh | 2011 July 3 |
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 |
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 |
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 |
Hi,
Try it using key = [128 172 128], you can have much better results.
Comment by Thilina S. | 2011 July 7 |
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 |
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 |
Oh, I see.
Have a nice day!
Comment by Sang Nguyen Thanh | 2011 July 8 |
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 |