Capture movie and video (It is all wrong?)

edited February 2018 in Library Questions

Hello, I am very new on processing and try to make a capture movie with recorded video, but unfortunately, I can not fix it for many days and nights. I am very tired of all this. I want to quit but must to do for a college, and maybe in the future for installation. Can any of you help me please, and show me where is the problem(s)? Thank you (sorry for my English languages, are not my native languages)

                                        import processing.video.*;

                                        int numPixels;
                                        // Number of columns and rows in the system
                                        int[] previousFrame;
                                        // Variable for capture device
                                        Movie movie;
                                        Capture video;


                                        void setup() { 
                                           size(640, 480);
                                          movie = new Movie(this, "VROXI.mp4"); 
                                          movie.loop();
                                        }

                                        // Step 4. Read new frames from the movie.
                                        void movieEvent(Movie movie) {  
                                          movie.read();
                                        }

                                        // Step 5. Display movie.
                                        void draw() {
                                          image(movie, 0, 0);
                                          video = new Capture(this, width, height);

                                          // Start capturing the images from the camera
                                          video.start(); 
                                          video.read(); // Read the new frame from the camera
                                          video.loadPixels(); // Make its pixels[] array available

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

                                        {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
                                        }
                                        }

Answers

  • edit post, highlight code, press ctrl-o to format...

  • It's ok now. :)

  • video = new Capture(this, width, height);
    

    this shouldn't be within draw. this is you creating a new Capture every frame. move it to setup()

    lines 40 to 64 aren't even in a method. i don't know what that is about. same with lines 65 onwards.

  • Thanks for the reply. I tried to do Frame Differencing but with a video. So already have a sketch with a video, and the other one from the library (Fr. Dif.) and then mix those together. But only video plays, but none capture.

  • Answer ✓

    do you want Capture though? he library reference says:

    "The Capture class grabs frames of video from an attached capture device such as a camera."

  • edited February 2018

    Hello again my dear, can you tell me about this? It's one that I did it before a month for rain sketch and added a frame differencing example. So it works only one, and webcamera it's open, but without result (no captures). Please help me!

    `import processing.video.*;

    Capture video; Drop[] drops = new Drop[500];

    void setup() { size(320, 240); for (int i = 0; i < drops.length; i++) { drops[i] = new Drop(); } }

    class Drop { float x = random(width); float y = random(-400, -80); float yspeed = random (6, 12); float len = random(10, 20);

    void fall() { y = y + yspeed; yspeed = yspeed + 0.1;

    if (y > height) {
      y = random(-200, -100);
      yspeed = random(4, 10);
    }
    

    }

    void show() { stroke(#EEEDF0); line(x, y, x, y+len); } }

    void draw() { background (#201F24); for (int i = 0; i < drops.length; i++) { drops[i].fall(); drops[i].show(); } import processing.video.*;

    int numPixels; int[] previousFrame; Capture video;

    // 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(); 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
    }
    

    } }`

  • edit post, highlight code, press ctrl-o to format...

  • is your input a video from a file or video from a camera?

  • The first it is from file mp4. The new one it is from file, sketch I done. Here you can see it. [https://www.openprocessing.org/sketch/483954]

    But I want to add Capture movie, and don't know how.

    (Hope to understand my horrible english)

  • Answer ✓

    the Capture class is for getting images from cameras - this is not what you want

    there is a video-export library for making movies. https://www.funprogramming.org/VideoExport-for-Processing/

    or you can just save the frames to disk and create a video from them later.

  • ignore the frame-differencing for now.

    just get the video playing.

    then add saveFrame() to get it saving those frames.

    post the code. format it nicely so we can read it.

  • edited February 2018

    Than you very much for your advices!

    I do not want to make any videos. I would like to play videos and also have a vision, perhaps with a capture ... (?) I tried once with a video mp4, and then with a sketch. Maybe stay to the last one and try with this. Anyway, I'm very confused.

        import processing.video.*;
    
        Capture video;
        Drop[] drops = new Drop[500];
    
        void setup() {
          size(320, 240);
          for (int i = 0; i < drops.length; i++) {
            drops[i] = new Drop();
          }
        }
    
        class Drop { 
          float x = random(width);
          float y = random(-400, -80);
          float yspeed = random (6, 12);
          float len = random(10, 20);
    
          void fall() {
            y = y + yspeed;
            yspeed = yspeed + 0.1;
    
            if (y > height) {
              y = random(-200, -100);
              yspeed = random(4, 10);
            }
          }
    
          void show() {
            stroke(#EEEDF0);
            line(x, y, x, y+len);
          }
        }
    
        void draw() {
          background (#201F24);
          for (int i = 0; i < drops.length; i++) {
            drops[i].fall();
            drops[i].show();
          }
    
          import processing.video.*;
    
          int numPixels;
          int[] previousFrame;
          Capture video;
    
    
          // 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();
          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
            }
          }
        }
    
  • edit post, highlight code, press ctrl-o to format.

    we can't fix what we can't read.

  • Sorry I can't fix it. I tried many times but seems wrong. If you can understanding, it's ok. If not never mind. :)

  • Ohhhhh! Now it's ok!!! (after many, many times!)

  • I do not want to make any videos

    but your first post said

    Hello, I am very new on processing and try to make a capture movie with recorded video

    i am confused.

    do you want the video in the background with the rain over the top of it?

    where does the frame differencing come in?

Sign In or Register to comment.