# Thilina's Blog

## Chroma Keying MATLAB Implementation 4.0

Hopefully this will be my final article on chroma keying, in this article I will try to address the key disadvantages and faults identified in my previous posts by myself. In my previous chroma keying functions I only segmented a region which has the luminance level of [x, 255] or [0, x]. But on working with function I realised that some colour details in images also has segmented out as a background mask.

With this experience I modified my function to segment out a small colour band which is most likely acts as the background of the foreground image. What I have done is obtain the mean colour (say mR, mG, mB) and its maximum deviation (say sR, sG, sB) on user defined points on the foreground image and then segment the foreground image using the colour band obtained from the above result. Now the modified segemented colour band will be [mR-sR:mR+sR, mG-sG:mG+sG, mB-sB:mB+Sb] which will results in less colour distortion in the resulting image.

For obtaining the colour details I added new code lines for the colour-picker function as below. This lines are added immediately after the line following the obtaining the mean colour of the pixels.

```
for i = 1:size(x,1)
if((0<x(i))&&(x(i)<=r)&&(0<y(i))&&(y(i)<=c))
ptr = (pxClr-[im(x(i),y(i),1) im(x(i),y(i),2) im(x(i),y(i),3)]);
vars(:,1) = max(vars(:,1), abs(ptr(:,1)));
vars(:,2) = max(vars(:,2), abs(ptr(:,2)));
vars(:,3) = max(vars(:,3), abs(ptr(:,3)));
end
end

```

With this I got the maximum deviation of the each colour with its mean colour in the region which we needs to segment out. Finally I modified the chromakey function with its new properties as below.

```function [image mask] = chromaKey2(background, foreground,key,vars)
R = key(1);
G = key(2);
B = key(3);
vR = vars(1);
vG = vars(2);
vB = vars(3);
imFR = ((R-vR)<=foreground(:,:,1))&(foreground(:,:,1)<=(R+vR));
imFG = ((G-vG)<=foreground(:,:,2))&(foreground(:,:,2)<=(G+vG));
imFB = ((B-vB)<=foreground(:,:,3))&(foreground(:,:,3)<=(B+vB));
imF = imFR&imFG&imFB;
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;
```

With this function I obtained much better results than my previous attempts. A video of the stand-alone application developed by above functions are as below.

2011 January 10 - Posted by | Chroma Keying, Image Processing, MATLAB