Color Tracking and createGraphics issue
in
Core Library Questions
•
2 years ago
Hello!
I am trying to develop some code to make some kind of virtual drawing board with a laser, the webcam detects the color from the laser and then draws a line from it.
So far i got the color detection and some work with createGraphics built in but i cant get the createGraphics "frame" on top of the video it is only behind it how do i get it on top of the video so i can get the desired effect ?
Any help would be really great , Thank you !
(this has the video slightly off stage so you can see what the createGraphics work is doing)
PGraphics pg;
import processing.video.*;
// Variable for capture device
Capture video;
// A variable for the color we are searching for.
color trackColor;
void setup() {
size(720,640);
pg = createGraphics(180, 180, P3D);
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);
}
void draw() {
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,200,200);
// 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 worldRecord = 500;
// XY coordinate of closest color
int closestX = 0;
int closestY = 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);
// Using euclidean distance to compare colors
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 current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}
// We only consider the color found if its color distance is less than 10.
// This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
if (worldRecord < 10) {
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(0.0);
stroke(0);
ellipse(closestX,closestY,18,18);
}
fill(0, 12);
rect(0, 0, width, height);
fill(255);
noStroke();
ellipse(mouseX, mouseY, 60, 60);
image(pg, 80, 80);
}
void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}
1