how to develop background removal using chroma keying in opencv

Seif Sharif picture Seif Sharif · Feb 18, 2013 · Viewed 10.6k times · Source

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?

Answer

b_m picture b_m · Feb 18, 2013

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.