Hi there, I have problems with my code. what I want to do is tracking color pixel. I want to capture an image with the only traced color(without background video). so I used PGraphics, but It is so slow that the program can't track color well. I think using PGraphics makes the program slow. is there any advice to make tracking faster?
Here is my code.
import processing.video.*;
Capture video;
color trackColor= color(145,43,54);
float threshold = 25;
PGraphics topLayer;
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;
}
void setup() {
frameRate(60);
size(640, 360);
String[] cameras = Capture.list();
printArray(cameras);
video = new Capture(this, cameras[3]);
video.start();
trackColor = color(145,43,54);
topLayer = createGraphics(width, height, g.getClass().getName());
}
void captureEvent(Capture video) {
video.read();
}
void draw() {
video.loadPixels();
image(video, 0, 0);
//threshold = map(mouseX, 0, width, 0, 100);
threshold = 50;
int avgX = 0;
int avgY = 0;
float worldRecord = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
int loc = x + y * video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
//
float d = distSq(r1, g1, b1, r2, g2, b2);
if (d < threshold*threshold) {
worldRecord = d;
avgX = x;
avgY = y;
}
}
}
topLayer.beginDraw();
topLayer.stroke(trackColor);
if (worldRecord < threshold*threshold) {
topLayer.fill(trackColor);
topLayer.strokeWeight(1);
topLayer.point(avgX, avgY); ////////////////////////////////////////
}
topLayer.endDraw();
image(topLayer, 0, 0);
}