Making a Graph with Frame Differencing Outputs

Hello, I use the Frame Differencing code which gives the movementSum as outputs on a video. I want to make a line graph with this outputs. In my code, I combined a line graph code with the FrameDifferencing and I need to record this line graph stream. The problem is that when the graph goes out of the window, it spoiled. I can't make a graph with this outputs properly. Can you help me to fix this code?. And after making this graph, how can I record it? You can change the "MagicCamera Device" with your webcam name to run this code.

ArrayList<Points> poop = new ArrayList();

int numPixels;
int[] previousFrame;
Capture video;

void setup() {
  size(320, 240);
  video= new Capture(this, width, height, "MagicCamera Device" );
  // This the default video input, see the GettingStartedCapture 
  // example if it creates an error
  // video = new Capture(this, width, height);

  // Start capturing the images from the camera

  numPixels = video.width * video.height;

  // Create an array to store the previously captured frame
  previousFrame = new int[numPixels];


void draw() {
  if (video.available()) {
    // When using video to manipulate the screen, use video.available() and
    // inside the draw() method so that it's safe to draw to the screen; // Read the new frame from the camera
    video.loadPixels(); // Make its pixels[] array available

    int movementSum = 0; // Amount of movement in the frame
    for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
      color currColor = video.pixels[i];
      color prevColor = previousFrame[i];
      // Extract the red, green, and blue components from current pixel
      int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;
      // Extract red, green, and blue components from previous pixel
      int prevR = (prevColor >> 16) & 0xFF;
      int prevG = (prevColor >> 8) & 0xFF;
      int prevB = prevColor & 0xFF;
      // Compute the difference of the red, green, and blue values
      int diffR = abs(currR - prevR);
      int diffG = abs(currG - prevG);
      int diffB = abs(currB - prevB);
      // Add these differences to the running tally
      movementSum += diffR + diffG + diffB;
      // Render the difference image to the screen
      pixels[i] = color(diffR, diffG, diffB);
      // The following line is much faster, but more confusing to read
      //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
      // Save the current color into the 'previous' buffer
      previousFrame[i] = currColor;
    // To prevent flicker from frames that are all black (no movement),
    // only update the screen if the image has changed.
    if (movementSum > 50) {


      float t = movementSum/2000;
  Points P = new Points(width, t );

      println(movementSum); // Print the total amount of movement to the console

  for (int i=0;i<poop.size();i++) {
    Points P = (Points)poop.get(i);
    //vertex(P.x, P.y);
    if (P.x<0)poop.remove(i);
class Points {
  float x, y;
  Points(float x, float y) {
    this.x = x;
    this.y = y;

Ekran Alıntısı


  • Two ways to do it. When your line reaches the end of the window (the right edge) you can stop the video or start from the left again, just like and electrocardiogram.

    Or after you process all your images from the webcam, then at the end, based on how many points you have, you can map your points to the width of the sketch. The point of this approach is that one needs to know how many points are stored before drawing the line.

    How can you recorded? You can save the points or you could save the image by calling saveFrame: You could also save it to a pdf object:


Sign In or Register to comment.