Remove green background

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Remove green background

Post 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.
Attachments
green.png
green.png (257.59 KiB) Viewed 139144 times
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post by sooN »

I managed to improve it, that's not easy with all the different "shades" of green but that's way better.
Attachments
ezezezezezezezezezezezez.png
ezezezezezezezezezezezez.png (242.48 KiB) Viewed 139125 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post 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.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post by fmw42 »

If you are willing to use PHP exec(), you might be interested in my greenscreen bash imagemagick script at my link below.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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 ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post by fmw42 »

No, it cannot. It uses features that are not in Imagick.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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();
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post 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.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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!
Attachments
dqssqdqddqs.png
dqssqdqddqs.png (365.96 KiB) Viewed 140319 times
qsqsqsdqsdqsdqdsq.png
qsqsqsdqsdqsdqdsq.png (198.35 KiB) Viewed 140319 times
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove green background

Post 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.
snibgo's IM pages: im.snibgo.com
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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.
sooN
Posts: 13
Joined: 2019-04-29T04:35:12-07:00
Authentication code: 1152

Re: Remove green background

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove green background

Post 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.
Post Reply