Hey Chrisir,
Unfortunately, my code still doesn’t seem to get working. Maybe if I share the whole code, my question will get more clear.
import processing.video.*;
Capture video;
PImage prev;
PImage img;
float threshold = 25;
float motionX = 0;
float motionY = 0;
float lerpX = 0;
float lerpY = 0;
int time;
void setup() {
size(640, 360);
String[] cameras = Capture.list();
printArray(cameras);
video = new Capture(this, cameras[4]);
video.start();
prev = createImage(640, 360, RGB);
time = millis();
img = loadImage("bestand.jpg");
}
void mousePressed() {
}
void captureEvent(Capture video) {
prev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
prev.updatePixels();
video.read();
}
void draw() {
background(0);
video.loadPixels();
prev.loadPixels();
threshold = 50;
int count = 0;
float avgX = 0;
float avgY = 0;
loadPixels();
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
int loc = x + y * video.width;
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
color prevColor = prev.pixels[loc];
float r2 = red(prevColor);
float g2 = green(prevColor);
float b2 = blue(prevColor);
float d = distSq(r1, g1, b1, r2, g2, b2);
if (d > threshold*threshold) {
avgX += x;
avgY += y;
count++;
pixels[loc] = color(255);
} else {
pixels[loc] = color(0);
}
}
}
updatePixels();
if(lerpX > width /2 && lerpY > height /2 && (millis() - time > 3000)) {
//keyPressed && key == 'n'))
fill(0, 0, 0);
//time = millis();
}
else{
}
if(lerpX > width /2 && lerpY < height /2 && (millis() - time > 3000)) {
//keyPressed && key == 'n'))
fill(0, 0, 255);
//time = millis();
}
else{
}
if(lerpX < width /2 && lerpY > height /2 && (millis() - time > 3000)) {
//keyPressed && key == 'n'))
fill(225, 0, 0);
//time = millis();
}
else{
}
if(lerpX < width /2 && lerpY < height /2 && (millis() - time > 3000)) {
//keyPressed && key == 'n'))
fill(0, 225, 0);
//time = millis();
}
else{
}
rect(0, 0, width, height);
if (count > 200) {
motionX = avgX / count;
motionY = avgY / count;
}
lerpX = lerp(lerpX, motionX, 0.1);
lerpY = lerp(lerpY, motionY, 0.1);
fill(255, 0, 255);
strokeWeight(2.0);
stroke(0);
ellipse(lerpX, lerpY, 36, 36);
}
float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
return d;
}
The thing which I try to accomplish, is for the color of the screen to change once the hand has touched a plane for three seconds. This means the seconds should start to count once the hand is on a certain point. If the hand has been there for 3 seconds, the color of the screen should change. This colour should stay on the screen, but I still have to figure out how to execute the code just once to make this happen. I think I have to use a Boolean for this, but my attempts haven’t been successful yet.