Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. // Daniel Shiffman
  2. // http://codingtra.in
  3. // http://patreon.com/codingtrain
  4. // Code for: https://youtu.be/QLHMtE5XsMs
  5.  
  6. import processing.video.*;
  7. import processing.serial.*;
  8.  
  9. Capture video;
  10. Serial myPort;
  11.  
  12. PImage pprev;
  13. PImage prev;
  14. PImage comprasion; //This will be the result of combining pprev with prev
  15.  
  16. float threshold = 60;
  17.  
  18. float motionX = 0;
  19. float motionY = 0;
  20.  
  21. float lerpX = 0;
  22. float lerpY = 0;
  23.  
  24.  
  25. void setup()
  26. {
  27.   size(640, 480);
  28.   //myPort = new Serial(this, "COM1", 9600);
  29.   //String[] cameras = Capture.list();
  30.   //printArray(cameras);
  31.   video = new Capture(this, width, height, 30);
  32.   video.start();
  33.   pprev = createImage(640, 480, RGB);
  34.   prev = createImage(640, 480, RGB);
  35.   comprasion = createImage(640, 480, RGB);
  36.   // Start off tracking for red
  37. }
  38.  
  39.  
  40. void captureEvent(Capture video)
  41. {
  42.   prev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
  43.   prev.updatePixels();
  44.  
  45.   video.read(); //loading first frame
  46.   pprev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
  47.   pprev.updatePixels();
  48.  
  49.   video.read(); //loading second frame
  50. }
  51.  
  52. void draw()
  53. {  
  54.   video.loadPixels();
  55.   pprev.loadPixels();
  56.   prev.loadPixels();
  57.   comprasion.loadPixels();
  58.  
  59.   image(video, 0, 0);
  60.  
  61.   //threshold = map(mouseX, 0, width, 0, 100);
  62.   threshold = 40;
  63.   int count = 0;
  64.   float avgX = 0;
  65.   float avgY = 0;
  66.  
  67.   loadPixels();
  68.  
  69.   for (int x = 0; x < video.width; x++ )
  70.   {
  71.     for (int y = 0; y < video.height; y++ )
  72.     {
  73.       int loc = x + y * video.width;
  74.      
  75.       comprasion = GetFrame(pprev, prev);
  76.      
  77.       color currentColor = video.pixels[loc];
  78.       float r1 = red(currentColor);
  79.       float g1 = green(currentColor);
  80.       float b1 = blue(currentColor);
  81.      
  82.       color prevColor = comprasion.pixels[loc];
  83.       float r2 = red(prevColor);
  84.       float g2 = green(prevColor);
  85.       float b2 = blue(prevColor);
  86.      
  87.  
  88.       float d = distSq(r1, g1, b1, r2, g2, b2);
  89.  
  90.       if (d > threshold*threshold) {
  91.         //stroke(255);
  92.         //strokeWeight(1);
  93.         //point(x, y);
  94.         avgX += x;
  95.         avgY += y;
  96.         count++;
  97.         //myPort.write('1');
  98.         //pixels[loc] = color(0);
  99.       } else {
  100.         //pixels[loc] = color(255);
  101.       }
  102.     }
  103.   }
  104.   updatePixels();
  105.  
  106.   //We decide to detect motion or not
  107.   if (count > 4000)
  108.   {
  109.     //myPort.write('1');
  110.     motionX = avgX / count;
  111.     motionY = avgY / count;
  112.     // Draw a circle at the tracked pixel
  113.  
  114.  
  115.   lerpX = lerp(lerpX, motionX, 0.2);
  116.   lerpY = lerp(lerpY, motionY, 0.2);
  117.  
  118.   fill(255, 50, 50);
  119.   strokeWeight(2.0);
  120.   stroke(0);
  121.   ellipse(lerpX, lerpY, 36, 36);
  122.   }
  123.   else
  124.   {
  125.     //myPort.write('0');
  126.   }
  127.   //image(video, 0, 0);y
  128.   //image(prev, 100, 0, 100, 100);
  129.  
  130.   //println(mouseX, threshold);
  131. }
  132.  
  133. float distSq(float x1, float y1, float z1, float x2, float y2, float z2)
  134. {
  135.   float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  136.   return d;
  137. }
  138.  
  139. PImage GetFrame (PImage a, PImage b) //This fuction should return an average frame of two previous frames.
  140. {
  141.   PImage c = createImage(640, 480, RGB);
  142.  
  143.   a.loadPixels();
  144.   b.loadPixels();
  145.   c.loadPixels();
  146.  
  147.    for (int x = 0; x < video.width; x++ )
  148.    {
  149.     for (int y = 0; y < video.height; y++ )
  150.     {
  151.       int loc = x + y * video.width;
  152.      
  153.       c.pixels[loc] = a.pixels[loc]*(99/100) + b.pixels[loc]*(1/100); //This should fill c image with correct pixels.  
  154.  
  155.     }
  156.    }
  157.    updatePixels();
  158.   return c;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement