Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1.  
  2. import processing.video.*;
  3. Capture video;
  4. PImage prevFrame;
  5.  
  6.  
  7.  
  8. void setup() {
  9. size(640, 480);
  10. video = new Capture(this, width, height, 30);
  11. video.start();
  12. prevFrame = createImage(video.width, video.height, RGB);
  13.  
  14. String[] cameras = Capture.list();
  15. if (cameras.length == 0) {
  16. println("There are no cameras available for capture.");
  17. exit();
  18. } else {
  19. println("Available cameras:");
  20. }
  21. for (int i = 0; i < cameras.length; i++) {
  22. println(cameras[i]);
  23. }
  24.  
  25.  
  26. }
  27.  
  28. void captureEvent(Capture video) {
  29. // Save previous frame for motion detection!!
  30. 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!
  31. prevFrame.updatePixels(); // Read image from the camera
  32. video.read();
  33. }
  34.  
  35. void draw() {
  36.  
  37. loadPixels();
  38. video.loadPixels();
  39. prevFrame.loadPixels();
  40.  
  41. // Begin loop to walk through every pixel
  42. for (int x = 0; x < video.width; x ++ ) {
  43. for (int y = 0; y < video.height; y ++ ) {
  44. int loc = x+y*width;
  45. float r = red(video.pixels[loc]);
  46. float g = green(video.pixels[loc]);
  47. float b = blue(video.pixels[loc]);
  48. pixels[loc] = color(r, g, b) / 2;
  49.  
  50. color current = video.pixels[loc]; // Step 2, what is the current color
  51. color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
  52.  
  53. // Step 4, compare colors (previous vs. current)
  54. float r1 = red(current);
  55. float g1 = green(current);
  56. float b1 = blue(current);
  57. float r2 = red(previous);
  58. float g2 = green(previous);
  59. float b2 = blue(previous);
  60. float diff = dist(r1, g1, b1, r2, g2, b2);
  61.  
  62. // Step 5, How different are the colors?
  63. // If the color at that pixel has changed, then there is motion at that pixel.
  64.  
  65. int s = second();
  66.  
  67.  
  68. if ((diff > 20) && (diff < 29)) {
  69. pixels[loc] = color(r, g, b) / 2;
  70. } else if (diff < 5) {
  71. pixels[loc] = color(s*2, s*2.1, s*2.4);
  72. }
  73. else if ((diff > 30) && (diff < 39)) {
  74. pixels[loc] = color(r, g, b) / 3;
  75. } else if ((diff > 40) && (diff < 49)) {
  76. pixels[loc] = color(r, g, b) / 5;
  77. } else {
  78. pixels[loc] = color(r*diff, g*diff, b*diff);
  79. }
  80. }
  81. }
  82.  
  83. updatePixels();
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement