# Applying Frame differencing tool to video file and exporting data as a csv file?

**URL:** <https://discourse.processing.org/t/applying-frame-differencing-tool-to-video-file-and-exporting-data-as-a-csv-file/16457>\
**Category:** Libraries\
**Created:** [December 16, 2019, 10:25pm UTC](https://discourse.processing.org/t/applying-frame-differencing-tool-to-video-file-and-exporting-data-as-a-csv-file/16457 "2019-12-16T22:25:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Louis](https://avatars.discourse-cdn.com/v4/letter/l/ecb155/32.png) [@Louis](https://discourse.processing.org/u/Louis)\
**Post date:** [December 16, 2019, 10:25pm UTC](https://discourse.processing.org/t/applying-frame-differencing-tool-to-video-file-and-exporting-data-as-a-csv-file/16457/1 "2019-12-16T22:25:46Z")

</div>

Hi, very new to coding, but for a MA project I am trying to develop a script that will read movement in a series of videos I have created and enable me to export the data as a CSV file?

This existing tool is almost perfect, but rather than capturing through a webcam, what changes would I need to make to get the script to read existing video files? And could I get the script to produce a csv file of the numerical data shown in the console?

Frame differencing tool code:

```auto
import processing.video.*;

int numPixels;
int[] previousFrame;
Capture video;

void setup() {
  size(640, 480);
  
  // 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
  video.start(); 
  
  numPixels = video.width * video.height;
  // Create an array to store the previously captured frame
  previousFrame = new int[numPixels];
  loadPixels();
}

void draw() {
  if (video.available()) {
    // When using video to manipulate the screen, use video.available() and
    // video.read() inside the draw() method so that it's safe to draw to the screen
    video.read(); // 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 > 0) {
      updatePixels();
      println(movementSum); // Print the total amount of movement to the console
    }
  }
}

```

Any help welcome!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 17, 2019, 2:16am UTC](https://discourse.processing.org/t/applying-frame-differencing-tool-to-video-file-and-exporting-data-as-a-csv-file/16457/2 "2019-12-17T02:16:11Z")

</div>

> exporting data as a csv file

create a table,

make columns / for a CSV header line

on mouse click create record

on key [s] save CSV file

```auto
Table table;
String outfile = "data/test.csv"; 

void setup() {
  size(300, 300);
  makeTable();
  strokeWeight(5);
  println("use: mouseClick and key [s] for save to CSV");
}

void draw() {
}

void mousePressed() {
  makeLine();
  point(mouseX, mouseY);
}

void makeTable() {
  table = new Table();
  table.addColumn("time");
  table.addColumn("x");
  table.addColumn("y");
}

void makeLine() {
  TableRow newRow = table.addRow();
  newRow.setString("time", ""+year()+"_"+nf(month(), 2)+"_"+nf(day(), 2)+"_"+nf(hour(), 2)+"_"+nf(minute(), 2)+"_"+nf(second(), 2));
  newRow.setInt("x", mouseX);
  newRow.setInt("y", mouseY);
}

void keyPressed() {
  if ( key == 's' ) {
    saveTable(table, outfile);
    println("save to "+outfile);
  }
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 20, 2019, 11:38pm UTC](https://discourse.processing.org/t/applying-frame-differencing-tool-to-video-file-and-exporting-data-as-a-csv-file/16457/3 "2019-12-20T23:38:06Z")

</div>

> [@Louis](#):
>
> what changes would I need to make to get the script to read existing video files?

Begin by looking at the examples for video playback in the video library Examples menu.

```auto
import processing.video.*;
Movie movie;

void setup() {
  size(640, 360);
  movie2 = new Movie(this, "amovie.mp4");
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  if (movie.available() {
    // compute frame differencing on `movie` and store data
  }
}

```

I’m _assuming_ that you are computing inter-frame differences on a single movie – not comparing simultaneous frames on two different movies…?
