Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Green screen chroma-key using ImageMagick

I had been searching for good algorithm for green screen chroma key using ImageMagick, but no satisfactory answer so far.

I would like to explore a simple method of using range selection along the HSV/HSB color space (similar to -fuzz) to select the green color I want and make them transparent. The -fuzz seems to apply only in RGB space, which is not desired.

Can anyone teach me how to select color with seperate range for H, S, V component, for example, 115-125 degree Hue, 40%-60% Saturation and 30-80% Value.

If there is other better chroma-key algorithm, your advice is highly appreciated too.

Thanks in advance.

like image 224
jhloke Avatar asked Aug 05 '12 16:08

jhloke


Video Answer


1 Answers

Imagemagick's FX can be used to generate a alpha channel. The hue, saturation, lightness, & luma keywords exists, but you'll need to calculate the color value by max(r, g, b).

hueMin=115/360;
hueMax=125/360;
saturationMin=0.40;
saturationMax=0.60;
valueMin=0.30;
valueMax=0.80;
value = max( r, max( g, b ) );
(
  ( hue > hueMin && hue < hueMax ) && (
  ( saturation > saturationMin && saturation < saturationMax ) || 
  ( value > valueMin && value < valueMax ))) ? 0.0 : 1.0

Saving the above into a file named hsl-greenscreen.fx and execute it against an image with:

convert source.png -channel alpha -fx @hsl-greenscreen.fx out.png

The FX script will probably need additional tweaking to match expected results. You'll also notice this will take a bit of CPU to complete, but that can be improved on.

Another option would be to apply the same -fuzz options, but on each HSV channel. Simply split & clone each channel, apply -fuzz against a target grey, and compose an image mask.

convert source.png -colorspace HSV -separate +channel \
  \( -clone 0 -background none -fuzz 5% +transparent grey32 \) \
  \( -clone 1 -background none -fuzz 10% -transparent grey50 \) \
  \( -clone 2 -background none -fuzz 20% -transparent grey60 \) \
  -delete 0,1,2 -alpha extract -compose Multiply -composite \
  -negate mask.png

Then assign the mask as the images alpha channel

convert source.png mask.png -alpha Off -compose CopyOpacity -composite out.png
like image 123
emcconville Avatar answered Oct 01 '22 18:10

emcconville