GreenScreen Effect for movie

edited December 2017 in Library Questions

I'm working on a greenscreen project right now and my task is to use a video with a greenscreen in the background. The green background should be replaced by an image. In the foreground, there is someone doing some random stuff (that doesn't matter). Only the green background is important. So first I tried some steps which are easier, they all worked pretty good, but now I'm trying to replace the green pixels from the greenscreen in the backgorund with black colored pixels, so that we have a black background. I used that code:

import processing.video.*;   //importing the video library
Movie movie;   //declaring movie

void setup() {
  size(1280, 720);
  movie = new Movie(this, "greenscreen_hand.mp4");   //loading movie and..
  movie.loop();   //..looping the movie
}

void draw() {
  loadPixels();
  movie.loadPixels();
  for (int j = 0; j < height; j++) {

    for (int i = 0; i < width; i++) {

      int stelle = i+(j*width);   //iterating through the screen and setting the number of the index through the whole frame
      float red = red(movie.pixels[stelle]);   //RGB values of the pixel
      float green = green(movie.pixels[stelle]);
      float blue = blue(movie.pixels[stelle]);

      color c = color(0, 0, 0);   //black pixels

      if (red>40 && red<80 && green>190 && blue>60 && blue<90) {   //if pixel is kind of green

        movie.set(i, j, c);   //new pixel color
      }
    }
  }

  image(movie, 0, 0);
}

void movieEvent(Movie m) {
  m.read();
}

void mousePressed() {   //that actually doesn't matter, it's just for some information from some steps before
  int stelle = mouseX+(mouseY*width);

  float red = red(movie.pixels[stelle]);
  float green = green(movie.pixels[stelle]);
  float blue = blue(movie.pixels[stelle]);

  println("R: " + red +" "+ "G: " + green +" "+ "B: " + blue);
}

Now I got a whole bunch of problems. Well, sometimes there is an ArrayIndexOutOfBoundsException: 0 (I don't know why actually :/) and sometimes it works and the video is starting, but the background is flickering (half-green and half-black) (maybe because pixels are loading too slow in every millisecond frame?)

Can someone help me? Or some suggestions about what I could change?

Answers

  • edited December 2017
        import processing.video.*;
        Movie movie;
        PImage img;
    
        void setup() {
          size(1280, 720);
          movie = new Movie(this, "greenscreen_hand.mp4");
          img = loadImage("myImage.jpg");
          movie.loop();
        }
    
        void draw() {
          loadPixels();
          img.loadPixels();
          movie.loadPixels();
          for (int j = 0; j < movie.height; j++) {
    
            for (int i = 0; i < movie.width; i++) {
    
              int stelle = i+(j*movie.width);
              float red = red(movie.pixels[stelle]);
              float green = green(movie.pixels[stelle]);
              float blue = blue(movie.pixels[stelle]);
              //color c = color(0, 0, 0);
              if (red>40 && red<80 && green>190 && blue>60 && blue<90) {
    
                float red2 = red(img.pixels[stelle]);
                float green2 = green(img.pixels[stelle]);
                float blue2 = blue(img.pixels[stelle]);
                color c = color(red2, green2, blue2);
    
                movie.set(i, j, c);
              }
            }
          }
    
          image(movie, 0, 0);
        }
    
        void movieEvent(Movie m) {
          m.read();
        }
    
        void mousePressed() {
          int stelle = mouseX+(mouseY*width);
    
          float red = red(movie.pixels[stelle]);
          float green = green(movie.pixels[stelle]);
          float blue = blue(movie.pixels[stelle]);
    
          println("R: " + red +" "+ "G: " + green +" "+ "B: " + blue);
        }
    

    I changed the black background to a random image. I didn't change much, but the flickering effect is still the same, just with the pixels of the image.

    Now my final question: Is the flickering background normal? I do think it is because Processing has to calculate every little pixel within every millisecond, so the screen is flickering, because it can't run fast enough to let the background look smooth.

    Any ideas? ^^"

  • ... because it can't run fast enough let the background look smooth.

    https://Processing.org/reference/rightshift.html

  • Check the reference for pixels[] as well as updatePixels (called in tandem with loadPixels). I believe using pixels[] is recommended over using set.

    Also check in the forum for chromakey

    Kf

Sign In or Register to comment.