Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.video.*;
- Capture video;
- PImage prevFrame;
- void setup() {
- size(640, 480);
- video = new Capture(this, width, height, 30);
- video.start();
- prevFrame = createImage(video.width, video.height, RGB);
- String[] cameras = Capture.list();
- if (cameras.length == 0) {
- println("There are no cameras available for capture.");
- exit();
- } else {
- println("Available cameras:");
- }
- for (int i = 0; i < cameras.length; i++) {
- println(cameras[i]);
- }
- }
- 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); // Before we read the new frame, we always save the previous frame for comparison!
- prevFrame.updatePixels(); // Read image from the camera
- video.read();
- }
- void draw() {
- loadPixels();
- video.loadPixels();
- prevFrame.loadPixels();
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x+y*width;
- float r = red(video.pixels[loc]);
- float g = green(video.pixels[loc]);
- float b = blue(video.pixels[loc]);
- pixels[loc] = color(r, g, b) / 2;
- color current = video.pixels[loc]; // Step 2, what is the current color
- color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
- // 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);
- float diff = dist(r1, g1, b1, r2, g2, b2);
- // Step 5, How different are the colors?
- // If the color at that pixel has changed, then there is motion at that pixel.
- int s = second();
- if ((diff > 20) && (diff < 29)) {
- pixels[loc] = color(r, g, b) / 2;
- } else if (diff < 5) {
- pixels[loc] = color(s*2, s*2.1, s*2.4);
- }
- else if ((diff > 30) && (diff < 39)) {
- pixels[loc] = color(r, g, b) / 3;
- } else if ((diff > 40) && (diff < 49)) {
- pixels[loc] = color(r, g, b) / 5;
- } else {
- pixels[loc] = color(r*diff, g*diff, b*diff);
- }
- }
- }
- updatePixels();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement