Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

stitching multiple sketches together

$
0
0

Im struggling to stitch my codes together to a random load of capture cams. Can someone help? i've been using this but I can't get past the first two sketches in the list, I don't know if it's getting more complicated for processing? I've included the first bit of code.

import processing.video.*;

final int numSketches=5;
int w=640;
int h=480;

int sketchToDo;
Capture cam;

boolean bright = true;
boolean greyScale;
int shiftAmount = 4;
int grid = 1;

//GlobalVariables allSketchesGlobalVariables;

void setup() {
  size(640, 480);
  sketchToDo = int( random( numSketches ) );
  switch( sketchToDo ) {
  case 0:
    setupSketch0(); // No size() calls!
    break;
  case 1:
    setupSketch1();
    break;
  }
}

void draw() {
  switch( sketchToDo ) {
  case 0:
    drawSketch0();
    break;
  case 1:
    drawSketch1();
    break;
  }
}

Glitch 1

import processing.video.*; Capture video;

PImage img1;
int w=640, h=480;

boolean bright = true;
boolean greyScale;
int shiftAmount = 4;
int grid = 1;


void setup() {
  size(640, 480);
  video = new Capture(this, 640, 480);
  video.start();
}

void draw() {
  loadPixels(); // Fills pixelarray
  float mouseMap = (int) map(mouseX, 0, width, 0, 255*3); // Brightness threshold mapped to mouse coordinates

if(shiftAmount > 24 || shiftAmount < 0){shiftAmount = 0;};

  for (int y = 0; y< h; y++)
  {
    for (int x = 0; x< w; x++)
    {
      color c = video.pixels[y*video.width+x];

      int a = (c >> 24) & 0xFF;
      int r = (c >> 16) & 0xFF;
      int g = (c >> 8) & 0xFF;
      int b = c & 0xFF;

      if (y %grid == 0) {

        if (bright)
        {
          if (r+g+b > mouseMap) {
            pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
          }
        }

        if (!bright)
        {
          if (r+g+b < mouseMap) {
            pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
          }
        }
      }
    }
  }
  updatePixels();

  if (greyScale) {
    filter(GRAY);
  }

  println("Shift amount: " + shiftAmount + " Frame rate: " + (int) frameRate + " Greyscale: " + greyScale) ;
}

void keyPressed()
// Keyboard controls
{
  switch(keyCode) {
  case UP:
    shiftAmount++;
    break;
  case DOWN:
    shiftAmount--;
    break;
  case LEFT:
    if (grid > 1) {
      grid--;
    }
    break;
  case RIGHT:
    grid++;
    break;
  case TAB:
    if (bright) {
      bright = false;
    }
    if (!bright) {
      bright = true;
    }
    break;
  case ENTER:
    if (!greyScale) {
      greyScale = true;
      break;
    }
    if (greyScale) {
      greyScale = false;
      break;
    }
  }
}

void captureEvent(Capture c) {
  c.read();
}
`

glitch 2

import processing.video.*;

Capture cam;

void setup() {
  size(640, 480);

  String[] cameras = Capture.list();
  cam = new Capture(this, width, height);
  cam.start();
  ellipseMode(CENTER);
}

void draw() {
  noStroke();
  background(255);
  for (int i = 0; i < width; i = i+20) {
    for (int j = 0; j < height; j = j+20) {
      fill(cam.get(i, j) * 4);
      ellipse(i, j, 20, 20);
    }
  }

  if (cam.available() == true) {
    cam.read();
  }
  //image(cam, 0, 0);
}

Glitch 3

    import processing.opengl.*;
    import processing.video.*;
    import com.jogamp.opengl.GL;
    import com.jogamp.opengl.GL2ES2;

    Capture video;

    PJOGL pgl;
    GL2ES2 gl;

    boolean rotar = false;

    void setup() {
        size(640, 480, OPENGL);

        video = new Capture(this, width, height);

        video.start();

    }

    void draw() {

        noFill();
        lights();

        strokeWeight(3);
        background(0);

        pgl = (PJOGL) beginPGL();
          gl = pgl.gl.getGL2ES2();

        if (video.available()) {
          video.read();
          video.loadPixels();
          background(0);

         gl.glBlendColor(0.0,0.0,1,0.8);

          gl.glDisable(GL.GL_DEPTH_TEST);
          gl.glEnable(GL.GL_BLEND);
          gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

          pushMatrix();

          if (rotar){
            translate(width/2, height/2, 0);
            rotateZ(radians((mouseX-(width))));

            //rotateZ(radians(mouseX));
            rotateY(radians(-(mouseY-(height)))); //rotateY -(mouseY
            //rotateY(radians(mouseX/TWO_PI));
            translate(-width/2, -height/2, 0);
          }


          for (int y = 0; y < video.height; y+=5) {  //antes 5

              beginShape(POINTS);
                for (int x = 0; x < video.width; x++) {

                        int pixelValue = video.pixels[x+(y*video.width)];

                        stroke(red(pixelValue*2), green(pixelValue*2), blue(pixelValue*3), 255); //255

                        vertex (x*2, y*2, -(brightness(pixelValue)*2));  //100
                }
              endShape();
          }

          popMatrix();
          endPGL();
          saveFrame("img_###.png");
        }
    }

    void mouseClicked(){
      rotar = !rotar;
    }

Glitch 4

import processing.video.*;
Capture video;

int w=640/2, h=480/2;
int pixelDensity = 1;

void setup() {
  size(800, 600, OPENGL);
  background(255);
  video = new Capture(this, w, h);
  video.start();
  camera(100, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 1); // Adjust camera
}

void draw() {
  noCursor();
  background(255);
  camera(mouseX*2, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 1); // Adjust camera
  translate(width/2-w/2, height/2-h/2, 100);
  video.loadPixels(); // Load webcam feed to pixel array

  for (int y = 0; y< h; y++)
  {
    if (y%pixelDensity==0) {
      for (int x = 0; x< w; x++)
      {
        if (x%pixelDensity==0) {
          // Color extraction using bit-shifting
          color c= video.pixels[y*video.width+x];
          int r = c >> 16 & 0xFF; // Red
          int g = c >> 8 & 0xFF; // Green
          int b = c & 0xFF; // Blue
          stroke(c);
          point(30+x, y+30, ((int)(r+g+b)/3)+mouseY);
        }
      }
    }
  }
}

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

void keyPressed()
{
  if (keyCode == LEFT && pixelDensity >1)
  {
    pixelDensity--;
  }
  if (keyCode == RIGHT)
  {
    pixelDensity++;
  }
}

Glitch 5

import processing.video.*;
import controlP5.*;
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress dest;
ControlP5 cp5;
Capture cam;

float minSat = 100;
float minBri = 170;
float hueRange = 5;
int blur = 0;
boolean orignalVideo = true;
int[] selectedHue = {0, 75, 150};
int currentColor = 0;
int currentX = 0;
PGraphics filterColorImg;

void setup() {
  size(640, 360);

  /* start oscP5, listening for incoming messages at port 9000 */
  oscP5 = new OscP5(this, 9000);
  //dest = new NetAddress("127.0.0.1",6448);
  dest = new NetAddress("192.168.43.62", 6448);

  cp5 = new ControlP5(this);
  cp5.addSlider("currentColor", 0, 2).setNumberOfTickMarks(3).linebreak();
  cp5.addSlider("minSat", 0, 255).linebreak();
  cp5.addSlider("minBri", 0, 255).linebreak();
  cp5.addSlider("hueRange", 0, 100).linebreak();
  cp5.addSlider("blur", 0, 30).linebreak();
  cp5.addToggle("orignalVideo");

  String[] cameras = Capture.list();
  cam = new Capture(this, cameras[0]);
  cam.start();
  filterColorImg = createGraphics(width, height);
  colorMode(HSB);
}

void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  fastblur(cam, blur); //Apply fastblur

  pushMatrix();
  scale(-1, 1);
  image(cam, - (width), 0, width, height);
  loadPixels();
  popMatrix();

  filterColorImg.beginDraw();
  filterColorImg.background(0);
  OscMessage msg = new OscMessage("/test");

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int loc = x + y*width;
      float h = hue(get(x, y));
      float s = saturation(get(x, y));
      float b = brightness(get(x, y));

      for (int i = 0; i<3; i++) {
        if ((h<(selectedHue[i]+hueRange) && h>(selectedHue[i]-hueRange)) && s > minSat && b > minBri) {
          pixels[loc] =  color(h, s, b);
          filterColorImg.set(x,y,color(h, s, b));
          if (x==currentX) {
            msg.add((float)1);

            msg.add(map(s+b, 0, 255+255, 0, 1));
          }
        } else {
          pixels[loc] =  color(0);
          if (x==currentX) {
            msg.add((float)0);
          }
        }
      }
    }
  }
  filterColorImg.endDraw();

  //oscP5.send(msg, dest);
  //println(msg);

  //updatePixels();
  if (orignalVideo) {
    pushMatrix();
    scale(-1, 1);
    image(cam, - (width), 0, width, height);
    popMatrix();
  } else {
   image(filterColorImg, 0, 0);
  }

  drawUI();
  drawTimer();
  currentX+=1;

  if (currentX>width) currentX = 0;
}

void mousePressed() {
  if (mouseX>180 || mouseY >180)
    selectedHue[currentColor] = (int)hue(get(mouseX, mouseY));
}

void drawUI() {
  int h = 180;
  int w = 180;
  for (int i = 0; i<selectedHue.length; i++) {
    fill(selectedHue[i], 255, 255);
    if (currentColor == i) {
      stroke(255);
    } else {
      noStroke();
    }
    rect(5 + i * 17, 5, 13, 13);
  }
  stroke(0);
  line(w, 0, w, h);
  line( 0, h, w, h);
}

void drawTimer() {
  stroke(255);
  line(currentX, 0, currentX, height);
}

void fastblur(PImage img, int radius)
{
  if (radius<1) {
    return;
  }
  int w=img.width;
  int h=img.height;
  int wm=w-1;
  int hm=h-1;
  int wh=w*h;
  int div=radius+radius+1;
  int r[]=new int[wh];
  int g[]=new int[wh];
  int b[]=new int[wh];
  int rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw;
  int vmin[] = new int[max(w, h)];
  int vmax[] = new int[max(w, h)];
  int[] pix=img.pixels;
  int dv[]=new int[256*div];
  for (i=0; i<256*div; i++) {
    dv[i]=(i/div);
  }

  yw=yi=0;

  for (y=0; y<h; y++) {
    rsum=gsum=bsum=0;
    for (i=-radius; i<=radius; i++) {
      p=pix[yi+min(wm, max(i, 0))];
      rsum+=(p & 0xff0000)>>16;
      gsum+=(p & 0x00ff00)>>8;
      bsum+= p & 0x0000ff;
    }
    for (x=0; x<w; x++) {

      r[yi]=dv[rsum];
      g[yi]=dv[gsum];
      b[yi]=dv[bsum];

      if (y==0) {
        vmin[x]=min(x+radius+1, wm);
        vmax[x]=max(x-radius, 0);
      }
      p1=pix[yw+vmin[x]];
      p2=pix[yw+vmax[x]];

      rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
      gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
      bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
      yi++;
    }
    yw+=w;
  }

  for (x=0; x<w; x++) {
    rsum=gsum=bsum=0;
    yp=-radius*w;
    for (i=-radius; i<=radius; i++) {
      yi=max(0, yp)+x;
      rsum+=r[yi];
      gsum+=g[yi];
      bsum+=b[yi];
      yp+=w;
    }
    yi=x;
    for (y=0; y<h; y++) {
      pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
      if (x==0) {
        vmin[y]=min(y+radius+1, hm)*w;
        vmax[y]=max(y-radius, 0)*w;
      }
      p1=x+vmin[y];
      p2=x+vmax[y];

      rsum+=r[p1]-r[p2];
      gsum+=g[p1]-g[p2];
      bsum+=b[p1]-b[p2];

      yi+=w;
    }
  }
}


import processing.video.*;
import controlP5.*;

int numPixels;
Capture video;
int keyColor = 0xff000000;
int keyR = (keyColor >> 16) & 0xFF;
int keyG = (keyColor >> 8) & 0xFF;
int keyB = keyColor & 0xFF;
PVector chromaAreaStart;
PVector chromaAreaEnd;
boolean setChromaArea = false;
int thresh = 20; // tolerance of
ControlP5 cp5;

void setup() {
  size(960, 720);

  video = new Capture(this, width, height);
  numPixels = video.width * video.height;

  video.start();
  addSlider();

  chromaAreaStart = new PVector(0, 0);
  chromaAreaEnd = new PVector(width, height);
}

void slider(int theThreshold) {
  thresh = theThreshold;
  println("a slider event. setting background to "+theThreshold);
}

void addSlider() {
  noStroke();
  cp5 = new ControlP5(this);

  // add a vertical slider
  cp5.addSlider("slider")
    .setPosition(10, 10)
    .setSize(200, 20)
    .setRange(0, 255)
    .setValue(thresh)
    ;
}

void draw() {
  if (video.available()) {
    background(0xFFFFFF);
    loadPixels();
    video.read(); // Read a new video frame
    video.loadPixels(); // Make the pixels of video available

    for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
      // Fetch the current color in that location
      color currColor = video.pixels[i];
      int currR = (currColor >> 16) & 0xFF; // apparently this is faster than using red(currColor);
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;

      // Compute the difference of the red, green, and blue values
      int diffR = abs(currR - keyR);
      int diffG = abs(currG - keyG);
      int diffB = abs(currB - keyB);

      // Render the pixels wich are not the close to the keyColor to the screen
      int pixelX = i % width;
      int pixelY = floor(i/width);

      if ((diffR + diffG + diffB)> thresh && isWithinChromeArea(pixelX, pixelY)) {
        pixels[i] = color(currR, currG, currB);
      }
    }
    updatePixels();

    if (setChromaArea) {
      fill(255, 0, 255, 60);
      noStroke();
      int rectW = abs(round(chromaAreaEnd.x - chromaAreaStart.x));
      int rectH = abs(round(chromaAreaEnd.y - chromaAreaStart.y));
      int startX = min(int(chromaAreaStart.x), int(chromaAreaEnd.x));
      int startY = min(int(chromaAreaStart.y), int(chromaAreaEnd.y));
      rect(startX, startY, rectW, rectH);
    }
  }
}

boolean isWithinChromeArea(int xPos, int yPos) {
  boolean withinChromaArea = true;

  int startX = min(int(chromaAreaStart.x), int(chromaAreaEnd.x));
  int startY = min(int(chromaAreaStart.y), int(chromaAreaEnd.y));
  int endX = max(int(chromaAreaStart.x), int(chromaAreaEnd.x));
  int endY = max(int(chromaAreaStart.y), int(chromaAreaEnd.y));

  withinChromaArea = xPos > startX && xPos < endX;

  if (withinChromaArea) {
    withinChromaArea = yPos > startY && yPos < endY;
  }

  return withinChromaArea;
}

void mousePressed() {
  if (keyPressed) {
    chromaAreaStart = new PVector(mouseX, mouseY);
  } else if (mouseY > 100) {
    setChromaColour();
  }
}

void mouseDragged() {
  if (keyPressed) {
    setChromaArea = true;
    chromaAreaEnd = new PVector(mouseX, mouseY);
  }
}

void mouseReleased() {
  setChromaArea = false;
}

void setChromaColour() {
  keyColor = get(mouseX, mouseY);
  keyR = (keyColor >> 16) & 0xFF;
  keyG = (keyColor >> 8) & 0xFF;
  keyB = keyColor & 0xFF;
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles