thomaskoh
Ex Member
Replacing the background pixels.
Reply #12 - Feb 8th , 2010, 10:40pm
I am using the video library to capture video.I am doing a project similiar to chromakey.I am to change the background with a chosen background image that i like by using a green background.By doing so,I can change the green background pixels with a still image pixels in the Data folder to change whatever background image that I like to have. This is achieved by reading the color of one of the captured green background pixel,then compare the whole frame pixels with the green pixels,therefore leaving behind only those pixels that does not have the green pixel. The effect that i get is quite bad which i believe is the limitation of the processing and the camera.I try to smoothen out the effect by making an offset in the range of comparing the R,G.B of the green pixels,however the effect is still very bad. The code is as follow: import processing.video.*; Capture video; PImage img; PImage img2; float bgcolorR,bgcolorG,bgcolorB; float videoR,videoG,videoB; float imgR,imgB,imgG; float offset=60; void setup() { size(200,200); strokeWeight(5); // Uses the default video input, see the reference if this causes an error video = new Capture(this, width, height, 24); numPixels = video.width * video.height; noCursor(); smooth(); } void draw() { if (video.available()) { video.read(); video.loadPixels(); img = loadImage("sunflower.jpg"); img2 = loadImage("ship.jpg"); int threshold = 127; // Set the threshold value float pixelBrightness; // Declare variable to store a pixel's color loadPixels(); for (int y = 0; y < height; y++ ) { for (int x = 0; x < width; x++ ) { int loc = x + y*width videoR=red(video.pixels[loc]); videoB=blue(video.pixels[loc]); videoG=green(video.pixels[loc]); bgcolorR=red(video.pixels[0]); bgcolorB=blue(video.pixels[0]); bgcolorG=green(video.pixels[0]); //comparing the video pixel with the green pixel with some offset value if((videoR>bgcolorR-offset&&videoR<bgcolorR+offset)&&(videoB>bgcolorB-offset&&vi deoB<bgcolorB+offset )&&(videoG>bgcolorG-offset&&videoG<bgcolorG+offset)){ color imgcolor=img.pixels[loc]; imgR = (imgcolor >> 16) & 0xFF; imgG= (imgcolor >> 8) & 0xFF; imgB = imgcolor & 0xFF; pixels[loc] = color(imgR,imgG,imgB); } else { pixels[loc]=video.pixels[loc]; } // pixels[loc]=video.pixels[loc]; } } updatePixels(); } } I wonder if anyone has done similiar thing before. Thanks for any help and advice