Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

problem in fire effect with webcam motion detection

$
0
0
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?

  1. import processing.video.*;

  2. int numPixels;
  3. int threshold = 50;
  4. int[] currentPixels;
  5. int[] lastGoodPixels;
  6. int[] backgroundPixels;
  7. Capture video;
  8. PImage pg;

  9. int effectWidth;
  10. int effectHeight;

  11. // This will contain the pixels used to calculate the fire effect
  12. int[][] fire;
  13. // Flame colors
  14. color[] palette;
  15. int[] calc1,calc2,calc3,calc4,calc5;


  16. void setup() 
  17. {
  18.   // Change size to 320 x 240 if too slow at 640 x 480
  19.   size(640, 480, JAVA2D); 
  20.   //size(320, 240, JAVA2D); 
  21.   
  22.   effectWidth = width/2;
  23.   effectHeight = height/2;
  24.   pg = createImage(effectWidth, effectHeight, RGB);

  25.   video = new Capture(this, effectWidth, effectHeight, 30);
  26.  
  27.   frameRate(60);
  28.   numPixels = effectWidth * effectHeight;
  29.   
  30.   // Create arrays to store the current and background image
  31.   backgroundPixels = new int[numPixels];
  32.   currentPixels = new int[numPixels]; 
  33.   lastGoodPixels = new int[numPixels];
  34.   
  35.   calc1 = new int[effectWidth];
  36.   calc3 = new int[effectWidth];
  37.   calc4 = new int[effectWidth];
  38.   calc2 = new int[effectHeight];
  39.   calc5 = new int[effectHeight];

  40.   colorMode(HSB);

  41.   fire = new int[effectWidth][effectHeight];
  42.   palette = new color[255];

  43.   // Generate the palette
  44.   for(int x = 0; x < palette.length; x++) {
  45.     //Hue goes from 0 to 85: red to yellow
  46.     //Saturation is always the maximum: 255
  47.     //Lightness is 0..255 for x=0..128, and 255 for x=128..255
  48.     palette[x] = color(x/3, 255, constrain(x*3, 0, 255));
  49.   }

  50.   // Precalculate which pixel values to add during animation loop
  51.   for (int x = 0; x < effectWidth; x++) {
  52.     calc1[x] = x % effectWidth;
  53.     calc3[x] = (x - 1 + effectWidth) % effectWidth;
  54.     calc4[x] = (x + 1) % effectWidth;
  55.   }
  56.   
  57.   for(int y = 0; y < effectHeight; y++) {
  58.     calc2[y] = (y + 1) % effectHeight;
  59.     calc5[y] = (y + 2) % effectHeight;
  60.   } 
  61.  video.start(); 
  62. }



  63. void captureEvent(Capture which) 
  64.   if (which!=video || !video.available())
  65.     return;
  66.   
  67.   video.read(); // Read a new video frame
  68.   video.loadPixels(); // Make the pixels of video available
  69.   
  70.   // Difference between the current frame and the stored background
  71.   int presenceSum = 0;
  72.   
  73.   // For each pixel in the video frame...
  74.   for (int i = 0; i < numPixels; i++) 
  75.   { 
  76.     // Fetch the current color in that location, and also the color
  77.     // of the background in that spot
  78.     color currColor = video.pixels[i] & 0xFF;
  79.     color bkgdColor = backgroundPixels[i] & 0xFF;
  80.     int diff = abs(currColor - bkgdColor);
  81.     currentPixels[i] = diff;
  82.     presenceSum += diff;
  83.   }
  84.   if (presenceSum > 0)
  85.   {
  86.     // image changed
  87.     arraycopy(video.pixels, backgroundPixels);
  88.     arraycopy(currentPixels, lastGoodPixels);
  89.     println("update " + presenceSum);
  90.   } 
  91. }


  92. void draw() 
  93. {
  94.   pg.loadPixels();
  95.   
  96.   int counter = 0;
  97.   // Do the fire calculations for every pixel, from top to bottom
  98.   for (int y = 0; y < effectHeight; y++) 
  99.   {
  100.     for(int x = 0; x < effectWidth; x++) 
  101.     {
  102.       int pixel = lastGoodPixels[counter];
  103.       if (pixel > threshold)
  104.       {
  105.         pixel = min((pixel - threshold)/2 + fire[x][y], 128);
  106.         fire[x][y] = pixel;
  107.       }
  108.       else
  109.       {
  110.         // Add pixel values around current pixel
  111.         fire[x][y] =
  112.             ((fire[calc3[x]][calc2[y]]
  113.             + fire[calc1[x]][calc2[y]]
  114.             + fire[calc4[x]][calc2[y]]
  115.             + fire[calc1[x]][calc5[y]]) << 5) / 129;
  116.       }
  117.       // Output everything to screen using our palette colors
  118.       pg.pixels[counter++] = palette[fire[x][y]];
  119.     }
  120.   }  
  121.   pg.updatePixels();

  122.   // display the results
  123.   image(pg,0,0,width,height); 

  124.   println("drawn");  
  125. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles