I am using the sketch below, which creates a fire effect on motion detection when using a webcam. The problem is, I am trying to be able to see the live feed instead of black image and background. I keep changing parameters but with no success. Any thoughts?
- import processing.video.*;
- int numPixels;
- int threshold = 50;
- int[] currentPixels;
- int[] lastGoodPixels;
- int[] backgroundPixels;
- Capture video;
- PImage pg;
- int effectWidth;
- int effectHeight;
- // This will contain the pixels used to calculate the fire effect
- int[][] fire;
- // Flame colors
- color[] palette;
- int[] calc1,calc2,calc3,calc4,calc5;
- void setup()
- {
- // Change size to 320 x 240 if too slow at 640 x 480
- size(640, 480, JAVA2D);
- //size(320, 240, JAVA2D);
- effectWidth = width/2;
- effectHeight = height/2;
- pg = createImage(effectWidth, effectHeight, RGB);
- video = new Capture(this, effectWidth, effectHeight, 30);
- frameRate(60);
- numPixels = effectWidth * effectHeight;
- // Create arrays to store the current and background image
- backgroundPixels = new int[numPixels];
- currentPixels = new int[numPixels];
- lastGoodPixels = new int[numPixels];
- calc1 = new int[effectWidth];
- calc3 = new int[effectWidth];
- calc4 = new int[effectWidth];
- calc2 = new int[effectHeight];
- calc5 = new int[effectHeight];
- colorMode(HSB);
- fire = new int[effectWidth][effectHeight];
- palette = new color[255];
- // Generate the palette
- for(int x = 0; x < palette.length; x++) {
- //Hue goes from 0 to 85: red to yellow
- //Saturation is always the maximum: 255
- //Lightness is 0..255 for x=0..128, and 255 for x=128..255
- palette[x] = color(x/3, 255, constrain(x*3, 0, 255));
- }
- // Precalculate which pixel values to add during animation loop
- for (int x = 0; x < effectWidth; x++) {
- calc1[x] = x % effectWidth;
- calc3[x] = (x - 1 + effectWidth) % effectWidth;
- calc4[x] = (x + 1) % effectWidth;
- }
- for(int y = 0; y < effectHeight; y++) {
- calc2[y] = (y + 1) % effectHeight;
- calc5[y] = (y + 2) % effectHeight;
- }
- video.start();
- }
- void captureEvent(Capture which)
- {
- if (which!=video || !video.available())
- return;
- video.read(); // Read a new video frame
- video.loadPixels(); // Make the pixels of video available
- // Difference between the current frame and the stored background
- int presenceSum = 0;
- // For each pixel in the video frame...
- for (int i = 0; i < numPixels; i++)
- {
- // Fetch the current color in that location, and also the color
- // of the background in that spot
- color currColor = video.pixels[i] & 0xFF;
- color bkgdColor = backgroundPixels[i] & 0xFF;
- int diff = abs(currColor - bkgdColor);
- currentPixels[i] = diff;
- presenceSum += diff;
- }
- if (presenceSum > 0)
- {
- // image changed
- arraycopy(video.pixels, backgroundPixels);
- arraycopy(currentPixels, lastGoodPixels);
- println("update " + presenceSum);
- }
- }
- void draw()
- {
- pg.loadPixels();
- int counter = 0;
- // Do the fire calculations for every pixel, from top to bottom
- for (int y = 0; y < effectHeight; y++)
- {
- for(int x = 0; x < effectWidth; x++)
- {
- int pixel = lastGoodPixels[counter];
- if (pixel > threshold)
- {
- pixel = min((pixel - threshold)/2 + fire[x][y], 128);
- fire[x][y] = pixel;
- }
- else
- {
- // Add pixel values around current pixel
- fire[x][y] =
- ((fire[calc3[x]][calc2[y]]
- + fire[calc1[x]][calc2[y]]
- + fire[calc4[x]][calc2[y]]
- + fire[calc1[x]][calc5[y]]) << 5) / 129;
- }
- // Output everything to screen using our palette colors
- pg.pixels[counter++] = palette[fire[x][y]];
- }
- }
- pg.updatePixels();
- // display the results
- image(pg,0,0,width,height);
- println("drawn");
- }