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

lørdag den 8. maj 2010

Crossfade two videos clips from a command line (CLI or term)

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

Please start by scrolling all the way down to the bottom of this post and watch the little video that is there. It will make the understanding of all this much easier.

This script can crossfade two videos from a command promote (CLI or term) in UNIX / Linux / mac. With crossfade, I mean that the first movie clip slowly tones into the next.

This command produced the example videoclip clip at the bottom of this post
./crossfade clip-a.mpg clip-b.mpg 8

I have put out the script on my homepage as a pure text file you can view it. And copy / past it to your edit or you could right-click the link and choose to download (and rename it) to your computer.

Here is the link to the script:

http://mimoart.ooz.dk/crossfade.text

HOW TO USE THIS SCRIP?

There is, as always with computers several ways to do every single thing. In this little walk-through I have chosen a one way for the sake of giving a practical example.

1st Check that you have FFmpeg and Netpbm installed (and that you are on a UNIX platform (Linux, Mac OS X, Solaris, Free BSD, etc.))

2nd Create a working directory let's just here as an example call it:

/home/mikkel/project/mymove/crossfade/

to this directory copy the two video clips that you want to crossfade (they should be in mpg format and equal in height and spread)

3rd copy / paste the text / script from the link her above to your editor and save it (pure text) as a file called "crossfade" in the same catalog.

4th Open a terminal (Xterm, rxvt, Aterm of your choice) and "cd" to the above selected directory

cd /home/mikkel/project/mymove/crossfade/

5th Make the script executable

type in the terminal window:

chmod +x crossfade

6th Choose how slow you want the transition to go. It is done with the figure at the end of command (the third argument)

in my example the figure is 8

./crossfade clip-a.mpg clip-b.mpg 8

rule of thumb is that each unit is a little more than a second. In my example here I have chosen 8 which gives me about 9 seconds in transition, you can choose a maximum of 50 which gives about a minute fading

7th Yes it is just to execute the command. In my example i write:

./crossfade clip-a.mpg clip-b.mpg 8

in the terminal window (the script can actually run from a non-X-environment as well as an X-environment terminal).

I have also made other script that uses ffmpeg and netpbm that performs video producing and video editing tasks. E. g. one to zoom in an image. One to rotate a single frame as if it was on a turntable and spun around. One for fading video clip to black. One for enhancing contrast in a very dark image etc. I'd love to send these scripts and description if anyone wants. Just write to me

contact:

http://mimoart.ooz.dk/contact.htm

Below here is notes to explain in further details.

Dependencies:
To the script there are two programs to be installed on your computer. FFmpeg and Netpbm. Netpbm is very basic software and will almost always available on your Unix / Linux machine or be easy to install. FFmpeg is planned to be compiled befor installation on your system, but it can also be found in the pre compiled version at most repositories.

Advantages of this way of crossfading video clips.

- If you have a netbook like for instance Asus eee it is much easier to work with a command then with drag and drop of the mouse because of the small screen size.

- The script can run on a very law machine without it going dead. It just takes a little longer.

- Sometimes it's more comfortable and more precise to work with text commands instead of having to sit and fiddle with the mouse

- I am use to use FFmpeg and Netpbm so for me it is a big advantage that I am not going to teach me a graphic video editing program to create this effect.

- I've tried to do the same with Kdenlive (a good graphic video editing program) on the same PC and it took much longer to render than my script takes to process. So it is faster

- It gives a much better insight into what is going on in process.


Preparation of film clips to be crossfades:
Film clips must be in mpg format which is easily done with Ffmpeg such

ffmpeg -i orig.avi -sameq clip-a.mpg

and they must have equal size for example 320 x 240

which is also easy to do with FFmpeg for example:

ffmpeg -i a.mpg -sameq -s qvga clip-a.mpg

where "QVGA" is size declaration. "QVGA" stands for the size 320 x 240

Sound
I have in this process not crossfaded sound too. I have worked with images alone and put the sound on at the end but if some want I can help to add audio processing into the script.

What happens:
The actual transition takes place at the video clips transformed to single frames. The "pamcomp" program from Netpbm takes care of merging frames two and two with a transparency mask that determines how much one image should be visible through the other. I use 30 levels of fading if it is to be a long fading for example 20 seconds, these thirty levels just spread over several frames. When this overlay process is finished for all the selected frames FFmpeg turns the frames back to mpg video again.

Before this transition process can take place some lengths of video clips must be measured. If the video clips are long (more than one minuet) it should be divided before it is write out as single frames so there will not be too many unnecessary frames in the process. This cutting is done with ffmpeg -ss and -t command and general Unix shell commands sed, expr etc. Maybe some of you will wonder why I let the script convert video clip from mpg to avi before it is measured. That is because the mpg format is not easy to measure in because it doesn't contains meta data, eg the duration and the like. avi does contain this data.

Space:
It is clear that this script requires some space on your hard disk. It writes typically around 3.000 single frames in to png images and that take up some space. In most cases 2 GB is sufficient.