Skip to content

Instantly share code, notes, and snippets.

@ecmelkytz
Last active December 31, 2015 22:59
Show Gist options
  • Save ecmelkytz/8056748 to your computer and use it in GitHub Desktop.
Save ecmelkytz/8056748 to your computer and use it in GitHub Desktop.
function [image,mask] = chromaKey(background, foreground, key)
R = key(1);
G = key(2);
B = key(3);
imF = (foregrond(:,:,1)<=R) & (foreground(:,:,2)<=G) & (foreground(:,:,3)>B);
imFu = cast(~imF, 'uint8');
imFm = cast(imF, 'uint8');
imCk = cast(ones(size(background)),'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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment