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?