Old Processing 1.0 + OpenCV code...

edited March 2017 in Kinect

Hey all,

I wrote this code back in 2011 (So long ago!) and am trying to resurrect it. Essentially it was a video grid that captured 10 frames and constantly looped them. I notice that the old OpenCV library doesn't seem to work anymore, and I can't seem to use it with the new library.

Any ideas?


import hypermedia.video.*;
OpenCV opencv;

int grid = 5; // grid dimensions
int numFrames = 10; // number of frames per loop
boolean randomGrid = false; // consecutive or random grid

PGraphics[] frames = new PGraphics[numFrames];
ArrayList randomNumbers = new ArrayList();
int t;

void setup() {
  size(640,480);
  opencv = new OpenCV(this);
  opencv.capture(width/grid,height/grid);
  for (int i=0; i<frames.length; i++) {
    frames[i] = createGraphics(width,height,P2D);
  }
  fillRandomNumbers();
}

void draw() {
  int currentFrame = frameCount % frames.length;
  opencv.read();
  if (frameCount % numFrames == 0) {
    if (randomGrid) { t = getRandomGrid(); }
    else { t++; }
  }
  int x = t%grid;
  int y = (t/grid)%grid;
  frames[currentFrame].beginDraw();
  frames[currentFrame].image(opencv.image(),x*width/grid,y*height/grid);
  frames[currentFrame].endDraw();
  image(frames[currentFrame],0,0);
}

int getRandomGrid() {
  if (randomNumbers.size() == 0) { fillRandomNumbers(); }
  int selected = int(random(randomNumbers.size()));
  int randomNumber = (Integer) randomNumbers.get(selected);
  randomNumbers.remove(selected);
  return randomNumber;
}

void fillRandomNumbers() {
  for (int i=0; i<grid*grid; i++) {
    randomNumbers.add(i);
  }
}

public void stop() {
  opencv.stop();
  super.stop();
}

Answers

  • I am not sure why the OpenCV part is not working. Maybe you could check the documentation or more recent examples. I notice you are not doing any processing using OpenCV. I would replace all your OpenCV calls with a Capture object and it should work. For more information, go to the Processing Foundation's webapge and check the page libraries >> Video and any Capture entry. Or install the video library and then explore the capture examples provided.

    Kf

  • Thanks @kfrajer!

    I managed to turn it into all capture.. I have it working except when I want to change

    cam = new Capture(this, width, height/grid);

    into

    cam = new Capture(this, width/grid, height/grid);

    It is just displaying black??

    full code:

    // Based on the work by Ross Philips
    
    `import processing.video.*;
    Capture cam;
    
    float x= random(-100,100);
    int endtime = 3000; //end time of the production
    int looptime = 25;
    int grid = 3; // grid dimensions
    int numFrames = looptime; // number of frames per loop
    boolean randomGrid = false; // consecutive or random grid
    
    
    PGraphics[] frames = new PGraphics[numFrames];
    ArrayList randomNumbers = new ArrayList();
    int t;
    
    void setup() {
    size(640,480);
    cam = new Capture(this, width, height/grid);
    for (int i=0; i<frames.length; i++) {
    frames[i] = createGraphics(width,height);
    }
    fillRandomNumbers();
    cam.start();
    }
    
    
    void draw() {
    
    int currentFrame = frameCount % frames.length;
    cam.read();
    
    //recording loop
    if (frameCount % numFrames == 0) {
    if (randomGrid) { t = getRandomGrid(); }
    else { t++; }
    }
    int x = t%grid;
    int y = (t/grid)%grid;
    frames[currentFrame].beginDraw();
    frames[currentFrame].image(cam,x*width/grid,y*height/grid);
    frames[currentFrame].endDraw();
    image(frames[currentFrame],0,0);
    
    
    }
    
    
    //playing back loop
    int getRandomGrid() {
    if (randomNumbers.size() == 0) { fillRandomNumbers(); }
    int selected = int(random(randomNumbers.size()));
    int randomNumber = (Integer) randomNumbers.get(selected);
    randomNumbers.remove(selected);
    return randomNumber;
    }
    
    void fillRandomNumbers() {
    for (int i=0; i<grid*grid; i++) {
    randomNumbers.add(i);
    }
    }
    
    public void stop() {
    cam.stop();
    super.stop();
    }`
    
  • Thanks @kfrajer!

    I managed to turn it into all capture.. I have it working except when I want to change

    cam = new Capture(this, width, height/grid);

    into

    cam = new Capture(this, width/grid, height/grid);

    It is just displaying black??

    full code:

    // Based on the work by Ross Philips
    
    `import processing.video.*;
    Capture cam;
    
    float x= random(-100,100);
    int endtime = 3000; //end time of the production
    int looptime = 25;
    int grid = 3; // grid dimensions
    int numFrames = looptime; // number of frames per loop
    boolean randomGrid = false; // consecutive or random grid
    
    
    PGraphics[] frames = new PGraphics[numFrames];
    ArrayList randomNumbers = new ArrayList();
    int t;
    
    void setup() {
    size(640,480);
    cam = new Capture(this, width, height/grid);
    for (int i=0; i<frames.length; i++) {
    frames[i] = createGraphics(width,height);
    }
    fillRandomNumbers();
    cam.start();
    }
    
    
    void draw() {
    
    int currentFrame = frameCount % frames.length;
    cam.read();
    
    //recording loop
    if (frameCount % numFrames == 0) {
    if (randomGrid) { t = getRandomGrid(); }
    else { t++; }
    }
    int x = t%grid;
    int y = (t/grid)%grid;
    frames[currentFrame].beginDraw();
    frames[currentFrame].image(cam,x*width/grid,y*height/grid);
    frames[currentFrame].endDraw();
    image(frames[currentFrame],0,0);
    
    
    }
    
    
    //playing back loop
    int getRandomGrid() {
    if (randomNumbers.size() == 0) { fillRandomNumbers(); }
    int selected = int(random(randomNumbers.size()));
    int randomNumber = (Integer) randomNumbers.get(selected);
    randomNumbers.remove(selected);
    return randomNumber;
    }
    
    void fillRandomNumbers() {
    for (int i=0; i<grid*grid; i++) {
    randomNumbers.add(i);
    }
    }
    
    public void stop() {
    cam.stop();
    super.stop();
    }`
    
  • edited April 2017

    @scttee -- if the sketch is doing this:

    size(640,480);
    cam = new Capture(this, width/grid, height/grid);
    

    ...then it resolves to:

    cam = new Capture(this, 213, 160);
    

    This doesn't work on my laptop cam. However, these work:

    cam = new Capture(this, width/2, height/3);
    cam = new Capture(this, 320, 160);
    

    I'm not sure, but I believe that Capture() cannot specify an arbitrary requestWidth and requestHeight.

    Instead, these must be values from a list of frame modes supported by the camera -- and cameras often support values like 160,320,640 but seldom 213.

  • @scttee

    Use this line String[] cameras = Capture.list(); to get access to the resolution available (and fps) for your cameras detected in your system. Just follow th example here:

    https://processing.org/reference/libraries/video/Capture.html

    Kf

Sign In or Register to comment.