hi forum i was able to do realtime image processing (color recognition )streamed from laptop camera but not succeed to use thread(); or class extending thread. need for help. thank you in advance.
bellow is code without thread and works as needed
import processing.video.*;
// Variable for capture device
Capture video;
// A variable for the color we are searching for.
color trackColor;
float threshold = 80;
void setup() {
//size(320, 240);
size(640, 360);
video = new Capture(this, width, height);
video.start();
// Start off tracking for red
trackColor = color(255, 0, 0);
}
void captureEvent(Capture video) {
// Read image from the camera
video.read();
}
void draw() {
video.loadPixels();
image(video, 0, 0);
threshold = map(mouseX,0,width,0,100);
// Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
float avgX = 0;
float avgY = 0;
int count = 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 = dist(r1, g1, b1, r2, g2, b2);
if (d < threshold) {
stroke(255);
strokeWeight(1);
point(x,y);
avgX += x;
avgY += y;
count++;
}
}
}
if (count > 10) {
avgX = avgX / count;
avgY = avgY / count;
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(2.0);
stroke(0);
ellipse (avgX, avgY, 16, 16);
println(frameRate);
println(count);
}
}
void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}
and code bellow is my trial n error
import processing.video.*;
// Variable for capture device
Capture video;
XCO xco = new XCO();
// A variable for the color we are searching for.
color trackColor;
float threshold = 80;
void setup() {
//size(320, 240);
size(640, 360);
video = new Capture(this, width, height);
video.start();
// Start off tracking for red
trackColor = color(255, 0, 0);
xco.start();
}
void captureEvent(Capture video) {
// Read image from the camera
video.read();
}
void draw() {
video.loadPixels();
image(video, 0, 0);
}
void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}
class XCO extends Thread{
void run(){
calc();
}
void calc(){
float avgX = 0;
float avgY = 0;
int count = 0;
threshold = map(mouseX,0,width,0,100);
// 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 = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
if (d < threshold) {
stroke(255);
strokeWeight(1);
point(x,y);
avgX += x;
avgY += y;
count++;
}
}
}
if (count > 10) {
avgX = avgX / count;
avgY = avgY / count;
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(2.0);
stroke(0);
ellipse (avgX, avgY, 16, 16);
println(frameRate);
println(count);
}
}
}