Green screen chroma-key using ImageMagick

前端 未结 1 1215
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 22:10

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

1条回答
  •  隐瞒了意图╮
    2021-01-02 22:53

    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
    

    0 讨论(0)
提交回复
热议问题