need advice for motion tracking
in
Core Library Questions
•
1 years ago
Hi,
I cannot finish the script as I try to add motion difference to another script.
Could you say where is a problem?
I am new to processing and I think I made mistakes when I tried to combine different scripts.
Here is the script:
import processing.video.*;
int numPixels;
int[] previousFrame;
Capture video;
int mcount = 0;
int thresh = 40;
boolean cheatScreen;
String letterOrder =
"jjhkjhkglkhn" +
"mkjloaeda";
char[] letters;
float[] bright;
char[] chars;
PFont font;
float fontSize = 1.5;
public void setup() {
size(640, 480, P2D);
video = new Capture(this, 80, 60, 15);
numPixels = video.width * video.height;
int count = video.width * video.height;
previousFrame = new int[numPixels];
loadPixels();
font = loadFont("KaiTi-48.vlw");
letters = new char[256];
for (int i = 0; i < 256; i++) {
int index = int(map(i, 0, 256, 0, letterOrder.length()));
letters[i] = letterOrder.charAt(index);
}
chars = new char[count];
bright = new float[count];
for (int i = 0; i < count; i++) {
bright[i] = 128;
}
}
public void captureEvent(Capture c) {
c.read();
}
void draw() {
background(0);
pushMatrix();
float hgap = width / float(video.width);
float vgap = height / float(video.height);
scale(max(hgap, vgap) * fontSize);
textFont(font, fontSize);
int index = 0;
translate(0, 1.0 / fontSize);
pushMatrix();
for (int x = 0; x < video.width; x++) {
int pixelColor = video.pixels[index];
// Faster method of calculating r, g, b than red(), green(), blue()
if (video.available()) {
video.read();
video.loadPixels();
int movementSum = 0;
int curPixel = 0;
for(int y = 0; y < video.width; y++){
int i = curPixel;
curPixel++;
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 = (prevColor >> 16) & 0xFF;
int prevG = (prevColor >> 8) & 0xFF;
int prevB = prevColor & 0xFF;
int diffR = abs(currR - prevR);
int diffG = abs(currG - prevG);
int diffB = abs(currB - prevB);
movementSum += diffR + diffG + diffB;
if((diffR > thresh) || (diffR < (0 - thresh)) || (diffG > thresh) || (diffG < (0 - thresh)) || (diffB > thresh) || (diffB < (0 - thresh))){
pixels[i] = color(255, 255, 0);
} else {
pixels[i] = color(currR, currG, currB);
}
previousFrame[i] = currColor;
}
}
if (movementSum > 0) {
updatePixels();
int pixelBright = max(currR, currG, currB);
float diff = pixelBright - bright[index];
bright[index] += diff * 0.1;
fill(pixelColor);
int num = int(bright[index]);
text(letters[num], 0, 0);
index++;
translate(1.0 / fontSize, 0);
}
popMatrix();
}
popMatrix();
if (cheatScreen) {
set(0, height - video.height, video);
}
}
I cannot finish the script as I try to add motion difference to another script.
Could you say where is a problem?
I am new to processing and I think I made mistakes when I tried to combine different scripts.
Here is the script:
import processing.video.*;
int numPixels;
int[] previousFrame;
Capture video;
int mcount = 0;
int thresh = 40;
boolean cheatScreen;
String letterOrder =
"jjhkjhkglkhn" +
"mkjloaeda";
char[] letters;
float[] bright;
char[] chars;
PFont font;
float fontSize = 1.5;
public void setup() {
size(640, 480, P2D);
video = new Capture(this, 80, 60, 15);
numPixels = video.width * video.height;
int count = video.width * video.height;
previousFrame = new int[numPixels];
loadPixels();
font = loadFont("KaiTi-48.vlw");
letters = new char[256];
for (int i = 0; i < 256; i++) {
int index = int(map(i, 0, 256, 0, letterOrder.length()));
letters[i] = letterOrder.charAt(index);
}
chars = new char[count];
bright = new float[count];
for (int i = 0; i < count; i++) {
bright[i] = 128;
}
}
public void captureEvent(Capture c) {
c.read();
}
void draw() {
background(0);
pushMatrix();
float hgap = width / float(video.width);
float vgap = height / float(video.height);
scale(max(hgap, vgap) * fontSize);
textFont(font, fontSize);
int index = 0;
translate(0, 1.0 / fontSize);
pushMatrix();
for (int x = 0; x < video.width; x++) {
int pixelColor = video.pixels[index];
// Faster method of calculating r, g, b than red(), green(), blue()
if (video.available()) {
video.read();
video.loadPixels();
int movementSum = 0;
int curPixel = 0;
for(int y = 0; y < video.width; y++){
int i = curPixel;
curPixel++;
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 = (prevColor >> 16) & 0xFF;
int prevG = (prevColor >> 8) & 0xFF;
int prevB = prevColor & 0xFF;
int diffR = abs(currR - prevR);
int diffG = abs(currG - prevG);
int diffB = abs(currB - prevB);
movementSum += diffR + diffG + diffB;
if((diffR > thresh) || (diffR < (0 - thresh)) || (diffG > thresh) || (diffG < (0 - thresh)) || (diffB > thresh) || (diffB < (0 - thresh))){
pixels[i] = color(255, 255, 0);
} else {
pixels[i] = color(currR, currG, currB);
}
previousFrame[i] = currColor;
}
}
if (movementSum > 0) {
updatePixels();
int pixelBright = max(currR, currG, currB);
float diff = pixelBright - bright[index];
bright[index] += diff * 0.1;
fill(pixelColor);
int num = int(bright[index]);
text(letters[num], 0, 0);
index++;
translate(1.0 / fontSize, 0);
}
popMatrix();
}
popMatrix();
if (cheatScreen) {
set(0, height - video.height, video);
}
}
1