help with script bg_removal

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
scottsheppard

help with script bg_removal

Post by scottsheppard »

I am trying to use the script bg_removal to remove a background that is a green screen, and I am unable to get the desired results.

I am using the following command to attempt to remove the background (I sampled the top left of the image for the RGB value):
Command: ./bg_removal.sh img_0092.jpg 'RGB(46,169,102)' 10 10 dest1010.jpg

My source image is http://fuathurio.dyndns.info/ssheppard/ ... g_0092.jpg

Here are some sample results: filename is used to specify the parameters to the script: ex dest1010.jpg is fuzz_out=10, fuzz_in=10

http://fuathurio.dyndns.info/ssheppard/ ... st1010.jpg
http://fuathurio.dyndns.info/ssheppard/ ... st1015.jpg
http://fuathurio.dyndns.info/ssheppard/ ... st1020.jpg
http://fuathurio.dyndns.info/ssheppard/ ... st1025.jpg

I tried modifying the fuzz_out parameter, but regardless of the setting it had no impact on the resulting image.

Also, I am unable to get the script to use the top left corner of the image as the background color. It returns the following error:
Command: ./bg_removal.sh img_0092.jpg 10 10 dest.jpg
Result : convert: invalid argument for option `sparse-color': Invalid number of Arguments @ error/mogrify.c/SparseColorOption/458.

ANY help is greatly appreciated!

Scott
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: help with script bg_removal

Post by snibgo »

Perhaps the script needs some work. Can you post it?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: help with script bg_removal

Post by fmw42 »

Unless you have an exact picture of the screen without any foreground this will be very hard, especially as your image is jpg and has compression issues with constant color background. Your background is not even enough in color. So all you can do is play with -fuzz values and hope for the best.

Unless we see your script, it will be hard to tell you what to do. But you can try -fuzz floodfilling rather than straight color replacement.

see http://www.imagemagick.org/Usage/draw/#matte
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: help with script bg_removal

Post by snibgo »

I would certainly use an image closer to what the camera photographed (not shrunk, etc).

Method 1: Simply and crudely turn the green white:

Code: Select all

convert img_0092.jpg -fx "hue<0.25||hue>0.50?u:white" img_0092_a.png
This gives jaggies, and we have lost the white sock and fur, which have picked up the reflected green.


Method 2 picks up more of the sock and fur, creates a black and white mask, blurred to remove jaggies, and makes the image transparent where it was green:

Code: Select all

convert img_0092.jpg -fx "hue<0.25||hue>0.50||lightness>0.85||saturation<0.2?white:black" -blur 0x1 mask.png
convert img_0092.jpg mask.png -alpha off -compose copy-opacity -composite x3.png
As the original RGB data is present, but hidden by the transparency, you can use Gimp or whatever to manually restore the sock (Gimp eraser anti-erase).
snibgo's IM pages: im.snibgo.com
scottsheppard

Re: help with script bg_removal

Post by scottsheppard »

Sorry about that, I should have said I did not write the script. I found the bg_removal script on the ImageMagick site: http://www.imagemagick.org/Usage/scripts/bg_removal

Also, the picture was taken with an iPhone. It is the original image (not resized, etc).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: help with script bg_removal

Post by snibgo »

About the best I can get with bg_removal is:
./bg_removal img_0092.jpg 'RGB(37,188,111)' 18 19 img_0002_C.jpg

These parameters cut slightly into the sock and fur, and leaves a little of the green surround. You can tweak the "18" to adjust your compromise.

I got the colour by using Gimp to get an average of a large number of pixels, towards the bottom-right. An IM command could do this, of course.

Making the second number, fuzz_in, ("19" in my example) much bigger than the first creates a wider margin between pixels that are entirely outside and those that are entirely inside. You probably don't want that for this image.
snibgo's IM pages: im.snibgo.com
scottsheppard

Re: help with script bg_removal

Post by scottsheppard »

Thanks for your feedback!
Post Reply