Page 1 of 2

Remove green background

Posted: 2019-04-29T04:41:16-07:00
by sooN
Hello,

I'm trying to remove the green background using IMagick because I have way to many pictures and I want to make it "automatic". I'm not familiar with IMagick but I still managed to see some results using what I've found on Google.

So, I'm joining a picture of what I managed to achieve with my "noob" script and I would like to have some advices to improve it.

And the code : https://paste2.org/CU6JVHeH

Thanks.

Re: Remove green background

Posted: 2019-04-29T06:29:31-07:00
by sooN
I managed to improve it, that's not easy with all the different "shades" of green but that's way better.

Re: Remove green background

Posted: 2019-04-29T10:36:02-07:00
by fmw42
Pad your image with a 1 pixel border of the green color before processing. Then use one of the flood fill methods from the corners. That way it will fill in between the legs. If you want further help, then post one of your input images.

Re: Remove green background

Posted: 2019-04-30T03:17:01-07:00
by sooN
Well, I'm now using this : $image->opaquePaintImage( $lightgreen, $finalColor, $fuzzLightGreen, false, Imagick::CHANNEL_DEFAULT);

I'm almost fully satisfied with the result. I'm adjusting the fuzz depending on the "brightness" of the image, it's not perfect yet but I believe I'm on the right way.

Re: Remove green background

Posted: 2019-04-30T09:17:48-07:00
by fmw42
But you need to pad the image with green so that you can get rid of the green between the legs in you image above.

Re: Remove green background

Posted: 2019-04-30T10:26:34-07:00
by fmw42
If you are willing to use PHP exec(), you might be interested in my greenscreen bash imagemagick script at my link below.

Re: Remove green background

Posted: 2019-05-01T06:55:39-07:00
by sooN
I'll have a look to your script, it sounds interesting. But I'd rather use "clean" PHP if possible, your script can't be done using the native IMagick PHP functions ?

Re: Remove green background

Posted: 2019-05-01T09:37:06-07:00
by fmw42
No, it cannot. It uses features that are not in Imagick.

Re: Remove green background

Posted: 2019-05-02T06:06:43-07:00
by sooN
I don't know how to use your script, I didn't managed to make it work. Any reason why those functionalities didn't get ported to Imagick ?

I managed to remove completly the green background with my PHP way but around the human body we can see like a 2 pixels green borders and it doesn't look good if we zoom in.

Don't you think it's possible to get a pretty close result as with your script using IMagick ?

Edit: Well, it works now doing this : exec( 'magick convert demo.jpg -modulate 100,100,0 \ demo3.jpg');

But, I don't really know how to use your script, I feel like I need to understand your approach and do it with many exec();

Re: Remove green background

Posted: 2019-05-02T09:10:26-07:00
by fmw42
You will have to ask the Imagick developer. The Imagemagick developers did not create nor maintain Imagick.

In my script, the two pixel border or so is desaturated.

My script uses connected components processing, which is not in Imagick

Porting everything is likely possible except for connected components. But if you are doing this for commercial purposes, then you would need to license my script in order to port it.

With my script you would do

exec("bash greenscreen.sh <optional arguments such as -t 22 etc> input output")

See my home page for pointers for use with PHP exec.

Contact me offline at my email address on my home page, if you want to discuss this further.

Re: Remove green background

Posted: 2019-05-03T01:51:56-07:00
by sooN
I'm on Windows, it seems a bit tricky to use your script on it. And the "production" server is on Linux.

But, I'm not planning to use it for commercial purposes. I just need to crop and remove the green screen of 3000 images quickly. I could do it manually with Paint.NET for example but this would take way too many times.

What is frustrating is that i'm so close to the perfect result using "clean" PHP but yeah, like I said, when you zoom in...

Do you think MagickWand might help ?

I can't tell how I'm going to fix my "issue" but thanks a lot anyway!

Re: Remove green background

Posted: 2019-05-03T03:32:03-07:00
by snibgo
Light skin or clothing with a green background shows a mixed colour at edges for two reasons: (a) camera pixels have a non-zero area, so some will straddle the skin and the background, and (b) white skin that is nearly co-planar with the lens acts as a mirror, reflecting the background, so it "picks up" the green colour.

To remove the green, a simple method is to erode the mask by a couple of pixels, and perhaps blur it. This "shrinks" the person, which may be a problem.

A more complex method is to process every pixel near an edge: guess at the green background colour (assume it is the same as the nearest pure background), and guess either the correct skin colour or the effective transparency. From these two values we can calculate the third. Thus, we can replace pixels with the correct skin colour and transparency. We might then threshold the transparency, perhaps by distance from the edge, because skin that has only "picked up" the green should be opaque.

Re: Remove green background

Posted: 2019-05-03T06:02:32-07:00
by sooN
Thanks for the information but I won't be able to fix it by myself, I don't really know how to work with images. The results i'm looking for should be similar to this website ( https://www.remove.bg ), sadly, this is not free.

Re: Remove green background

Posted: 2019-05-03T07:07:25-07:00
by sooN
Little update, I managed to make your script work on the Linux server, the "human" is colorized and the background is in in black/grey, it is not deleted. It looks good but this is not the behavior i'm looking for of course :D

If I set the background to white, the image is fully white, everything disappears.

Re: Remove green background

Posted: 2019-05-03T10:02:55-07:00
by fmw42
Post the image and I will process it and tell you the arguments. Note that if you want a transparent background, then you need to output to PNG. If you want a white background or some other color, then use the -b bgcolor argument -b white. Then you can save to JPG.