Green screen chroma-key using ImageMagick

纵饮孤独 提交于 2019-12-03 15:55:26

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!