Here is my version. When the background has high saturation "chroma-key", finding it is fairly easy. But it creates more edge problems. Windows BAT scripts.
Step 1: Make a background green across the whole image. I ignore the lightness of Lab colorspace -- I'm looking for a particular hue and saturation, regardless of lightness. I reduce this to a small number of colours, expecting the chroma-key will occupy just one slot. Assuming coord (0,0) is chroma-key, I fill from there. (Other techniques can be used for more mobile subjects.)
Code: Select all
%IM%convert ^
greenBack.jpg ^
-alpha Set ^
( +clone ^
-blur 0x5 ^
-colorspace Lab -channel R -evaluate set 50%% +channel ^
-colorspace sRGB ^
+dither -colors 30 ^
-fuzz 15%% ^
-fill None ^
-draw "matte 0,0 replace" ^
-channel A -negate +channel ^
) ^
-compose CopyOpacity -composite ^
-blur 0x40 ^
-alpha off ^
gb_1.png
There are "creases" where the subject is in front of the chroma-key. That is no problem.
Step 2: Find the difference between the original and the green-screen. Blurring smooths JPEG compression problems. "-grayscale Brightness" gives the lightest of the three channels. "-level" makes it a black/white mask with small anti-alias. For other sources, adjust the level to get a clean mask.
Code: Select all
%IM%convert ^
( greenBack.jpg -blur 0x1 ) ^
gb_1.png ^
-compose Difference -composite ^
-grayscale Brightness ^
-level 20,25%% ^
gb_2.png
Step 3: Apply the mask opacity to the original.
Code: Select all
%IM%convert ^
greenBack.jpg ^
gb_2.png ^
-compose CopyOpacity -composite ^
gb_3.png
Step 4: Green has bled into the source, most noticable in the hair and around the hands. Blowing up the image, we can see it occurs everywhere. I call a separate script to de-bleed the edge. This de-bleeding is colour-blind. It ignores the actual colour of the chroma-key. If it was more intelligent, it could probably handle the hair properly.
Code: Select all
call %PICTBAT%deBleedEdge gb_3.png 8 gb_4.png
Where the guts of deBleedEdge.bat is:
Code: Select all
%IM%convert ^
%INFILE% ^
( -clone 0 -alpha Extract ) ^
( -clone 1 ^
-morphology Distance "Euclidean:7,%EDGE_W%^!" ^
) ^
( -clone 1-2 -compose Difference -composite ) ^
( -clone 0,2 ^
-compose CopyOpacity -composite ^
-blur 0x%BLUR_SIG% ^
) ^
( -clone -1,3 ^
-compose CopyOpacity -composite ^
) ^
-delete 1-4 ^
-compose Over -composite ^
%OUTFILE%
Step 5: De-bleeding has pixelated the edges, so we feather the transparent/opaque boundary inwards for the finished result.
Code: Select all
%IM%convert ^
gb_4.png ^
( +clone -alpha extract ^
-morphology Distance "Euclidean:7,2^!" ^
) ^
-compose CopyOpacity -composite ^
gb_out.png
The hair isn't perfect. Tweaking the above might improve it, but at the expense of other areas. Perhaps a "hair-detector" mask is needed.
The shiney lecturn has reflected green. A matt spray (available in all good photo studios) would cure that.