6

Is it possible to apply chroma keying, meaning to remove a green (or blue...) screen background, using Gimp? (I happen to use Gimp, but I suppose an answer using Photoshop would be just as good, unless it's referencing a specific Photoshop plugin.)

What I mean is the following:

enter image description here

(Note: I don't know how to do it yet, this mockup was made starting from a stock transparent picture.)

If we look at what needs to be done (or approximated as far as possible, see below) it is the following:

enter image description here

Notice how this procedure is not merely adding an alpha channel; it is changing all colors that lie in the same quadrant as the key color itself. Meaning, if the key color is green, all greenish colors need to be substantially modified, in addition to having a suitable alpha channel added:

enter image description here

The reason I posted this question is that all other questions and answers about this topic miss this crucial point.

You can not turn a shade of green into semi-transparent red, and another shade of green into semi-transparent blue, just by fiddling with the magic wand, the color selection tool, or by copying a channel into a mask.

I don't know the theoretical background behind chroma keying and I'm guessing it could be quite hairy. For instance, the color that lies exactly between the key color (green in this case) and its exact opposite (which would be magenta in this case) is a perfect gray (by definition of opposite color.) Do we need to turn that shade of gray into semi-transparent magenta? What if it was an actual gray in the original picture? (Which is actually more likely.)

Given all this undefined behaviour in the problem space itself, I'm sure Adobe Premiere and all the other professional video production tools contain very sophisticated chroma keying algorithms.

Can we even approximate the result of those algorithms using basic image manipulation, as found in Gimp? Or is any such endeavor bound to fail?

Alternatively, is there an open source Gimp plugin or separate program that can perform a good quality chroma keying on pictures?

Edit: Wikipedia's article gives an example of a simple function for green screen: rgb(r,g,b) -> rgba(r, min(g,b), b, A*(r+b)-B*g) and goes on to explain that modern algorithms can be much more complicated. Even with the simple function, I understand what it's saying, but I would not know how to perform it using simple selections and channel manipulation.

6
  • Why green screen? There are better ways to remove backgrounds in both GIMP and Photoshop. GIMP and Photoshop aren't video editors. Green screen is mainly used in video.
    – Billy Kerr
    May 9, 2021 at 21:19
  • @BillyKerr Because I have source pictures already made against a green screen and need to separate the foreground. I have tried with the usual techniques (magic wand and such) but they miss the substantial zone where the two colors are mixed, as I show above. Hollywood proves that this result can be achieved, or approximated, and has been so for a century using film. And so there must be ways to do it in digital image processing.
    – Tobia
    May 9, 2021 at 21:31
  • @BillyKerr "Good for video but not for photography" makes no sense, as video is but a sequence of photographies. My question is not about how to take good pictures (I don't even own a camera.) My question is about digital image processing.
    – Tobia
    May 9, 2021 at 21:32
  • I mean it's better for video than it is for Photography. Neutral backgrounds are preferred in still photography. If you've ever worked in a studio, you'd know. I have, and green screen is not optimal.
    – Billy Kerr
    May 9, 2021 at 21:34
  • Again, I have no control over the source pictures. This is a technical question about image manipulation, not a question on how to take good pictures.
    – Tobia
    May 9, 2021 at 21:34

3 Answers 3

7

Obviously your scalp with green background presents well your problematic images - large areas where the green is mixed with hair:

enter image description here

Unfortunately there's no way to calculate exactly the original color of the mixes areas because we do not know is it mixed due the transparency (= more transparent hairs near the edges) or was it originally with no green background colored to more green at the edges than in the middle of the scalp. We must make a guess.

In your image of "what should be done" you guessed that the horizontal color stripes continue with the same hue and saturation, but the transparency increases as the color moves closer to pure green. Actually you didn't guess, you constructed the stripes that way, but a dumb automate with no AI cleverness cannot know how an image is constructed. It sees only the mixing result pixel by pixel with no idea what kind of common creation process could be behind different pixels. You need something much more advanced than GIMP if you want some guessing ability.

To stay in truth your colored stripes would be recovered quite well with the next process because the programmer has thought in same way as you - like the unwanted color is there only due transparency.

The process: The green can be turned to transparency by applying Colors > Color to Alpha.

This is the result with your stripes:

enter image description here

The next one is a more realistic result - the background is mixed with the target in a complex way. It's not pure linear transparency:

enter image description here

The treshold sliders are now quite randomly set. Areas which are pure green, only darker than the background are magenta. Areas which were too far from pure green, but still green are "as is". The green vanishes but the red and blue increase if the opacity treshold slider is moved:

enter image description here

No good tresholds can be found. The other slider would crop the hair. But you can colorize the image. If your brain has some horsepower beyond the usual you can use channel mixer (Colors > Component > Channel Mixer). I skip it.

In your case a single hue recoloring can be acceptable. Pick from the original image a well saturated sample and fill with it a new top layer. Let that layer have blending mode Color LCH. There's other "color" blending modes but Color LCH fits into this case because it it copies Hue and chroma from the sample:

enter image description here

merge the layers if the color is good.

You can desaturate the sparse areas like in the original by making a duplicate, desaturating the bottom copy and erasing with soft brush partially the colored top layer. Or preferably by inserting a layer mask for easy fix in case of errors.

0
5

Green screen is not optimal for still photography. It's good for video because it's an excellent quick technique for processing thousands of frames. In still photography, this is not the case. Instead, it's problematic. Green screen typically has issues that are best avoided altogether. In still photography for example, neutral backgrounds are usually preferred to avoid the problem of colour contamination. Software like Photoshop and GIMP have background removal tools which can be used to make a good mask without having to resort to using green screen.

As for solving the problem you seem to have been landed with, you will have to jump through some additional hoops.

You can use the green channel to create a mask and invert it and adjust the levels of the mask, but this doesn't solve the problem of colour contamination. It's almost impossible for masking alone to remove the colour contamination.

Here's an example

enter image description here

If the image had instead been on a neutral grey background, this problem would not have existed. A simple mask would have been good enough.

To fix it, you would either need to refine the mask further to remove the colour contamination, at the risk of losing detail, or paint over the edges of original image to remove the green. Neither is a great solution, although the last option is probably your best bet.

In this example I sampled a brown colour from the hair, and used the paintbrush with blending mode set to HSL Colour, to paint over the edges. The result isn't bad, but would be more difficult with a more complex image with different colours.

enter image description here

3
  • Thank you. Using the green channel as basis for a mask, and painting with Color blending mode are techniques that I have already used in the past, but as you say, they get more difficult with complex pictures. I was hoping to find a plugin or technique that replicates what video software already does to each frame, which is usually pretty good.
    – Tobia
    May 9, 2021 at 22:23
  • By pretty good I mean, all YouTubers use green screens and more or less improvised lighting, and you rarely see green halos around them, so the default green screen options found in video editors must be pretty good.
    – Tobia
    May 9, 2021 at 22:26
  • 1
    @Tobia There used to be a plugin for GIMP called Blue Box, that could be used for green/blue chroma key backgrounds, but it doesn't solve the colour contamination problem. No idea where you can find it now, or even if it would still work in recent versions of GIMP. Photoshop has a decontaminate colour slider in the Select and Mask works space. It does much the same as painting over the image edges, although more automated, it doesn't always work well in extreme cases though.
    – Billy Kerr
    May 9, 2021 at 22:41
1

ADD: The questioner has inserted a comment which tells that this answer is useless. His photos have colored zones where the background and target colors are mixed and he is not going to reshoot his images in a way which allows easy background removal.

======

You can apply Color to Alpha adjustment. BUT: The image should not have an edge zone where the edge material color and the key color are mixed. 1 pixel wide zone fortunately can be shaved off later if the target in the image is big enough in pixels. Wide mixed zones need guessing or knowledge what colors there should be It cannot be calculated without heavy assumptions.

If you are going to shoot your own photos you should have so strong and uniform white foreground light that the key color background doesn't affect the edge colors of the target. Blurry lenses and JPG compression are also bad.

Many color based background removal tools are advertised with an example how well they clip hair with no manual tinkering. It doesn't happen without pro quality camera, perfect lights and uniformly lit single color background. Sometimes a sunny beach offers the light and the background. The sand reflects the light ==> there's no deep shadows and the blue sky is a good single color background which is surely so far away that foreground light+the target do not create shadows on the background.

I have suggested here several times for static objects another approach. Take 2 shots with surely non-moving camera (=have pro tripod). Take shot 1 to get a dark silhouette against a bright background and another with bright foreground lights ON. Build a mask from the silhouette.

2
  • I understand what you are saying, but you are missing the point of my question. I have no control over the source pictures, and the problem is exactly the zone where the two colors mix. Hollywood proves that this result can be achieved, or approximated, and has been so for a century using film. And so there must be ways to do it in digital image processing.
    – Tobia
    May 9, 2021 at 21:28
  • @Tobia Hollywood has no problem with the lights, background and the quality of lens, The do not have mixed colors at the edges of the target. In the past the processing was trickier than today with computers, but the light, background and camera quality requirements stay if the method is color keying. Computers make possible clever edge following algorithms. GIMP's background removal tool is well worth checking. Insert a real problematic image (not a screenshot!!!!!) or us to see the severity of your case. Crop if needed, but do not scale down.
    – user82991
    May 10, 2021 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.