Storing & retrieving video camera frames in an ArrayList?

edited January 2016 in Library Questions

I'm trying to save frames from my MacBook's web camera to an Arraylist and then show three different images from the ArrayList.

The problem I'm having is that even though I'm pulling PImages from three different locations in the array (I think), I keep getting three of the exact same most recent images. Am I not referencing different locations in the ArrayList correctly?

Here's a stripped down version of the code on GitHub to show the problem I'm having:

Steve

Tagged:

Answers

  • that's 39 lines - you can easily post it here and more people will see it.

    line 28 is saving a reference to cam. you need a copy of the pixels, not a reference.

  • Answer ✓

    @sspboyd===

    try this one (from your "transformed" code )

    import processing.video.*;
    
    Capture cam;
    ArrayList<PImage> vidTimeLine = new ArrayList<PImage>();
    
    
    void setup() {
      size(640, 480);
      String[] cameras = Capture.list();
    
      if (cameras.length == 0) {
        exit();
      } else {
        cam = new Capture(this, 320, 240); // 640x480@15fps
        cam.start();     
      }      
    }
    
    void draw() {
    
      frameRate(15);// added
      if (cam.available() == true) {
        cam.read();
    PImage f = cam.get();// transformed
        vidTimeLine.add(f);
      }        
    
        int vtlSz = vidTimeLine.size();
    
    if(vtlSz > 500){ // 150 = 10 seconds at 15fps 
    
            // change values in order to see the differences
    
    image(vidTimeLine.get(200+int(random(20,150))), 0,0);// most recent frame
        image(vidTimeLine.get(vtlSz-int(random(1,100))), width/3,0);      // about 5 seconds into the past
            image(vidTimeLine.get(vtlSz-140+(int(random(25,80)))), 2*(width/3),0); // about 9 seconds into the past
    //      // three images are NO MORE the exact same when they are pulling from different index locations in the array?
        }
    }
    
  • Thanks very much akenaton and koogs. Totally fixed it. Now to deal with memory errors.

  • I'm trying to read frames from a image array without success. used your answer as inspiration. does anybody can help pleaseee?? thank you !!

    my code:

    // trying to implement a long exposure feature using kinect rgb camera.
    // when trying to flick thru images on the array, it doesn't seem to be saving images properly on array.
    // 
    // p.s. - could use onboard webcam too, just have to use kinect because that's the 
    // external camera i need to use.
    
    import org.openkinect.freenect.*;
    import org.openkinect.processing.*;
    Kinect kinect;
    
    
    int totalTime = 3200; // in miliseconds   
    int steps = 32;       // also means amount of pictures taken and merged later
    ArrayList<PImage> secondArray = new ArrayList<PImage>(steps+1);
    
    int delayPhotos = totalTime/ steps;
    long opac = 255 / steps; 
    
    void setup() {
      background(0);
      frameRate(15);
      size(640, 480);
    
    
      kinect = new Kinect(this);
      kinect.initVideo(); // inicia video rgb do kinect
    }
    
    void mousePressed()
    {
      PImage imageKinect = kinect.getVideoImage();
    
      for (int i = 1; i <= steps; i++) 
      {
        secondArray.add(imageKinect);           // saves frames no array
        imageKinect.save("image" + i + ".jpg"); // saves images to disk
        tint(255, opac * (steps - i));    // changes the opacity every step
        delay (delayPhotos);              // delay between each step
        blendMode(BLEND);
        print(i); 
        println(" " + opac * (steps - i));
        save("combined.jpg");
        image(imageKinect, 0, 0);
      }
    }
    
    void keyPressed()
    {
      println(key);
      if (key == 'p' || key == 'P') 
      {
        println("Prints sequence of images in Muybridge style");
        background(0);
    
        // trying temporarily for 32 images
        int w = 80;
        int h = 60; 
        int x = 0;
        int y = 0;
        tint(255, 255);
        image(secondArray.get(1), x, y, w, h);
        image(secondArray.get(5), 80, y, w, h);
        image(secondArray.get(8), 160, y, w, h);
        image(secondArray.get(15), 240, y, w, h);
        image(secondArray.get(20), 320, y, w, h);
        image(secondArray.get(22), 360, y, w, h);
    
        // error: can't get different images from array. seems to be
        //getting latest frame from camera, instead
    }
    }
    
    void draw() {
    
      //image(kinect.getVideoImage(), 0, 0);
    }
    
  • Is this the same question that's in this post?

  • Hello Kevin, Yes it is. I have seen this post before opening another thread. Thanks for your help!

    F.

Sign In or Register to comment.