Resize the resolution of a webcam during the sketch

edited December 2015 in Library Questions

Hi yesterday I posted this at the old forum but I cannot find the discussion back anymore so I will repeat my question here.

I am using the processing video library to measure movement. I am using a Webcam as source material and for the measurements the size or resolution needs to be small. But when i am taking a snapshot and use saveFrame I want to use the full resolution.

I believe that this is because the camera keeps recording at the definition stated here:

cam = new Capture (this, VIDEO_WIDTH, VIDEO_HEIGHT, 15);

I have tried adding this before the snapshot:

              cam.read();
              image(cam, 0, 0, 1280, 720);
              delay(100);
              saveFrame("test/line-####.bmp");
              previousFoto=millis();
              println("foto");
              delay(100);
              background(0);

But the resolution of the camera is still the old resolution set at the beginning. The image is stretched to fit the 1280*720. How can I change the resolution.

Tagged:

Comments

  • You can't change the resolution of your camera input while the sketch is running. I'd suggest to use a high res camera input and scale it down whenever you need to.

    To check what resolution and framerate combinations your camera supporst just run this String[] cameras = Capture.list(); println(cameras);

    There's a small tutorial about it here, too: http://processing.org/reference/libraries/video/Capture.html

  • edited October 2013

    I tried adapting the code but it hasnt worked out yet. I use this code as a basis of my project, all credits to creativecoding.org.

    the max res of my camera with which I want to saveFrame is 1280x720 the measurement should be done at 1280/2 x 720/2.

    Where should I make the changes?

    ` import processing.video.*;

    final int VIDEO_WIDTH = 1280; final int VIDEO_HEIGHT = 720; // Anzahl der Quadranten in der Breite final int VIDEO_COLS = 20; // Anzahl der Quadranten in der Höhe final int VIDEO_ROWS = 20;

    float[] activity = new float[VIDEO_COLS * VIDEO_ROWS]; float[] buffer1 = new float[VIDEO_WIDTH * VIDEO_HEIGHT]; float[] buffer2 = new float[buffer1.length]; float[] buffer3 = new float[buffer1.length];

    Capture cam = null;

    void setup () { size (VIDEO_WIDTH, VIDEO_HEIGHT); cam = new Capture (this, VIDEO_WIDTH, VIDEO_HEIGHT, 15); frameRate (15); }

    void draw () {

    if (cam.available ()) {

    cam.read ();
    
    int index;
    int pxPerCol = VIDEO_WIDTH / VIDEO_COLS;
    int pxPerRow = VIDEO_HEIGHT / VIDEO_ROWS;
    
    image (cam, 0, 0);
    
    // 'activity' array auf 0 setzen
    for (int i=0; i < activity.length; i++) {
      activity[i] = 0;
    }
    
    // Für jedes Pixel im Videoraster
    for (int i=0; i < cam.pixels.length; i++) {
    
      // x und y Position des Quadranten berechnen
      int x = (int) ((i % cam.width) / pxPerCol);
      int y = (int) ((i / cam.width) / pxPerRow);
    
      // Quadranten-Zugehörigkeit des Pixels herausfinden.
      // Später wichtig für das 'activity' array.
      index = y * VIDEO_COLS + x;
    
      // Farbe an der Position 'i' im Kamerabild
      color col = cam.pixels[i];
      // Die Summe aus allen drei Farbkanälen bilden
      float sum = red (col) + green (col) + blue (col);
    
      // Farbwertänderung des Pixels bezogen auf alle Bilder errechnen
      float deltaPixel = (buffer1[i] + buffer2[i] + buffer3[i]) / 3 - sum;
    
      if (deltaPixel < 0) {
        deltaPixel *= -1;
      }
    
      // Die Änderung auf den Gesamtwert des Quadranten addieren
      activity[index] += deltaPixel;
    
      // Verschiebe das 'Bildgedächnis' um einen Rutsch nach hinten.
      buffer3[i] = buffer2[i];
      buffer2[i] = buffer1[i];
      buffer1[i] = sum;
    }
    
    // Für jeden Quadranten
    for (int i=0; i < activity.length; i++) {
    
      // Durchschnittliche Farbwertänderung berechnen.
      // Mit dem Teilen der Summe durch die Anzahl der Pixel
      activity[i] /= pxPerCol* pxPerRow;
    
      // Quadrant in das Sketchfenster zeichnen
      stroke (255, 20);
      fill (0, 255, 230, activity[i]);
      rect ((i % VIDEO_COLS) * pxPerCol, (i / VIDEO_COLS) * pxPerRow, pxPerCol, pxPerRow);
    }
    

    } }

    `

Sign In or Register to comment.