Something used heaps in the film industry is the "Greenscreen" I thought I would take a quick look at how to make a greenscreen that works fast enough to run on a live webcam stream. And infact one that works with any coloured background. It has many many limitations, but was a fun experiment! To run this example you will need OpenCV with the SWIG Python bindings installed. You can get this code from my SVN repository here.
Firstly the background I started with:
Adding an object to the scene, and carrying out back ground subtraction:
So anyhow the code:
Some objects don't work so well. Output is very noisy.
Now there is nothing to say a green background must be used at all, so what about just using my desk as the background:
And adding my highlighter to the desk...
And a simple shot showing the final scene to show I didn't cheat.
And there you have it.
Firstly the background I started with:
Adding an object to the scene, and carrying out back ground subtraction:
So anyhow the code:
#!/usr/bin/env python from VideoCapturePlayer import VideoCapturePlayer as VCP from opencv import cv def threshold_image(image, n=[]): """Record the first 5 images to get a background, then diff current frame with the last saved frame. """ if len(n) < 5: # n[4] will be our background # First capture a few images n.append(cv.cvCloneMat(image)) if len(n) == 5: # last time here # could do averaging here. pass return image original = n[4] differenceImage = cv.cvCloneMat( image ) cv.cvAbsDiff( image, original, differenceImage ) thresholdValue = 50 # 32 cv.cvThreshold( differenceImage, differenceImage, thresholdValue, 255, cv.CV_THRESH_BINARY ) cv.cvSmooth(differenceImage, differenceImage, cv.CV_MEDIAN, 15) gray = cv.cvCreateImage( cv.cvGetSize(differenceImage), 8, 1 ) cv.cvCvtColor( differenceImage, gray, cv.CV_BGR2GRAY ) result = cv.cvCloneMat( image) cv.cvSetZero(result) cv.cvAnd(image,image, result, gray) return result if __name__ == "__main__": title = "Background Subtraction Output" VCP(threshold_image, title).main()
Some objects don't work so well. Output is very noisy.
Now there is nothing to say a green background must be used at all, so what about just using my desk as the background:
And adding my highlighter to the desk...
And a simple shot showing the final scene to show I didn't cheat.
And there you have it.