CV Problems

edited May 2015 in Library Questions

Hi guys;

I'm pretty new to Processing, so these problems are probably really easily fixed, but hey-oh.

I'd really appreciate any insight you can offer!

I'm getting the error ArrayIndexOutOfBoundsException:0 when I try to run this code:

import processing.video.*;

Capture cam;
PImage frame;
PImage destination;

void setup() {

  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int x=0;x<cameras.length;x++) {
      println(cameras[x]);
    }

    cam = new Capture(this, cameras[0]);
    cam.start(); 
  }

  size(360,240);

  frame = createImage(cam.width,cam.height,RGB);
  destination = createImage(cam.width,cam.height,RGB);

  println("setup");

}

void draw() {
  if (cam.available() == true) {
    cam.read();
    println("cam read");
  }

  cam.loadPixels();
  frame.loadPixels();

  for(int x=0;x<cam.width;x++) {
    for (int y=0;y<cam.height;y++) {
      int loc = x+y*cam.width;
      frame.pixels[loc] = cam.pixels[loc];
      }
    }

  println("pixels copied");

  int threshold = 127;

  for(int x=0;x<cam.width;x++) {
    for (int y=0;y<cam.height;y++) {
      int loc = x+y*cam.width;
      if (brightness(frame.pixels[loc]) <= threshold) {
        destination.pixels[loc] = color(0);
      } else {
        destination.pixels[loc] = color(255);
      }
     }
   }

  destination.updatePixels();
  image(destination,0,0);

  println("printed");
}

Thanks in advance!

P.S. The code isn't displaying correctly in my browser, if it happens for anyone else I apologise! EDIT: it's working now, thanks to GoToLoop.

Answers

  • Aha, thank you!

  • edited May 2015 Answer ✓

    Seems like somehow cam.pixels[] was mysteriously a 0-length array at times. :-/
    Rather than trying to find out why, I've just picked a working Capture example I've already got here.
    Then thrown in the pixels[] & brightness() stuff back in order to have a similar sketch to yours.

    /**
     * B&W Capture (v2.2)
     * by GoToLoop (2015/May/25)
     * forum.processing.org/two/discussion/10986/cv-problems
     */
    
    import processing.video.Capture;
    Capture cam;
    PImage bw;
    
    volatile boolean requestDraw, isBusy;
    static final int BW_THRESHOLD = 0200;
    
    void setup() {
      initFeed();
    
      size(cam.width, cam.height, JAVA2D);
      bw = createImage(width, height, RGB);
    
      noLoop();
      frameRate(cam.frameRate);
      println(cam.frameRate);
    
      registerMethod("post", this);
    }
    
    void draw() {
      color[] camPix = cam.pixels, bwPix = bw.pixels;
      int idx = 0, len = camPix.length;
    
      isBusy = true;
      while (idx != len)
        bwPix[idx] = brightness(camPix[idx++]) < BW_THRESHOLD? 0 : -1;
      isBusy = false;
    
      bw.updatePixels();
      background(bw);
    
      frame.setTitle(str(round(frameRate)));
    }
    
    void post() {
      if (requestDraw)  requestDraw = !(redraw = true);
    }
    
    void captureEvent(Capture c) {
      while (isBusy)  delay(1);
      c.read();
      redraw = requestDraw = true;
    }
    
    void initFeed() {
      if (cam != null)  return;
    
      String[] cams = Capture.list();
      printArray(cams);
      println();
    
      ( cam = new Capture(this, cams[1]) ).start();
      while (cam.width == 0)  delay(10);
    }
    
  • Oh, wonderful! Thank you very much :-) Thoroughly appreciated!

  • edited July 2015

    Glad you've liked it! >:D<
    BtW, here's v2.3 which I believe it's got a better performance than v2.2!
    This time it uses a separate thread("") just for the RGBtoBW() conversion: :ar!

    /**
     * B&W Capture (v2.33)
     * by GoToLoop (2015/May/25)
     * forum.processing.org/two/discussion/10986/cv-problems
     */
    
    import processing.video.Capture;
    Capture cam;
    PImage bw = new PImage();
    
    volatile boolean requestDraw;
    static final int BW_THRESHOLD = 040, FPS_ADJUST = 5, CAM = 1;
    
    void setup() {
      initFeed();
    
      size(cam.width, cam.height, JAVA2D);
      bw = createImage(width, height, RGB);
    
      noLoop();
      float canvasFPS = cam.frameRate + FPS_ADJUST;
      frameRate(canvasFPS);
      println("Cam's FPS:",cam.frameRate, "\t Canvas's FPS:", canvasFPS);
    
      registerMethod("post", this);
      thread("RGBtoBW");
    }
    
    void draw() {
      synchronized (bw) {
        background(bw);
        frame.setTitle(str(round(frameRate)));
      }
    }
    
    void post() {
      if (requestDraw)  requestDraw = !(redraw = true);
    }
    
    void captureEvent(Capture c) {
      synchronized (bw) {
        c.read();
        bw.notify();
        redraw = requestDraw = true;
      }
    }
    
    void initFeed() {
      if (cam != null)  return;
    
      String[] cams = Capture.list();
      printArray(cams);
      println("\nChosen Cam #" + CAM + ':', cams[CAM]);
    
      ( cam = new Capture(this, cams[CAM]) ).start();
      while ((cam.width & cam.height) == 0)  delay(10);
    }
    
    void RGBtoBW() {
      synchronized (bw) {
        final color[] camPix = cam.pixels, bwPix = bw.pixels;
        final int len = camPix.length;
    
        for (int idx = 0;; idx = 0)  try {
          bw.wait();
    
          while (idx != len)  bwPix[idx]
            = brightness(camPix[idx++]) < BW_THRESHOLD? 0 : -1;
    
          bw.updatePixels();
        }
    
        catch (InterruptedException interrupt) {
          interrupt.printStackTrace();
        }
      }
    }
    
  • Oh, wow, thank you! I'll now set about running this and trying to figure out what on earth it all does... Thank you!

  • Oh my goodness, thank you! I really don't know what to say, I wasn't expecting this much information :-)

Sign In or Register to comment.