Trying to get webcam feed to affect Pvector Array

Hello there Processing community! I hope you’re all doing well! :smiley:

As the title says, I’m attempting to feed the brightness input from my webcam to a vector array that has certain curves and variations applied to it. The intended output is a live effect applied to the webcam feed.

I’ve tried repurposing a sketch that used to receive input as a .jpg file located in the same folder as the sketch. When I run it I just get a black render window but no errors whatsoever so I’m quite confused.

I’ve made sure that my webcam is working properly. I’m working in Processing 2.

I’d be very appreciative of any assistance you could provide me.

Here’s my code:

import processing.video.*;

//load image/cam

//PImage img;
Capture cam;

// dynamic list with our points, PVector holds position
ArrayList<PVector> points = new ArrayList<PVector>();
 
// colors used for points
color[] pal = {
  color(0, 91, 197),
  color(0, 180, 252),
  color(23, 249, 255),
  color(223, 147, 0),
  color(248, 190, 0)
};
 
// global configuration
float vector_scale = 0.01; // vector scaling factor, we want small steps
float time = 0; // time passes by
 
void setup() {
  size(640, 480);  
  strokeWeight(0.66);
  background(0);
  noFill();
  smooth(8);
  
  //img = loadImage("IMAGE.jpg"); 
  cam = new Capture(this, 640, 480);
  cam.start();
  // noiseSeed(1111); // sometimes we select one noise field
 
  // create points from [-3,3] range
  for (float x=-3; x<=3; x+=0.07) {
    for (float y=-3; y<=3; y+=0.07) {
      // create point slightly distorted
      PVector v = new PVector(x+randomGaussian()*0.003, y+randomGaussian()*0.003);
      points.add(v);
    }
  }
}

void captureEvent(Capture cam) {  
  cam.read();
}

void draw() {
  
  int point_idx = 0; // point index
  for (PVector p : points) {
    // map floating point coordinates to screen coordinates
    float xx = map(p.x, -6.5, 6.5, 0, cam.width);
    float yy = map(p.y, -6.5, 6.5, 0, cam.height);
 
    // select color from palette (index based on noise)
    int cn = (int)(100*pal.length*noise(point_idx))%pal.length;
    stroke(pal[cn], 15);
    point(xx, yy); //draw
    
    // R - loading an image
    PVector v1 = sinusoidal(p,1);
 
    int cam_x = (int)map(p.x,-3,3,0,cam.width);
    int cam_y = (int)map(p.y,-3,3,0,cam.height);
 
    float b = brightness(cam.get(cam_x,cam_y) )/255.0;
    PVector br = kampyle(b);
 
    float a = 5 * PVector.angleBetween(br,v1);
 
    PVector v = astroid(a);

    p.x += vector_scale * v.x;
    p.y += vector_scale * v.y;
 
    // go to the next point
    point_idx++;
  }
  time += 0.001;
}

//-------------------------------------------------------------------------------//

// ALL CURVES PUT INTO FUNCTIONS //

PVector circle(float n) { // polar to cartesian coordinates
  return new PVector(cos(n), sin(n));
}
 
PVector astroid(float n) {
  float sinn = sin(n);
  float cosn = cos(n);
 
  float xt = sq(sinn)*sinn;
  float yt = sq(cosn)*cosn;
 
  return new PVector(xt, yt);
}
 
PVector cissoid(float n) {
  float sinn2 = 2*sq(sin(n));
 
  float xt = sinn2;
  float yt = sinn2*tan(n);
 
  return new PVector(xt, yt);
}
 
PVector kampyle(float n) {
  float sec = 1/sin(n);
 
  float xt = sec;
  float yt = tan(n)*sec;
 
  return new PVector(xt, yt);
}
 
PVector rect_hyperbola(float n) {
  float sec = 1/sin(n);
 
  float xt = 1/sin(n);
  float yt = tan(n);
 
  return new PVector(xt, yt);
}
 
final static float superformula_a = 1;
final static float superformula_b = 1;
final static float superformula_m = 6;
final static float superformula_n1 = 1;
final static float superformula_n2 = 7;
final static float superformula_n3 = 8;
PVector superformula(float n) {
  float f1 = pow(abs(cos(superformula_m*n/4)/superformula_a), superformula_n2);
  float f2 = pow(abs(sin(superformula_m*n/4)/superformula_b), superformula_n3);
  float fr = pow(f1+f2, -1/superformula_n1);
 
  float xt = cos(n)*fr;
  float yt = sin(n)*fr;
 
  return new PVector(xt, yt);
}

//-------------------------------------------------------------------------------//

// VARIATIONS AS VECTOR FIELD DEFINITITONS //

PVector sinusoidal(PVector v, float amount) {
  return new PVector(amount * sin(v.x), amount * sin(v.y));
}
 
PVector waves2(PVector p, float weight) {
  float x = weight * (p.x + 0.9 * sin(p.y * 4));
  float y = weight * (p.y + 0.5 * sin(p.x * 5.555));
  return new PVector(x, y);
}
 
PVector polar(PVector p, float weight) {
  float r = p.mag();
  float theta = atan2(p.x, p.y);
  float x = theta / PI;
  float y = r - 2.0;
  return new PVector(weight * x, weight * y);
}
 
PVector swirl(PVector p, float weight) {
  float r2 = sq(p.x)+sq(p.y);
  float sinr = sin(r2);
  float cosr = cos(r2);
  float newX = 0.8 * (sinr * p.x - cosr * p.y);
  float newY = 0.8 * (cosr * p.y + sinr * p.y);
  return new PVector(weight * newX, weight * newY);
}
 
PVector hyperbolic(PVector v, float amount) {
  float r = v.mag() + 1.0e-10;
  float theta = atan2(v.x, v.y);
  float x = amount * sin(theta) / r;
  float y = amount * cos(theta) * r;
  return new PVector(x, y);
}
 
PVector power(PVector p, float weight) {
  float theta = atan2(p.y, p.x);
  float sinr = sin(theta);
  float cosr = cos(theta);
  float pow = weight * pow(p.mag(), sinr);
  return new PVector(pow * cosr, pow * sinr);
}
 
PVector cosine(PVector p, float weight) {
  float pix = p.x * PI;
  float x = weight * 0.8 * cos(pix) * cosh(p.y);
  float y = -weight * 0.8 * sin(pix) * sinh(p.y);
  return new PVector(x, y);
}
 
PVector cross(PVector p, float weight) {
  float r = sqrt(1.0 / (sq(sq(p.x)-sq(p.y)))+1.0e-10);
  return new PVector(weight * 0.8 * p.x * r, weight * 0.8 * p.y * r);
}
 
PVector vexp(PVector p, float weight) {
  float r = weight * exp(p.x);
  return new PVector(r * cos(p.y), r * sin(p.y));
}
 
// parametrization P={pdj_a,pdj_b,pdj_c,pdj_d}
float pdj_a = 0.1;
float pdj_b = 1.9;
float pdj_c = -0.8;
float pdj_d = -1.2;
PVector pdj(PVector v, float amount) {
  return new PVector( amount * (sin(pdj_a * v.y) - cos(pdj_b * v.x)),
  amount * (sin(pdj_c * v.x) - cos(pdj_d * v.y)));
}
 
final float cosh(float x) { return 0.5 * (exp(x) + exp(-x));}
final float sinh(float x) { return 0.5 * (exp(x) - exp(-x));}

//-------------------------------------------------------------------------------//

//save frame on key press//
int number = 0;

void keyPressed(){
  if(key == 's'){
    println("Saving...");
    String s = "screen" + nf(number,4) +".png";
    save(s);
    number++;
    println("Done saving.");
  }
}
1 Like

kampyle is returning NAN at certain points. i think a few of the other funcs are a bit screwey too just check your math.

2 Likes

If you can’t narrow it down using error messages or exception handling then I’d suggest starting with something minimal that works, then building back up. So, start with a video feed. Works? Okay, now have a simple aspect of the video feed affect a rectangle? Works? Then start adding back on your functions one at a time…

1 Like