Having difficulties with accessing webcam when using motion detection code.
Webcam works with other applications and when running Processing code, the viewing window is just dark. I tried to used the printArray Capture.list() to select the correct webcam and I do not receive any errors but only get a dark window.
// Exercise 16-7: Create a sketch that looks for the average location
// of motion. Can you have an ellipse follow your waving hand?
import processing.video.*;
// Variable for capture device
Capture video;
// Previous Frame
PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold = 50;
void setup() {
fullScreen(P2D, 2);
//size(320, 240);
// Using the default capture device
// Capture camera list
printArray(Capture.list());
video = new Capture(this, Capture.list()[80]);
width = 800;
height = 600;
video.start();
// Create an empty image the same size as the video
prevFrame = createImage(video.width, video.height, RGB);
}
// New frame available from camera
void captureEvent(Capture video) {
// Save previous frame for motion detection!!
prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
prevFrame.updatePixels();
video.read();
}
void draw() {
background(0);
// You don't need to display it to analyze it!
image(video, 0, 0);
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
// These are the variables we'll need to find the average X and Y
float sumX = 0;
float sumY = 0;
int motionCount = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
// What is the current color
color current = video.pixels[x+y*video.width];
// What is the previous color
color previous = prevFrame.pixels[x+y*video.width];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// Motion for an individual pixel is the difference between the previous color and current color.
float diff = dist(r1, g1, b1, r2, g2, b2);
// If it's a motion pixel add up the x's and the y's
if (diff > threshold) {
sumX += x;
sumY += y;
motionCount++;
}
}
}
// average location is total location divided by the number of motion pixels.
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
// Draw a circle based on average motion
smooth();
noStroke();
fill(255);
ellipse(avgX, avgY, 16, 16);
}
I appreciate any support or advice on getting a webcam to work with processing.