'how to develop background removal using chroma keying in opencv

I need to develop an algorithm to remove a background from the foreground image. I would like to use chroma keying. This will be done on a live camera stream and I need to do it using OpenCV in C++.

The algorithm I thought would be something like:

  1. Capture frame from webcam into matrix
  2. Compute 3d rgb space graph
  3. The points around a certain value e.g. (10,255,10) for green background.
  4. Convert all points around green region to 0.
  5. And the rest to 1.
  6. Multiply that with the original image (convolution)
  7. Should get rid of background.

I would like some help on a method to remove a plain color background from foreground image without the color from the foreground image being removed.
Is there like a special type of crop function to remove background?



Solution 1:[1]

I'm using OpenCV from Python. Suppose you followed a basic tutorial and have the frame from the camera and have a background image. A single line of code should do the trick with the numpy's where function.

outputImage = np.where(frame == (10,255,10), background, frame)

Now the outputImage contains the original frame where it wasn't green and your background image where it was green. As you said, you should use some tolerance.

Solution 2:[2]

I'm using OpenCV in C++, but I don 't fully understand what you want, @Seif (sorry for my poor English). Do you want to replace the green in the background with the colour of the foreground, and you're stuck at "Multiply that with the original image (convolution)"? Your algorithm is terribly true, but you must put the green to angle, circle... and implement them, there are many algorithms to do it (I'm doing it like that), otherwise, I think it's impossible except changing any green pixel to a similar pixel of your foreground.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 b_m
Solution 2 SwiftsNamesake