FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Video, Camera
(Moderator: REAS)
   Reading pixels, web cam, help
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Reading pixels, web cam, help  (Read 2814 times)
cereals


Reading pixels, web cam, help
« on: Feb 3rd, 2004, 2:11am »

I have been working on this piece code, that is the begining of marker recognition. At the moment I am trying to implement a web cam, from the web cam it then maps the webcam via the texture property, on to a shape. The problem then is that I want to be able to create a blocky (pixelated) version of the video on the shape.
 
So when somebody holds up a marker to the camers, it is able to rotate it and adjust to a better angle to view it.  
 
The idea of the pixelated version is to reduce the cpu load, as it is now only scanning 32x24 not 320x240.
 
Make sense? anyway the code.. note very good at the mo.. stuck bits and pieces to show you..
 
 
// code
 
 
// VARIBLES
float angle=0,cosine=0;
// TEXTURE
BImage tex1,tex2; // texture
int texw=320, texh=240; // texture size
int new_texw=texw/2, new_texh=texh/2; // texture center point
int x=0, y=0; // texture position
// COLOUR POINT
Point tracker = new Point(0, 0); // orignal cross
// These will be updated, allowing them to change when init of piece (callorbration)
Color trackRGB = new Color(93, 53, 38 ); // background of poster
Color markerRGB = new Color(95, 157, 163); // image colour
// IMAGE MATRIX ARRAYS
int numPixels;
float n_col;
float[] n_r = new float[768];
float[] n_g = new float[768];
float[] n_b = new float[768];
// SETUP
void setup(){
  size(texw,texh); // x,y canvas
  background(0); // init bg
  //
  tex1 = loadImage("trans.gif");
  tex2 = loadImage("trans.jpg");
  beginVideo(texw, texh, 12);
}
// ON_ENTER_FRAME
void loop(){
  background(0); // clear the canvas
  image(video, 0, 0); // add video to canvas
  // GRAB THE VIDEO
  for(int i=0; i<texw; i++){
    for(int z=0; z<texh; z++){
      tex1.pixels[(i*texh)+z] = pixels[(i*texh)+z];
    }
  }
  // ROATIONAL EFFECT
  background(0); // clear background
  // Draw the texture on the square
  angle+=0.08;
  cosine=cos(angle)/2;
  drawTexSquare(tex1, cosine);
  // CONVERT TO BLOCKS (PIXELATE)
  blocky();
  //
  delay(50);
}
// DRAW TEXTURE ON SQUARE
void drawTexSquare(BImage mytexture,float yaw){
  translate(texw/2,texh/2);
  push(); // records the positiong
  rotate(yaw);
  noStroke();
  beginShape(QUADS);
  texture(mytexture);
  // vertex syntax, x, y, width, height
  vertex(x-new_texw, y-new_texh, 0, 0);
  vertex(x-new_texw, y+texh-new_texh, 0, texh);
  vertex(x+texw-new_texw, y+texh-new_texh, texw, texh);
  vertex(x+texw-new_texw, y-new_texh, texw, 0);
  endShape();
  // GRAB THE CANVAS PIXELS
  // copy this texture to another image, so then i can copyit again
  pop(); // stops the roation
  //
  for(int q=0; q<texw; q++){
    for(int w=0; w<texh; w++){
      tex2.pixels[(q*texh)+w] = pixels[(q*texh)+w];
    }
  }
  background(0); // make sure it is actually drawing the new image
  background(tex2);
  //print("start");
  //image(tex2, -new_texw, -new_texh);
  //image(tex2,0,0);
  //blocky();
}
// DRAW BLOCKS
void blocky(){
  int c=0;
  int num=0;
  for(int f=0; f<240; f=f+1){
    for(int g=0; g<320; g=g+1){
      if(f%10 ==0 && g%10 ==0){
      // PROBLEM IS HERE, I WANT TO COPY THE PIXELS FROM IMAGE tex2 OR THE stage, BUT IT WON'T ONLY VALUES 0.0 ??
        color pix = pixels[num];
        n_r[c] = red(pix);
        n_g[c] = green(pix);
        n_b[c] = blue(pix);
        c=c+1;
      }
      //
      num=num+1;
    }
  }
 
  int d=0;
  for(int a=0; a<24; a=a+1){
    for(int b=0; b<32; b=b+1){
      stroke(255);
      color new_r = (color) n_r[d];
      color new_g = (color) n_g[d];
      color new_b = (color) n_b[d];
      //fill(red(new_c),green(new_c),blue(new_c));
      fill(new_r, new_g, new_b);
      rect((b*10)-new_texw, (a*10)-new_texh, 10, 10);
      d=d+1;
    }
  }
}
 
//////////
 
Any help will be helpful
cheers.
« Last Edit: Feb 3rd, 2004, 2:12am by cereals »  
elout

12747371274737 WWW
Re: Reading pixels, web cam, help
« Reply #1 on: Feb 3rd, 2004, 3:09pm »

I did some webcam 3D stuff here, hope it helps.
 
http://processing.org/discourse/yabb/board_Contribution_Info_ac_tion_display_num_1064190417.html
 
 
cereals


Re: Reading pixels, web cam, help
« Reply #2 on: Feb 3rd, 2004, 9:07pm »

Cheers, i didn't find anything to help, good work though!
 
I'll just clarify my problem, I have placed an image on to the stage and then I want to read the values of the stage or the tex2 image, but it only gives values 0.0 for red, blue and green.
 
//
 
Am I doing anything wrong, with this?  
 
color pix = pixels[num];
 
I know,  color pix = video.pixels[num]; works! But I don't want the video, I need the stage colors or tex2 image values..
 
//
 
Problem function:
 
// DRAW BLOCKS  
void blocky(){  
  int c=0;  
  int num=0;  
  for(int f=0; f<240; f=f+1){  
    for(int g=0; g<320; g=g+1){  
 if(f%10 ==0 && g%10 ==0){  
   color pix = pixels[num];  
   n_r[c] = red(pix);  
   n_g[c] = green(pix);  
   n_b[c] = blue(pix);  
   c=c+1;  
 }  
 //  
 num=num+1;  
    }  
  }  
 
  int d=0;  
  for(int a=0; a<24; a=a+1){  
    for(int b=0; b<32; b=b+1){  
 stroke(255);  
 color new_r = (color) n_r[d];  
 color new_g = (color) n_g[d];  
 color new_b = (color) n_b[d];  
 fill(new_r, new_g, new_b);  
 rect((b*10)-new_texw, (a*10)-new_texh, 10, 10);  
 d=d+1;  
    }  
  }  
}  
 
//////////  
 
As before anyhelp would be great
 
//
cheers simon.
 
elout

12747371274737 WWW
Re: Reading pixels, web cam, help
« Reply #3 on: Feb 5th, 2004, 12:17pm »

This is how I read or set my rgb values.
Code:

//getting the rgb;
r=((mypix[i] >> 16) & 0xff);
g=((mypix[i] >>  8) & 0xff);
b=(mypix[i] & 0xff);
 
//setting rgb:
output[i] =  ( (0xff<<24) | (r<<16) | (g<<8) | b);

 
cereals


Re: Reading pixels, web cam, help
« Reply #4 on: Feb 5th, 2004, 7:33pm »

Cheers elout, after looking at you code, its unfortuantely causes the same problem as:
 
//getting the rgb;  
r=red(mypix[i]);  
g=green(mypix[i]);
b=blue(mypix[i]);  
 
...
 
I found that for some reason, the code colorizes the vertex shape?
 
...
 
Any body have any ideas how I can stop this..
 
« Last Edit: Feb 5th, 2004, 8:05pm by cereals »  
cereals


Re: Reading pixels, web cam, help
« Reply #5 on: Feb 6th, 2004, 12:42am »

Just like to say I have fixed the problem..
 
The way I got around it was to draw directly on to an image rather than using the fill() and rect() functions.
 
The new function if anybody is interested (excuse the sloppy code.. it works at the moment, so I am happy)
 
Code:

// DRAW BLOCKS
void blocky(){
  int c=0;
  int num=0, tum_len=0;
  color pix = pixels[0];
  float[] col_pix = new float[76800];
  //
  for(int f=0; f<240; f=f+1){
    for(int g=0; g<320; g=g+1){
 if(f%10 ==0 && g%10 ==0){
   for(int tum=tum_len; tum<(tum_len+10); tum=tum+1){
     for(int vum=num; vum<(num+10); vum=vum+1){
  col_pix[vum+(320*tum)] = pixels[num];
     }  
   }
 }
 num=num+1;
    }
  }
  // If doing rotateY or rotateZ then video overlays the 10x10 pixels.
  // resetting the background to 0, solves this.
  background(0);
  // Place the new pixels on the image
  int num2=0;
  for(int q=0; q<texw; q++){
    for(int w=0; w<texh; w++){
 tex2.pixels[(q*texh)+w] = (color) col_pix[num2];
 num2+=1;
    }
  }
  // Display the image
  image(tex2, -new_texw, -new_texh);
}
 

 
once again thanks for the help..
cheers elout..
 
 
toxi

WWW
Re: Reading pixels, web cam, help
« Reply #6 on: Feb 6th, 2004, 7:56pm »

hey, you could also collapse this code into just 3 lines by using the replicate() function(needs version 67 or newer):
 
Code:
void setup() {
  size(320,240);
}
 
void draw() {
  // just fill up image with random colours
  for(int i=0; i<pixels.length; i++) pixels[i]=(int)random(0xffffff);
  BImage tex=new BImage(32,24);
  // create a scaled down version of the image
  tex.replicate(g,0,0,width,height,0,0,tex.width,tex.height);
  //and copy back at window size
  replicate(tex,0,0,tex.width-1,tex.height-1,0,0,width,height);
}

 
hth, toxi.
 

http://toxi.co.uk/
cereals


Re: Reading pixels, web cam, help
« Reply #7 on: Feb 8th, 2004, 8:04pm »

wow.. cheers toxi.
..
a lot nicer looking code
 
kevinP

Email
Re: Reading pixels, web cam, help
« Reply #8 on: Feb 8th, 2004, 11:16pm »

on Feb 6th, 2004, 7:56pm, toxi wrote:
hey, you could also collapse this code into just 3 lines by using the replicate() function(needs version 67 or newer):

 
This is copying the upper-left corner and then scaling it up to the full window Is replicate documented somewhere
 
-K
 

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »