Skip to content

Instantly share code, notes, and snippets.

@juliangoulding
Created April 5, 2013 00:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangoulding/5315716 to your computer and use it in GitHub Desktop.
Save juliangoulding/5315716 to your computer and use it in GitHub Desktop.
This is an adaption of a processing sketch on learningprocessing.com http://www.learningprocessing.com/exa... The code takes the average of all the movement and moves the ball towards it. Simple really. Check out all the code and try it fro yourself here. Please post video responses if you do something cool with it!
import processing.video.*;
Capture video;
PImage prevFrame;
float threshold = 150;
int Mx = 0;
int My = 0;
int ave = 0;
int ballX = width/2;
int ballY = height/2;
int rsp = 25;
void setup() {
size(1024,720);
video = new Capture(this, width, height, 30);
prevFrame = createImage(video.width,video.height,RGB);
}
void draw() {
if (video.available()) {
prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
Mx = 0;
My = 0;
ave = 0;
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
color current = video.pixels[loc];
color previous = prevFrame.pixels[loc];
float r1 = red(current); float g1 = green(current); float b1 = blue(current);
float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
float diff = dist(r1,g1,b1,r2,g2,b2);
if (diff > threshold) {
pixels[loc] = video.pixels[loc];
Mx += x;
My += y;
ave++;
} else {
pixels[loc] = video.pixels[loc];
}
}
}
fill(255);
rect(0,0, width, height);
if(ave != 0){
Mx = Mx/ave;
My = My/ave;
}
if (Mx > ballX + rsp/2 && Mx > 50){
ballX+= rsp;
}else if (Mx < ballX - rsp/2 && Mx > 50){
ballX-= rsp;
}
if (My > ballY + rsp/2 && My > 50){
ballY+= rsp;
}else if (My < ballY - rsp/2 && My > 50){
ballY-= rsp;
}
updatePixels();
noStroke();
fill(200,0,0);
ellipse(ballX, ballY, 50, 50);
}
@bilal83
Copy link

bilal83 commented May 3, 2018

hello,
it is nice work... but I do not understand what is rsp means?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment