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

How to combine codes so that my camera is in black and white

$
0
0

Hi, i'm new to these forums and hope somebody might be able to help me. I have the following code which uses a camera and displays it as a series of points taking the colour of pixels from the display. (Hope that makes sense, not sure how I can describe it effectively, sorry)

class CPoint
{
  //VARIABLES
  float x,y;
  float diameter;
  color pcolor;

  //CONSTRUCTOR
  //CPoint(x,y,diameter,color)
  CPoint(float CPx, float CPy, float CPdiam, color CPcolor)
  {
    x=CPx;
    y=CPy;
    diameter=CPdiam;
    pcolor=CPcolor;
  }

  //DISPLAYING FUNCTION
  void display()
  {
    pushMatrix();
    translate(x,y);
    noStroke();
    fill(pcolor);
    ellipse(0,0,diameter,diameter);
    popMatrix();
  }
}


import processing.video.*;

float cdiam=15;
color ccolor=color(252,245,15);
int npoints=20;
float xdivider;
float ydivider;
PImage picture;

Capture firstcam;

//Object array
CPoint[][] cpoints=new CPoint[npoints][npoints];

//SETUP
void setup()
{

  size(640,360);
  noStroke();
  smooth();

  String[]devices=Capture.list();
  println(devices);
  firstcam=new Capture(this,devices[3]);
  firstcam.start();

  xdivider=width/(npoints+1);
  float xc=xdivider;
  ydivider=height/(npoints+1);
  float yc=ydivider;

  for (int j=0; j<npoints; j++)
  {
    for (int i=0; i<npoints;i++)
    {

      cpoints[i][j]=new CPoint(xc,yc,cdiam,ccolor);
      xc=xc+xdivider;
    }
    xc=xdivider;
    yc=yc+ydivider;
  }
  frameRate(12);
}

//DRAW
void draw()
{
  background(0);
  if(firstcam.available()==true)
  {
      firstcam.read();

    for (int j=0; j<npoints; j++)
    {
      for (int i=0; i<npoints; i++)
      {
        int imgX=int(cpoints[i][j].x);
        int imgY=int(cpoints[i][j].y);

        ccolor=firstcam.get(imgX,imgY);

        cpoints[i][j].pcolor=ccolor;
        cpoints[i][j].display();
      }
    }
  }
}

I also have a code to turn an image black and white below:

PImage opera;
int pixelcount;
color pixelcolor;
color newcol;


void setup()
{
  size(800,600);
  colorMode(HSB,360,100,100);
}

void draw()
{
  opera=loadImage("OperaHse.jpg");
  pixelcount=opera.width*opera.height;
  opera.loadPixels();

  for (int pos=0;pos<pixelcount; pos++)
  {
    pixelcolor=opera.pixels[pos];

    float H=hue(pixelcolor);
    float S=saturation(pixelcolor);
    float B=brightness(pixelcolor);

    //Black and White
    H=0;
    S=0;

    newcol=color(H,S,B);
    opera.pixels[pos]=newcol;
  }

  image(opera,0,0,width,height);
}

(Opera referred to the image which was Sydney Opera House)

I've tried combining the two so the image from the camera is in black and white, but I'm struggling. I hope somebody might be able to help me.

Thanks in advance


Viewing all articles
Browse latest Browse all 2896

Trending Articles