Anyway to have the cursor follow a colour tracking point?
in
Contributed Library Questions
•
2 years ago
so im building a drawing sketch where you draw with colour tracking (very original...)
but as im not having the video drawn on the stage your pretty much blind to as where your brush will paint.
so is there a simple solution to having either the cursor follow the tracked coordinates or have a ellipse follow it without being drawn onto the stage?
heres the code if it helps,
thanks in advance.
Neil
but as im not having the video drawn on the stage your pretty much blind to as where your brush will paint.
so is there a simple solution to having either the cursor follow the tracked coordinates or have a ellipse follow it without being drawn onto the stage?
heres the code if it helps,
thanks in advance.
Neil
- import processing.video.*;
//camera
Capture video;
color trackColor;
int[] xpos = new int[50];
int[] ypos = new int[50];
//camera
import controlP5.*;
ControlP5 controlP5;
int myColor = color(255,255,0);
int StrokeSize = 10;
ColorPicker cp;
Textlabel myTextlabelA;
Textlabel myTextlabelB;
Textlabel myTextlabelC;
void setup() {
size(640,480);
frameRate(60);
background(255);
// load the custom cursor shape from svg
cursorsvg = loadShape("cursor.svg");
// create PGraphic for changing colors
cursorcolored = createGraphics(30, 30, JAVA2D);
controlP5 = new ControlP5(this);
controlP5.addSlider("StrokeSize",01,120,60,10,80,100,100);
// numbers minimum maximum default. then position.
cp = controlP5.addColorPicker("picker",10,10,100,20);
controlP5.setControlFont(new ControlFont(createFont("Verdana",8), 8));
myTextlabelA = controlP5.addTextlabel("label","HOLD DOWN SPACE TO DRAW",5,470);
myTextlabelA.setColorValue(0xffcccccc);
myTextlabelB = controlP5.addTextlabel("labelB","PRESS C TO CLEAR",5,450);
myTextlabelB.setColorValue(0xffcccccc);
myTextlabelC = controlP5.addTextlabel("labelC","PRESS S TO SAVE",5,430);
myTextlabelC.setColorValue(0xffcccccc);
//camera
video = new Capture(this,width-1,height,60);
// Start off tracking for red
trackColor = color(102,255,255);
//camera
//camera
for (int i = 0; i < xpos.length; i ++ ) {
xpos[i] = 0;
ypos[i] = 0;
}}
//camera
void draw() {
noStroke();
fill(75);
rect(0,0,135,480);
noStroke();
fill(cp.getColorValue());
ellipse(67,350,StrokeSize,StrokeSize);
noStroke();
//camera
// Capture and display the video
if (video.available()) {
video.read();
}
// 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 = 100;
// 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 = (video.width - x - 1) + 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;
}
}
}
// Shift array values
for (int i = 0; i < xpos.length-1; i ++ ) {
// Shift all elements down one spot.
// xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
// New location
xpos[xpos.length-1] = closestX; // Update the last spot in the array with the mouse location.
ypos[ypos.length-1] = closestY;
mouseX = closestX;
mouseY = closestY;
//camera
if(keyPressed) {
if (key == ' ' || key == ' ') {
fill(0);
stroke(cp.getColorValue());
strokeWeight(StrokeSize);
line(mouseX, mouseY, pmouseX, pmouseY);}
if (key == 'c' || key == 'c') {
background(255);}
if (key == 'C' || key == 'C') {
background(255);}
if (key == 's' || key == 's') {
save("sketchydrawing.png");}
if (key == 'S' || key == 'S') {
save("sketchydrawing.png");}
} else {
fill(255,0);
line(mouseX, mouseY, pmouseX, pmouseY);
}
void slider(float theColor) {
myColor = color(theColor);
println("a slider event. setting background to "+theColor);
}
1