We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › color grayData = get(22,22)
Page Index Toggle Pages: 1
color grayData = get(22,22) (Read 1186 times)
color grayData = get(22,22)
Dec 19th, 2009, 5:34am
 
Hey there,

i get confused byte results when i pic up a color value with this command:

color grayData = get(22,22)

the value is not right that what i need. Black is 0 and goes up to gray at +127 and then comes instead of +128, +129 +130...., a change of sign to -128 and goes on with count down -127 -126 -125 to -1 (white).

What the hell is wrong?? I need a row from x to y without no brake like this :

0,1,2,3....255,256

. I know thats an byte (127 to -128), but i dont know how is the right syntax to convert it.  Do you have a answer ?? Pleas help a bloody beginner.
Thanks
Re: color grayData = get(22,22)
Reply #1 - Dec 19th, 2009, 7:03am
 
You can extract the Red, Green and Blue components from color grayData

by using
red(grayData)
green(grayData)
blue(grayData)
Re: color grayData = get(22,22)
Reply #2 - Dec 19th, 2009, 7:50am
 
But i use the filter(GRAY) command before. Is it not the same? I get a binary string like this 11111111010111100101111001011110 also FF5E5E5E. There is all the same information in all colors (RGB) and alpha channel is filled with 11111111.
Re: color grayData = get(22,22)
Reply #3 - Dec 19th, 2009, 8:52am
 
Then the red, green and blue data will return the same values...
so you can use either of them.

Re: color grayData = get(22,22)
Reply #4 - Dec 19th, 2009, 9:18am
 
You're leaving out a full description of how you get these negative values, or even how you are extracting the "grey value" from the "color" variable.

"color" is basically the same as "int".

I'm guessing you've done something like:

byte greyLevel = red(greyData);

If you have created a variable of type "byte", it can only have values in the range -128 to +127. If you need a counting number higher than 127, you have to use another data type, such as "int".

My suggestion:

Code:
color grayData = get(22,22);
int greyLevel = greyData & 0xFF; // just use the red bits


-spxl
Re: color grayData = get(22,22)
Reply #5 - Dec 20th, 2009, 4:51am
 
Thank you very much for answering,
but it dos not work right now Sad


For a better understanding i should send you the whole code:



import processing.video.*;
import processing.serial.*;

Capture cam;

Serial myPort;  // Create object from Serial class


void setup() {
 
 size(200, 160);
 myPort = new Serial(this, Serial.list()[0], 115200, 'N', 8, 2.0);
 String[] devices = Capture.list();
 println(devices);
 cam = new Capture(this, 10, 8, devices[2]);
 
}


void draw() {
 rect(20,20,20,20); //rect(x, y, width, height)
 noFill();
 stroke(255);
 frameRate(5);
 
 if (cam.available() == true) {
   cam.read();
   cam.filter(GRAY);
   image(cam,0,0,200,160); //image(img, x, y, width, height)
   rect(21,21,2,2); //rect(x, y, width, height)
   noFill();
   stroke(255);
 }
 
 cam.loadPixels();
 color grayData = get(22,22) ; // den pixel aus dem quadrat von rect...
 int greyLevel = grayData & 0xFF;

 println ("  binary = "+binary(grayData)+"  hex = "+hex(grayData,6)+ "  byte = "+byte(grayData));  // als Beispiel : 11111111 01011101 01011101 01011101



things that can help. i got it from other kind discussions
//  byte b1 = (byte)(ceil(grayData) & 0xFF);
//  byte b2 = (byte)( (ceil(grayData) >>> 8) & 0xFF );
//  println (b1 +"  "+b2);


//  float grayDataToSend = ceil( map (grayData, 0, 255, 735, 2520));

 
   myPort.write(0xAA);
   myPort.write(0xA0);
   myPort.write(0x55);
   myPort.write(0x04);
   myPort.write(1);
   myPort.write(1);
   myPort.write(25);
   
   myPort.write(0xAA);
   myPort.write(0xA0);
   myPort.write(0x55);
   myPort.write(0x03);
   myPort.write(1);
   myPort.write(1);
   myPort.write(0);
   
   myPort.write(0xAA);
   myPort.write(0xA0);
   myPort.write(0x55);
   myPort.write(0x01);
   myPort.write(0x01);
   myPort.write(0x02);
   myPort.write(0xDC);  //here must be the low byte
   myPort.write(0x02); //and here the high byte of servoposition
}


The reason for that, asking your self
I would like to control a position of a simple servo motor (from max to max angle position) by brightness values of a web cam (one pixel from white to black). It shall be a nice installation with interact moving parts.

For this i use a iMac and a SD84 USB controller (http://www.robot-electronics.co.uk/htm/sd84tech.htm.

The problem is....i am a noob in Processing Undecided
Re: color grayData = get(22,22)
Reply #6 - Dec 21st, 2009, 4:57am
 
OK, the solution is the following code.

But i do not know why this transformation from color picking to hex shifting works Sad i try it so often and use little code sections from here and now Yippie!
Code:
import processing.video.*;
import processing.serial.*;

Capture cam;

Serial myPort; // Create object from Serial class


void setup() {

size(200, 160);
myPort = new Serial(this, Serial.list()[0], 115200, 'N', 8, 2.0);
String[] devices = Capture.list();
println(devices);
cam = new Capture(this, 10, 8, devices[2]);
colorMode (HSB,255);
}


void draw() {
rect(20,20,20,20); //rect(x, y, width, height)
noFill();
stroke(255);
frameRate(15);

if (cam.available() == true) {
cam.read();
cam.filter(GRAY);
image(cam,0,0,200,160); //image(img, x, y, width, height)
rect(21,0,2,2); //rect(x, y, width, height)
noFill();
stroke(255);
}

cam.loadPixels();
color gray = cam.pixels[27];

int B = gray & 0xFF;
println (B);

float Bmap = map (B, 0, 255, 735, 2520);
println (Bmap);

int roundBmap1 = round (Bmap);
int roundBmap2 = round (Bmap);

byte b1 = (byte)((roundBmap1) & 0xFF);
byte b2 = (byte)( ((roundBmap1) >>> 8) & 0xFF );

println (hex(b1));
println (hex(b2));


myPort.write(0xAA);
myPort.write(0xA0);
myPort.write(0x55);
myPort.write(0x04);
myPort.write(1);
myPort.write(1);
myPort.write(25);

myPort.write(0xAA);
myPort.write(0xA0);
myPort.write(0x55);
myPort.write(0x03);
myPort.write(1);
myPort.write(1);
myPort.write(0);

myPort.write(0xAA);
myPort.write(0xA0);
myPort.write(0x55);
myPort.write(0x01);
myPort.write(0x01);
myPort.write(0x02);
myPort.write(b1);
myPort.write(b2);
}


THX at all
Re: color grayData = get(22,22)
Reply #7 - Dec 23rd, 2009, 7:05am
 
Hey, it looks like you've essentially used what I suggested, though I note now that the description was incorrect; "masking" by bitwise-and-ing with 0xFF gets you the blue bits, not the red bits (hopefully, if the cam colour value is actually grey, there will be no difference).

zoodo wrote on Dec 21st, 2009, 4:57am:
 cam.loadPixels();
 color gray = cam.pixels[27]; // 32-bit ARGB value

 int B = gray & 0xFF; // Lowest 8 bits, the Blue bits, with a value from 0-255
 println (B);
 
 float Bmap = map (B, 0, 255, 735, 2520); // scaled value (make the 0-255 value "bigger")
 println (Bmap);
 
 int roundBmap1 = round (Bmap); // convert to integer
 int roundBmap2 = round (Bmap); // unused!
 
 byte b1 = (byte)((roundBmap1) & 0xFF); // low bits (bits 7-0) of scaled value
 byte b2 = (byte)( ((roundBmap1) >>> 8) & 0xFF ); // bits 15-8 of scaled value
 

Page Index Toggle Pages: 1