cam.get(x*2, y*2) doesn't work, why?

edited January 2016 in Library Questions

hi, processers! I have been going through the funprogramming.com tutorial no. 126 and wanted to do the same thing, but using a webcam as a source to read. So I came up with this simple analog:

import processing.video.*;

Capture cam;

void setup() {
  size(640, 480);
  colorMode(HSB, 1);
  noStroke();
  smooth();

  cam = new Capture(this, 640, 480, 30);
  cam.start();
}

void draw() {
  if (cam.available()) {
    cam.read();
  }

  int x = width/2;
  int y = height/2;
  for (int i=0; i<100; i++) {
    int c = cam.get(320, 240); // or cam.get(x*2, y*2), same result as cam.get(cam.width/2, cam.height/2)
    float h = hue(c);
    float s = saturation(c);
    float b = brightness(c);
    fill(h, s, b);
    ellipse(x, y, s*20, s*20);

    x += sin(h*7) * b * random(40);
    y += cos(h*7) * b * random(40);
  }
}

however it seems processing is drawing ellipses from the middle of the screen towards the sides, not randomly in any position on the screen. Do you have any ideas why is that happening?

Cheers!

Answers

  • This is because your x and y values are set at int x = width/2; int y = height/2. You then go into a loop to place your ellipses 'randomly'. They will never be really random, as each time through your loop, you are doing x += sin(h*7) * b * random(40) (and something similar for your y coordinates). So you x and y coordinates are always going to end up being placed somewhere around the middle of the screen (with some randomness). You might want to think about something more like int x =(int) random(0, width) and int y = (int)random(0,height).

  • edited January 2016

    but the same lines set to determine x and y values seem to work fine for original code:

    `import processing.video.*;

    String PATH = "C:/Users/Om/Videos/myvid.mp4"; Movie mov;

    void setup() { size(640, 360); frameRate(30); mov = new Movie(this, PATH); mov.play(); mov.speed(1); mov.volume(1); colorMode(HSB, 1); background(1); noStroke(); }

    void movieEvent(Movie m) { m.read(); }

    void draw() {

    int x = width/2; int y = height/2;

    for (int i=0; i<100; i++) {

    int c = mov.get(x*2, y*2);
    
    float h = hue(c);
    float s = saturation(c);
    float b = brightness(c);
    
    fill(h, s, b);
    
    ellipse(x, y, s*20, s*20);
    
    x += sin(h*7) * b * random(40);
    y += cos(h*7) * b * random(40);
    
    if (x<0 || y<0 || x >= width || y>= height) {
      break;
    }
    

    } } `

    the only thing I changed was the source to read pixels from a video to webcam.

Sign In or Register to comment.