We are about to switch to a new forum software. Until then we have removed the registration on this forum.
What's wrong with my code. When I run the code, it pops out a grey windows nothing inside there. The camera should be activated and detect motion in the screen
import processing.video.*;
int numPixels;
int[] previousFrame;
Capture video;
void setup(){
size (640, 480);
video = new Capture (this, width, height);
numPixels = video.width * video.height;
previousFrame = new int[numPixels];
video.start();
}
void draw() {
if (video.available()){
video.read();
video.loadPixels();
int movementSum=0;
loadPixels();
for (int i = 0; i < numPixels; i++){
color currColor = video.pixels[i];
color prevColor = previousFrame[i];
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int prevR = (currColor >> 16) & 0xFF;
int prevG = (currColor >> 8) & 0xFF;
int prevB = currColor & 0xFF;
int diffR = abs(currR - prevR);
int diffG = abs(currG - prevG);
int diffB = abs(currB - prevB);
movementSum += diffR + diffG + diffB;
pixels[i] = color(diffR,diffG,diffB);
previousFrame[i] = currColor;
}
if (movementSum > 0){
updatePixels();
println(movementSum);
}
}
}
Answers
I think if you add
video.start();
in setup(), it should work.
Kf
Same. Added video.start(). Nothing happens.
You have fallen into the trap of cut and paste. Lines 30-31 need to be changed to make use of the
prevColor
otherwise there will never be any change.I got interested in this and had a go fixing this.
You can smooth the motion by using the lerp function