How to remove a “green screen” portrait background

后端 未结 5 2042
说谎
说谎 2021-02-04 11:37

I\'m looking for a way to automatically remove (=make transparent) a \"green screen\" portrait background from a lot of pictures.

My own attempts this far have been... e

5条回答
  •  情话喂你
    2021-02-04 12:06

    Since you didn't provide any image, I selected one from the web having a chroma key with different shades of green and a significant amount of noise due to JPEG compression.

    There is no technology specification so I used Java and Marvin Framework.

    input image:

    The step 1 simply converts green pixels to transparency. Basically it uses a filtering rule in the HSV color space.

    As you mentioned, the hair and some boundary pixels presents colors mixed with green. To reduce this problem, in the step 2, these pixels are filtered and balanced to reduce its green proportion.

    before:

    after:

    Finally, in the step 3, a gradient transparency is applied to all boundary pixels. The result will be even better with high quality images.

    final output:

    Source code:

    import static marvin.MarvinPluginCollection.*;
    
    public class ChromaToTransparency {
    
        public ChromaToTransparency(){
            MarvinImage image = MarvinImageIO.loadImage("./res/person_chroma.jpg");
            MarvinImage imageOut = new MarvinImage(image.getWidth(), image.getHeight());
            // 1. Convert green to transparency
            greenToTransparency(image, imageOut);
            MarvinImageIO.saveImage(imageOut, "./res/person_chroma_out1.png");
            // 2. Reduce remaining green pixels
            reduceGreen(imageOut);
            MarvinImageIO.saveImage(imageOut, "./res/person_chroma_out2.png");
            // 3. Apply alpha to the boundary
            alphaBoundary(imageOut, 6);
            MarvinImageIO.saveImage(imageOut, "./res/person_chroma_out3.png");
    
        }
    
        private void greenToTransparency(MarvinImage imageIn, MarvinImage imageOut){
            for(int y=0; y= 60 && hsv[0] <= 130 && hsv[1] >= 0.4 && hsv[2] >= 0.3){
                        imageOut.setIntColor(x, y, 0, 127, 127, 127);
                    }
                    else{
                        imageOut.setIntColor(x, y, color);
                    }
    
                }
            }
        }
    
        private void reduceGreen(MarvinImage image){
            for(int y=0; y= 60 && hsv[0] <= 130 && hsv[1] >= 0.15 && hsv[2] > 0.15){
                        if((r*b) !=0 && (g*g) / (r*b) >= 1.5){
                            image.setIntColor(x, y, 255, (int)(r*1.4), (int)g, (int)(b*1.4));
                        } else{
                            image.setIntColor(x, y, 255, (int)(r*1.2), g, (int)(b*1.2));
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            new ChromaToTransparency();
        }
    }
    

提交回复
热议问题