We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
how to grab a webcam frame or image? (Read 1681 times)
how to grab a webcam frame or image?
Feb 3rd, 2010, 4:41am
 


Im wanting to do some stereo vision based on the article however I am confused,

Once I started the two webcam feeds, how do I grab each frame/image to then do my processing on them?

I was hoping there would be some sort of videofeed1.getimage(); type func or something?

thanks colin
Re: how to grab a webcam frame or image?
Reply #1 - Feb 3rd, 2010, 7:08am
 
int pix1 = videofeed1.pixels[ videofeed1.width * y + x ];

Check out the Images and Pixels article in the learning section for more information about how to use pixels[] arrays.

Check the demos that come with Processing, eg

Examples > Libraries > Video (capture) > BrightnessThresholding

I've written a template for accessing two camera feeds (though haven't tested it beyond compiling, since my system doesn't like / work with the QuickTime capture used by Processing!)

-spxl

Code:
/**
* spxlDuelingCameras
* http://subpxiels.com
*
* PLEASE NOTE: this code uses the same index into the
* pixels[] arrays for BOTH cameras and the OUTPUT window,
* which is assuming they are all the same size (eg 320x240).
* If you want to use different sized camera frames or a
* different-sized output window, you will need to calculate the
* pixels[] array offsets separately or you will end up with a
* big mess (if not an out of bounds exception if you try to
* read past the end of one of the smaller arrays).
*
* Updated: 2010-02-07 by subpixel
* - Use w,h variables in call to size().
* - Use width and height for main loops instead of cam1.width
*   and cam1.height
* - Fixed code for setting p2,r2,g2,b2
*/

import processing.video.*;

Capture cam1;
Capture cam2;

void setup()
{
 // Start with simple case where both
 // cameras produce the same frame size
 int w = 320;
 int h = 240;
 int fps = 30;

 size(w, h, P2D);
 frameRate(fps); // desired OUTPUT frame rate
 
 try
 {
   String[] camNames = Capture.list();
 
   println("Camera names: \n" + camNames);

   println("Initialising: " + camNames[0]);
   cam1 = new Capture(this, w, h, camNames[0], fps);
   
   println("Initialising: " + camNames[1]);
   cam2 = new Capture(this, w, h, camNames[1], fps);
 }
 catch (Exception e)
 {
   println("Error attaching camera(s): " + e.getMessage());
   exit();
 }
}

// This just updates the Capture object
// with the new frame when it is available
public void captureEvent(Capture c)
{
 c.read();
}

public void draw()
{
 loadPixels(); // For OUTPUT window's pixels[] array
 
 for (int i=0, y=0; y < height; y++)
 {
   for (int x = 0; x < width; x++, i++)
   {
     // Pixel from cam1 (red, green, blue)
     int p1 = cam1.pixels[i];
     int r1 = (p1 >>> 16) & 0xff;
     int g1 = (p1 >>>  8) & 0xff;
     int b1 =  p1         & 0xff;

     // Pixel from cam2 (red, green, blue)
     int p2 = cam2.pixels[i];
     int r2 = (p2 >>> 16) & 0xff;
     int g2 = (p2 >>>  8) & 0xff;
     int b2 =  p2         & 0xff;
     
     // Information available:
     // - current (x,y) coordinates
     // - current pixels[] array(s) index
     // - pixel value from each camera
     //   at the current (x,y) cooordinates
     // - red, green, blue component colour values
     //   for pixels from each camera
     
     // (Very!) simple example: average r,g,b values
     int r = (r1 + r2) / 2;
     int g = (g1 + g2) / 2;
     int b = (b1 + b2) / 2;
     
     // "Safety" code
     r = constrain(r, 0, 255);
     g = constrain(g, 0, 255);
     b = constrain(b, 0, 255);

     // Recombine r,g,b into integer pixel value      
     int p = (r << 16) | (g << 8) | b;
     
     pixels[i] = p; // OUTPUT window's pixels[] array
   }
 }
 
 updatePixels(); // OUTPUT window's pixels[] array
}
Re: how to grab a webcam frame or image?
Reply #2 - Feb 6th, 2010, 1:05pm
 
Meep - there was some obvious bad code for determining the RGB values coming from the second camera:

Code:
int p2 = cam1.pixels[i]; 


Note that this is camera 1's pixel value, not camera 2!

Similarly, the following lines, previously using p1 instead of p2, have been changed.

-spxl
Re: how to grab a webcam frame or image?
Reply #3 - Feb 8th, 2010, 3:58am
 
This Topic was moved here from Sound,  Music Libraries by antiplastik.
Page Index Toggle Pages: 1