2

I am looking for a way to programmatically analyze a video feed from an external usb webcam under OSX.

Since I haven't done any low level programming like this before I am currently kind of lost on where to start.

How can I access a webcam feed and grab the image data to then process further? At this point I am just trying to understand the basic concept and am not looking for language-specific solutions. Any sample code would be highly appreciated.

I'd appreciate it very much if someone could point me in the right direction and help me get started.

Thank you very much in advance!

Thomas

2 Answers2

1

Use OpenCV.

And check my previous answer on this subject if you are looking for a code example to display the webcam images. It converts the video feed to grayscale and displays them on a window:

OpenCV 2.1: Runtime error

If you just want to display the frames, then replace the else block by this:

else
{
    cvShowImage("Colored video", color_frame);
}

In case you are wandering how to manipulate the pixels of the frame:

int width = color_frame->width; 
int height = color_frame->height;
int bpp = color_frame->nChannels; 
for (int i=0; i < width*height*bpp; i+=bpp) 
{
  if (!(i % (width*bpp))) // print empty line for better readability
      std::cout << std::endl;

  std::cout << std::dec << "R:" << (int) color_frame->imageData[i] <<  
                          " G:" << (int) color_frame->imageData[i+1] <<  
                          " B:" << (int) color_frame->imageData[i+2] << " "; 
}
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
1

For quick access to a webcam and for manipulation of pixel data, you can use Processing with the Video library - the easiest way to start is to check out the examples bundled with the IDE.

Processing is a java based visualisation language which is easy to learn and use and works on WIndows, MacOSX and Linux. I found the webcam stuff worked out of the box on my MacBook.

Here is an example script (based on an example bundled in the IDE) which loads a webcam feed and renders the pixels in greyscale.


import processing.video.*;

int numPixels;
Capture video;

void setup() {
  // Change size to 320 x 240 if too slow at 640 x 480
  size(640, 480, P2D); 

  video = new Capture(this, width, height, 24);
  numPixels = video.width * video.height;
  // Make the pixels[] array available for direct manipulation
  loadPixels();
}

void draw() {
  if (video.available()) {
    video.read(); // Read a new video frame
    video.loadPixels(); // Make the pixels of video available
    for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
      // Make all the pixels grey if mouse is pressed
      if (mousePressed) {
        float greyVal = brightness(video.pixels[i]);
        pixels[i] = color(greyVal);
      } else {
        // If mouse not pressed, show normal video
        pixels[i] = video.pixels[i];
      }
    }
    updatePixels(); // Notify that the pixels[] array has changed
  }
}

Moreover, there is a great interface to OpenCV which can be used for edge detection etc.

Brendan
  • 18,771
  • 17
  • 83
  • 114