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.
Page Index Toggle Pages: 1
pixels[] (Read 1661 times)
pixels[]
Apr 22nd, 2005, 5:07pm
 
I've been using copy() and pixels a lot with images in memory. I can't make head nor tail of the new functions though. I don't know if I'm supposed to be using loadPixels on a PImage or from where I'm calling copy().

The idea is simple, I measure the difference between one frame and the next. I've added a bit of fade to it as I'm still working on it but I've ground to a halt trying to figure out p85 syntax.

Old Code:
Code:

boolean newFrame = false;
BImage swap;
BImage display;
BGraphics buf;
void setup() {
size(320, 240);
swap = loadImage("a.gif");
display = swap.copy();
background(255);
beginVideo(320, 240, 12);
}
void videoEvent() {
newFrame = true;
}
void loop() {
//background(display);
if(newFrame) {
// noStroke();
fill(255,255,255,30);
rect(0,0,width,height);
difference();
newFrame = false;
swap = video.copy();
}
}
void difference(){
for (int iy = 0; iy < video.height; iy++){
for (int ix = 0; ix < video.width; ix++){
color c1 = video.pixels[ix + iy * video.width];
color c2 = swap.pixels[ix + iy * swap.width];
int r1=c1>>16&0xff;
int g1=c1>>8&0xff;
int b1=c1&0xff;
int r2=c2>>16&0xff;
int g2=c2>>8&0xff;
int b2=c2&0xff;
//display.pixels[ix + iy * display.width] = color (255-abs(r1-r2),255-abs(g1-g2),255-abs(b1-b2), 50);
if (abs(r1-r2) > 0 && abs(g1-g2) > 0 && abs(b1-b2) > 0){
stroke(color (255-abs(r1-r2),255-abs(g1-g2),255-abs(b1-b2), 50));
point(ix,iy);
}
}
}
}

New, and still not working code:
Code:

import processing.video.*;
Capture myCapture;
PImage swap;
PImage display;
void setup() {
size(320, 240);
String s = "Creative WebCam Live! Pro-WDM";
myCapture = new Capture(this, s, width, height, 12);
swap = loadImage("a.gif");
display = loadImage("a.gif");//.copy(swap,0,0,swap.width,swap.height,0,0,swap.width,swap.height);
//still can't figure out copy()
background(255);
}
void captureEvent(Capture myCapture) {
myCapture.read();
}
void draw() {
fill(255,255,255,30);
rect(0,0,width,height);
difference();
swap.copy(myCapture,0,0,myCapture.width,myCapture.height,0,0,swap.width,swap.height);
image(display, 0, 0);

}
void difference(){
for (int iy = 0; iy < myCapture.height; iy++){
for (int ix = 0; ix < myCapture.width; ix++){
color c1 = myCapture.pixels[ix + iy * myCapture.width];
color c2 = swap.pixels[ix + iy * swap.width];
int r1=c1>>16&0xff;
int g1=c1>>8&0xff;
int b1=c1&0xff;
int r2=c2>>16&0xff;
int g2=c2>>8&0xff;
int b2=c2&0xff;
//display.pixels[ix + iy * display.width] = color (255-abs(r1-r2),255-abs(g1-g2),255-abs(b1-b2), 50);
if (abs(r1-r2) > 0 && abs(g1-g2) > 0 && abs(b1-b2) > 0){
stroke(color (255-abs(r1-r2),255-abs(g1-g2),255-abs(b1-b2), 50));
point(ix,iy);
}
}
}
}


Could somebody set me straight please?
Re: pixels[]
Reply #1 - Apr 26th, 2005, 1:35pm
 
I've looked at the examples in reference and it doesn't seem to be the case that I have to use loadPixels() or updatePixels() with images in memory. But with the code above there seems to be a need to say "display.updatePixels()" or else it feeds me a black image. Why is this?
Code:

import processing.video.*;
Capture myCapture;
PImage swap,display;
void setup() {
size(320, 240);
String s = "Creative WebCam Live! Pro-WDM";
myCapture = new Capture(this, s, width, height, 12);
swap = loadImage("a.gif"); //blank .gif the size of video
display = loadImage("a.gif");
//can't seem to do the p68 trick of creating a blank image with copy
//display.copy(swap,0,0,swap.width,swap.height,0,0,swap.width,swap.height);
background(255);
}
void captureEvent(Capture myCapture){
myCapture.read();
}
void draw() {
difference();
swap.copy(myCapture,0,0,myCapture.width,myCapture.height,0,0,swap.width,swap.height);
image(display, 0, 0);
}
void difference(){
for (int iy = 0; iy < myCapture.height; iy++){
for (int ix = 0; ix < myCapture.width; ix++){
color c1 = myCapture.pixels[ix + iy * myCapture.width];
color c2 = swap.pixels[ix + iy * swap.width];
int r1=c1>>16&0xff;
int g1=c1>>8&0xff;
int b1=c1&0xff;
int r2=c2>>16&0xff;
int g2=c2>>8&0xff;
int b2=c2&0xff;
display.pixels[ix + iy * display.width] = color (255-abs(r1-r2),255-abs(g1-g2),255-abs(b1-b2),100);
}
}
display.updatePixels(); //Why do I have to stick this line in?
}
Re: pixels[]
Reply #2 - Apr 26th, 2005, 5:25pm
 
you have to call updatePixels() so that opengl and java2d know that the image data has changed, because they both need to do post-processing on it.

we didn't need to do this before (because P2D and P3D don't need it) which was nice, but unfortunately it's just something we have to deal with in order to make opengl/java2d happen. (small price for opengl, right?)

loadPixels() just means "i'm reading to mess with things", and updatePixels() means "i'm done editing, make sure it gets updated on the next draw.
Re: pixels[]
Reply #3 - Apr 27th, 2005, 9:39pm
 
I think the documentation is not yet clear enough regarding when and where to use loadPixels() and updatePixels(). I'll work on it.
Page Index Toggle Pages: 1