My Departement Of Nerdcore Enthusiasm

Tutorials and the like. Things which I have committed myself to. Often linux/UNIX-related.

onsdag den 26. maj 2010

Anyone who wants to play with blue screen/green screen?

Chroma-key with netpbm

Attention:
The video that I made these technical solutions for is now finished and can be viewed on youtube http://www.youtube.com/watch?v=uRXsp7LZaDQ


"Chroma key compositing (or chroma keying) is a technique for compositing two images or frames together in which a color (or a small color range) from one image is removed (or made transparent), revealing another image behind it."

"It is commonly used for weather forecast broadcasts, wherein the presenter appears to be standing in front of a large map, but in the studio it is actually a large blue or green background"

en.wikipedia.org

http://en.wikipedia.org/wiki/Chroma_key




I made this because someone on ffmpeg list asked how it could be done and I it found interesting task. I work here with the low efficacy of a single image. Then I/we could go on the road to work on how to make it working with a fifo pipe so we can apply the effect on an entire video clip. (And you could also extend it to be in a moving background. A second video stream in the background).






This script requires some preparation. First and foremost, you must find your key color. The color you want to replace with an other image. Then you must produce the background image that will replace the background of the original image.

Find your key color.. the color that best represent background color. I divided the key color in to actually being tree colors, three nuances of the key color you could say, because such a back surface actually varies quit a lot in nuances due to light and shadows.

So you open your image/video frame in an imaging editing program such as eg Gimp. Use the color picker tool. Look at the image and see if you can find the lightest spot on that background surface and click it with the color picker tool. Than look at the color pallet and read the HTML code for the color.

This code looks for example like this.

5d67ac

for netpbm to understand it it must be rewritten so that it looks like this:

rgb:5d/67/ac

Now you'll find what looks as the darkest spot on that background surface and get the HTML code for that. And finally you find a spot on that background surface that could represent something in between the darkest and the lightest spot.



this three values have to be set in to the script on the place as you see here below.

Produce the new background image.

The new background image should have the same format (height and spread) as your original video/frame/image so it is a good idea to open your frame/image in gimp and then copy paste the new background image on top of the original image and then drag around with it and scaled up and down until the new background image is quite right in relation to the original image and then save it in the PNM format with a new name.

You can play around with the three key color and with the closeness percentage. The closeness percentages is sat at the top of the script as you can see when you look at the script text here below. It is the number that represents how tolerant the color selection should be a high percentages will include more color nuances and ad a low percentages will include less color nuances so if you find that the background "eats" up parts of the image that should not be part of the background you could choose i lower number and if you find that the background is insufficiently covered you could choose a higher number.

How to use the small script?:

1th Copy paste the text/script below to your editor.

2th Insert your three key color values.

3th Save it (pure text) as a file called "chroma" in your working directory, where you also have the image that you work with and the new background image that you have prepared.

4th Open a terminal (Xterm, rxvt, Aterm of your choice) in your working directory.


5th Make the script executable

type in the terminal window:

chmod +x chroma

6th execute the script

example:

./chroma bluebox.pnm substit.pnm

where bluebox.pnm is the name of my original image and substit.pnm is the name of the file that contains the background image that i wont instead of the background.

The script:

#!/bin/sh

#script needs two arguments of the original frame/picture and

#picture with the new background. Both in PNM format, eg

#./Chroma bluebox.pnm substit.pnm

#The three deferent levels of the key color. You have to fill in your values here
dark=rgb:1d/3b/77
middle=rgb:53/61/a2
light=rgb:7a/87/cd

closeness=13

ppmchange -closeness=$closeness $dark $middle $middle $middle $light $middle $1 >ret0tmp.pnm;
ppmcolormask $middle ret0tmp.pnm >alpha.pbm;
pamcomp -alpha=alpha.pbm $1 $2 >changed.pnm;
echo "the result is in the image file: changed.pnm"


With love

Mikkel

Ingen kommentarer:

Send en kommentar