chroma key

Anybody know a way i can implement chroma keying in c++. Like removing a green screen like they do in movies.

but i have an object in front of a green screen and i need to remove the background from the foreground object.

i had this planned algorithm but don't know if it will work

matrix image

image = image file.jpg

search through each column of the matrix that matches the range of the background color and turn it black and the rest to white
do this for each row.

convolute this mask with the original image which should get rid of the background i think.

but one problem i have is how can i find all the RGB values of each pixel in the image.

[(10,20,32),(200,20,0),....]

so i know the range the background color has. if only it was easy as plot RGB graph of the image and create a cluster for the range colors that matches the background with simple code but it is quite harder than i thought.


Well, like you describe, you essentially need to create an alpha mask based on per-pixel comparison. So you must examine every pixel in the image, and compare that with your given range. If the pixel is determined as part of the background (based on the range comparison), set it as black in the new image (the mask). If not, set it as white.

After you do that, you may want to go over the resultant image and soften the mask borders (determine the white pixels that border with black pixels, and give them a grey percentage so that the edges of the mask have a slight feather (the degree of feathering could also be an input of your algorithm).

The resultant image could be used as an alpha mask applied to the original image or frame.
Last edited on
Nice one mate, thanks so how would i apply a mask to an original image, multiply, convolute or add or subtract.
Essentially the value of your alpha mask determines the ammount to which your original image's pixel will retain it's value. A white pixel in the mask would retain the full color (originalPixelValue * 1.0), a black pixel would not retain any color (originalPixelValue * 0.0), a gray pixel would get 50% of it's original value (originalPixelValue * 0.5), etc. . You could (pre)multiply the alpha, and this will of course darken the areas where the alpha has a non-white value, but you need the alpha channel to distinguish the pixels that were affected by the mask.

For the alpha mask to work, you need to blend the pixels of the image that is below your alpha-masked image, so that the result of the blending is determined based on the mask.

Also see this for the difference between straight and premultiplied alpha: http://www.cgdirector.com/quick-tip-straight-alpha-vs-premultiplied-alpha/

And see this about using convolution with pre-multiplied alpha: http://stackoverflow.com/questions/5052261/image-convolution-with-pre-multiplied-alpha
Now i am trying to implement it but i can't seem to loop through the matrix values.

i tried something like

Mat img=(imagefile);
int pixelvalue [3];

for(i=1;i<img.size();i++)
{
for(j=1;j<img.size();j++)
pixelvalue(1)=img.value % or what function available to analyze the current pixel value.

i want to loop through each row of the matrix incrementing the column index everytime but there is the problem with the matrix being an int not being able to convert to a const char.

i want to find the range of rgb values in the image that is stored in a matrix form so i can generate the normal distribution model of the background but i can't even get that far.

why is it not as easy as matlab you can loop through matrix elements.
Last edited on
Topic archived. No new replies allowed.