Remove green background
Remove green background
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.
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 (257.59 KiB) Viewed 106353 times
Re: Remove green background
I managed to improve it, that's not easy with all the different "shades" of green but that's way better.
- Attachments
-
- ezezezezezezezezezezezez.png (242.48 KiB) Viewed 106334 times
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
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
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
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
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 ?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
No, it cannot. It uses features that are not in Imagick.
Re: Remove green background
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();
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();
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
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.
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
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!
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 (365.96 KiB) Viewed 106018 times
-
- qsqsqsdqsdqsdqdsq.png (198.35 KiB) Viewed 106018 times
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Remove green background
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.
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
Re: Remove green background
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
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 ![Very Happy :D](./images/smilies/icon_biggrin.gif)
If I set the background to white, the image is fully white, everything disappears.
![Very Happy :D](./images/smilies/icon_biggrin.gif)
If I set the background to white, the image is fully white, everything disappears.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove green background
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.