Removing the Green Screen From the image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
varun

Removing the Green Screen From the image

Post by varun »

Hi ,
I just want to remove the green screen from the image taken with the green background , I want to generate a transparent image after tge green screen so that once I put this transparent image on any of the background , it should be a composite image with the new background.
Following is the example to see.
http://static.vsocial.com/vespa/images/ ... screen.jpg
In the above link you can see there is a scooter infront of a green screen. What I want to do is that I want to replace the green are with a transparent one. I all I want to generate a transparent image out of it .
Last edited by varun on 2009-08-10T09:20:17-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing the Green Screen From the image

Post by fmw42 »

I think you need to post a link to your file so that we can see what you have. I don't understand what you want to do.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Removing the Green Screen From the image

Post by Bonzo »

You do not say what method you are using but this will do what you want:

Code: Select all

convert GTS_yellow_green_screen.jpg -fuzz 20% -transparent #1eff01 output.png
There may be a slight green "halo" around the vespa - try increasing the -fuzz value to remove.

Try this to overlay it over your background image all in one line of code:

Code: Select all

convert GTS_yellow_green_screen.jpg -fuzz 20% -transparent #1eff01 miff:- | composite -compose Dst_Over background.jpg -
You will have a problem if the colour is in the vespa or what ever image you are using.

Here is a good tutorial about what you want to do:
http://tech.natemurray.com/2007/12/conv ... arent.html

This should not be in this section of the forum but the users section.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Removing the Green Screen From the image

Post by el_supremo »

Or with one convert command, (assuming that the green screen is pure green):

Code: Select all

convert green_test_bgnd.png ( green_test_ovl.png -alpha on -fuzz 20% -transparent rgb(0,255,0)  ) -composite green_test_out.png
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Removing the Green Screen From the image

Post by anthony »

Technique for this problem is something I have been seriously considering for a very long time.

The obvious method as given above is simply to do a fuzzed color replacement. This works very well, but generates 'aliased' or thresholded edges. That however is find if you plan to save the image as a GIF image on transparency.

It also works very well on very very large images such as what you have provided, as after color replacement resizing the image smaller will restore the anti-aliasing (semi-transparent) edge pixels.

However this technique fails badly when the image is already small and will not be resized smaller! It is in these situations I have been trying to come up with ways of
recovering the original pixels before they were mixed with a known background color.

That is I am looking for way of subtracting a specific color leaving 1) the correct semi-transparency 2) the original color.

The technique Masking with Anti-Aliased Edges was developed with this goal in mind, but is only designed to recover the transparency, from wimages with a fixed color (usually black) border.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cedk
Posts: 16
Joined: 2010-11-23T05:36:22-07:00
Authentication code: 8675308

Re: Removing the Green Screen From the image

Post by cedk »

Hi,

what about Green screen ?

Code: Select all

convert GTS_yellow_green_screen.jpg -channel alpha -fx "K0*b − K1*g + K2" output.png
CedK
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Removing the Green Screen From the image

Post by anthony »

As someone decided to resurrect this very very old discussion.. New methods and techniques are now exampled in...

IM Examples, Photo Handling, Chroma-Key Masking
http://www.imagemagick.org/Usage/photos/#chroma_key
Which is essentially the same type of thing, but using hue for the actual masking.
The next section (incomplete)
http://www.imagemagick.org/Usage/photos/#green_screen
which includes a basic formula shown above for aliased pixel recovery.

The biggest addition however has been the working out of 'perfect background removal', but this is only possible if two different backgrounds are available (such as white and black) so this is not really 'green screen'
http://www.imagemagick.org/Usage/maskin ... background
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cedk
Posts: 16
Joined: 2010-11-23T05:36:22-07:00
Authentication code: 8675308

Re: Removing the Green Screen From the image

Post by cedk »

Looking on Google for solutions (my problem is to remove a blue screen in background), I found this topic, and solutions. I also saw that the subject had been consulted many times. Hence the decision for "someone" to bump the topic ...;-)
However, anyone know the formula for a blue background?

Someone (CedK)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Removing the Green Screen From the image

Post by anthony »

It is probably the same but with blue and green values swapped.

The formula comes from wikipedia, green screen. Their are other much more complex green screen anti-aliasing methods.

Once you have the 'alpha' and the known 'green screen' background, you can fix the colors as shown for the second step of 'perfect background removal'. It is the generation of the missing alpha that is the hard part, though the color fix formula was quite a puzzle for many years until solved by Forum Member HugoRune.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lunawebs
Posts: 2
Joined: 2012-12-19T13:40:38-07:00
Authentication code: 6789

Re: Removing the Green Screen From the image

Post by lunawebs »

we've got the concept of using GD to remove green from an image - how about removing from a video?
In other words a user uploads a video - we pull the frames from the video - edit them indivdiually - then render them out again to get a video with the chroma key work done.
Here's the catch - is it possible to go about this process without pulling out each individual frame?
How about now adding another video to the now newly rendered keyed out video?
You get the idea - you upload a video - the server / script keys out the green, takes the new video with the alpha channel and puts in an already produced video behind the subject.

Would love to hear any thoughts. Ideally - will use PHP on CentOS.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing the Green Screen From the image

Post by fmw42 »

IM is an image processor and not a video processor. It will have to separate each frame and then put them back together again.
lunawebs
Posts: 2
Joined: 2012-12-19T13:40:38-07:00
Authentication code: 6789

Re: Removing the Green Screen From the image

Post by lunawebs »

thanks for the fast reply, we have ffmpeg splitting out the frames to images,
ffmpeg -i video.mov imagefolder/pic%d.png

working with image magick to pull the chroma key. It works but not great, we've tried
convert test.png -fuzz 20% -transparent #5db442 out.png

kind of blotchy, trying to figure out how to refine the edge
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing the Green Screen From the image

Post by fmw42 »

Post Reply